如何更深入的多维数组索引?

时间:2012-08-19 13:45:23

标签: php arrays

代码:

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

这很好用:

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

它给出“3”。但是,如果我需要以编程方式执行该操作呢?搜索还是什么?

3 个答案:

答案 0 :(得分:1)

你可以使用array_walk_recursive遍历数组的每个元素,包括数组元素,但你可能最好使用OOP方法并制作项目的对象(并可能在其中构建一个数组)让它更多更直观。

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\n";
}

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

输出:

a holds apple
b holds banana
sour holds lemon

一个类的例子如下:

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();

然后您可以按如下方式访问属性:

echo $myObject->var3; // Output: c
echo $myObject->var4->element2; // output: f

答案 1 :(得分:1)

/**
     * 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;
}

答案 2 :(得分:1)

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;
    }
    }