CabConModding
Facebook
Twitter
youtube
Discord
Contact us
RSS
Menu
CabConModding
Home
New
Top
Premium
Rules
FAQ - Frequently Asked Questions
Games
Fornite
Call of Duty: Black Ops 3
Clash of Clans
Grand Theft Auto 5
Apex Legends
Assassin’s Creed Origins
Forums
Premium
Latest posts
What's new
Latest posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Log in
Register
What's new
Premium
Latest posts
Menu
Log in
Register
Navigation
Install the app
Install
More options
Dark Theme
Contact us
Close Menu
Forums
Gaming
Call of Duty Classics
Call of Duty: Black Ops 3
Call of Duty: Black Ops 3 Mods and Scripts
Call of Duty: Black Ops 3 Scripts
Call of Duty: Black Ops 3 Scripts Question
Call of Duty: Black Ops 3 Answered Questions
Getting keys of multidimensional arrays
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Cxwh" data-source="post: 14865" data-attributes="member: 22664"><p>I am not 100% sure as to what you mean, but I think this is what you're looking for:</p><p>[CODE]</p><p>function searchArrayKey(Array, value)</p><p>{</p><p> foreach(key, val in Array)</p><p> {</p><p> if(value == val)</p><p> return key;</p><p> if(isArray(val))</p><p> return searchArrayKey(val, value);</p><p> }</p><p> return false;</p><p>}</p><p>[/CODE]</p><p></p><p>I tested it in php and it worked, but note:</p><p>If you have if multiple keys returning the same value, only the first one of them will be returned</p><p>[PHP]</p><p><?php</p><p>$_69 = ["6", "9"];</p><p>$_911 = ["9", "1", "1"];</p><p>$testArray = ["Bush", "did", $_69, $_911];</p><p>function searchArrayKey($arr, $value)</p><p>{</p><p> foreach($arr as $key => $val)</p><p> {</p><p> if($val == $value) //or $var === $value if you want them to be identical</p><p> return $key;</p><p> if(is_array($val))</p><p> return searchArrayKey($val, $value);</p><p> }</p><p> return false;</p><p>}</p><p>echo searchArrayKey($testArray, "9") . "<br/>"; //output -> 1 | var_dump will return "1"</p><p>?>[/PHP]</p></blockquote><p></p>
[QUOTE="Cxwh, post: 14865, member: 22664"] I am not 100% sure as to what you mean, but I think this is what you're looking for: [CODE] function searchArrayKey(Array, value) { foreach(key, val in Array) { if(value == val) return key; if(isArray(val)) return searchArrayKey(val, value); } return false; } [/CODE] I tested it in php and it worked, but note: If you have if multiple keys returning the same value, only the first one of them will be returned [PHP] <?php $_69 = ["6", "9"]; $_911 = ["9", "1", "1"]; $testArray = ["Bush", "did", $_69, $_911]; function searchArrayKey($arr, $value) { foreach($arr as $key => $val) { if($val == $value) //or $var === $value if you want them to be identical return $key; if(is_array($val)) return searchArrayKey($val, $value); } return false; } echo searchArrayKey($testArray, "9") . "<br/>"; //output -> 1 | var_dump will return "1" ?>[/PHP] [/QUOTE]
Verification
Post reply
Forums
Gaming
Call of Duty Classics
Call of Duty: Black Ops 3
Call of Duty: Black Ops 3 Mods and Scripts
Call of Duty: Black Ops 3 Scripts
Call of Duty: Black Ops 3 Scripts Question
Call of Duty: Black Ops 3 Answered Questions
Getting keys of multidimensional arrays
CabConModding is now on facebook! Check the latest Updates, the Site Status and much more now!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top