I'm actually working on a solution for another thread, however I don't know how to do this, or if it is even possible. I want to display the array a variable or item is in, like this: $stuff = array("this", "that", "theotherthing"); $answers = array("no", "yes", "maybe"); PHP: How would I input $theotherthing into a script that would tell me that it's part of the array $stuff? If I input "maybe" into the script it should come back with $answers. Thanks! -Peter EDIT:: I don't need to "display" the "parent array," I just need the script to identify it so I can do with it whatever I please.
Something along the lines of this? <?php $item = "yes"; $master_arr = array( "stuff" => array("this", "that", "theotherthing"), "answers" => array("no", "yes", "maybe") ); foreach ($master_arr as $test){ if ( in_array($item, $test) ){ $id_array = key($master_arr); } } ?> PHP: Isn't quite right, but I think its close... nico?
I was going to suggest something like this, as it seemed to be your initial question: function array_multisearch() { $args = func_get_args(); $query = $args[0]; $matches = array(); unset($args[0]); foreach ($args AS $key => $array) { if (!is_array($array)) { trigger_error(__FUNCTION__ . '() expects parameter ' . ++$key . ' to be an array.'); } else if (in_array($query, $array)) { array_push($matches, $array); } } return $matches; } PHP: This would return an array of the arrays where the item was found in. $stuff = array("this", "that", "theotherthing"); $answers = array("no", "yes", "maybe"); $item = "yes"; // You can add as many arrays as you want. $results = array_multisearch($item, $stuff, $answers/*, array, array, array,...*/); echo '<pre>' . print_r($results, true) . '</pre>'; PHP: Outputs: Array ( [0] => Array ( [0] => no [1] => yes [2] => maybe ) ) Code (markup): With a slight modification you could get the first array only where the item was found in.
Well, when I tested my code it returned the name of the first array regardless of the input, if I recall correctly. Does it work when you guys test it?