如何更深入的多维数组索引? [关闭]

Code:

$a = array();
$a[] = 'a';
$a[] = 'b';
$a[] = 'c';
$a[] = array('e', 'f', array('g', '3'), 'c');
$a[] = 'd';

this works fine:

return $a[3][2][1];

it gives "3". But what if I need to do it with programmatically? A searching or something?

You could use array_walk_recursive to go through each element of the array including array elements, but you might be better off using an OOP approach and making objects of the items (and possibly hilding an array inside them) which would make it much more intuitive.

Example of array_walk_recursive:

<?php
$sweet = array('a' => 'apple', 'b' => 'banana');
$fruits = array('sweet' => $sweet, 'sour' => 'lemon');

function test_print($item, $key)
{
    echo "$key holds $item
";
}

array_walk_recursive($fruits, 'test_print');
?>

output:

a holds apple
b holds banana
sour holds lemon

An example of a class would be as follows:

class mySubArray
{
    public $element1='e';
    public $element2='f';
    public $element3=array();
    public $element4='c';
}

class mySomething
{
    public $var1='a';
    public $var2='b';
    public $var3='c';
    public $var4=array();
    public $var5='d';

    public function __construct()
    {
        $this->var4= new mySubArray();
        $this->var4->element3[0]='g';
        $this->var4->element3[0]='3';
    }
}

$myObject = new mySomething();

Then you can access the properties as follows:

echo $myObject->var3; // Output: c
echo $myObject->var4->element2; // output: f
/**
     * Searches haystack for needle and 
     * returns an array of the key path if 
     * it is found in the (multidimensional) 
     * array, FALSE otherwise.
     *
     * @mixed array_searchRecursive ( mixed needle, 
     * array haystack [, bool strict[, array path]] )
     */

function array_searchRecursive( $needle, $haystack, $strict=false, $path=array() )
{
    if( !is_array($haystack) ) {
        return false;
    }

    foreach( $haystack as $key =&gt; $val ) {
        if( is_array($val) &amp;&amp; $subPath = array_searchRecursive($needle, $val, $strict, $path) ) {
            $path = array_merge($path, array($key), $subPath);
            return $path;
        } elseif( (!$strict &amp;&amp; $val == $needle) || ($strict &amp;&amp; $val === $needle) ) {
            $path[] = $key;
            return $path;
        }
    }
    return false;
}
Using these 2 functions will search multidimensional arrays and return ALL matching paths (items) NOT just a single item.



if(!function_exists('array_search_recursive')){
    function array_search_recursive($needle, $haystack, $key_lookin=""){
    $path = NULL;
    if (!empty($key_lookin) && array_key_exists($key_lookin, $haystack) && $needle === $haystack[$key_lookin]) {
    $path[] = $key_lookin;
    } else {
    foreach($haystack as $key => $val) {
    if (is_scalar($val) && $val === $needle && empty($key_lookin)) {
    $path[] = $key;
    break;
    } elseif (is_array($val) && $path = array_search_recursive($needle, $val, $key_lookin)) {
    array_unshift($path, $key);
    break;
    }
    }
    }
    return $path;
    }
    }



    // Recursive backtracking function for multidimensional array search
    if(!function_exists('search_r')){
    function search_r($value, $array){
    $results = array();

    if(is_array($array)){
    $path = array_search_recursive($value, $array);
    if (is_array($path) && count($path) > 0){
    $results[] = $path;
    unset($array[$path[0]]);
    $results = array_merge($results, search_r($value, $array));
    }else{

    }
    }

    return $results;
    }
    }