Display Parent Array?

Discussion in 'PHP' started by dp-user-1, May 20, 2007.

  1. #1
    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.
     
    dp-user-1, May 20, 2007 IP
  2. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #2
    ok, array_search can be also a solution for your problem

    array_search("maybe", $answers)
     
    gibex, May 20, 2007 IP
  3. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #3
    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? :)
     
    dp-user-1, May 20, 2007 IP
  4. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #4
    yeah!
    NinjaNoodles' code is working :)
     
    coderbari, May 21, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    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.
     
    nico_swd, May 21, 2007 IP
  6. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #6
    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?
     
    dp-user-1, May 21, 2007 IP