递归遍历PHP多维数组以进行比较

时间:2016-02-23 12:27:15

标签: php arrays recursion multidimensional-array

我有一个从具有未知数量的元素和子级别的JSON结构转换而来的大型数组。

类似的东西:

$marr = array
(
    "Order" => array
    (
        "Details" => array
        (
            "Document" => array
            (
                "Number" => "1585636772",
                "Date" => "2014-12-31"
            ),
            "Delivery" => array
            (
                "Date" => "2015-01-02",
                "TrackingCode" => "5703",
                "Name" => "Example Name",
                "Address" => "Example Address"
            )
        )
    )
);

另一方面,我有一系列我需要比较的项目,并找出它们是否在上面的数组中。这个"索引器"将始终反映上面相同的结构(它是在比较步骤之前生成的),因为我认为这将有助于我确保以更简单的方式进行适当的比较。

类似的东西:

$indexer = array
(
    "Order" => array
    (
        "Details" => array
        (
            "Document" => array
            (
                "Date" => "variable_name_1"
            ),
            "Delivery" => array
            (
                "Date" => "variable_name_2"
            )
        )
    )
);

我不完全确定如何最好地比较它们。我查看了 array_walk_recursive(),它只返回叶子值,我试图在一个基本的递归函数上编写我自己的尝试,该函数将执行 foreach()然后尝试做类似的事情:

if( isset($marr["Order"]["Details"]["Document"]["Date"]) )
{
    $store[ $indexer["Order"]["Details"]["Document"]["Date"] ] = $marr["Order"]["Details"]["Document"]["Date"];
}

所以最后我会有一个基本数组,它存储在 $ indexer 上列出的别名 $ marr 下的所有值。像这样:

$store["variable_name_1"] = "2014-12-31";
$store["variable_name_2"] = "2015-01-02";

现在已经两天头疼了,我似乎无法找到解决这个问题的最佳方法。我试图通过$ indexer到达它的结尾,获得"变量名称",然后与$ marr进行比较以存储其数据,但我似乎总是失去$的父节点尝试以递归方式执行此操作时的索引器。我会很感激任何建议。

2 个答案:

答案 0 :(得分:1)

你可以使用这个递归函数:

function storeFromIndex($marr, $indexer) {
    if (!is_array($indexer)) {
        return array($indexer => $marr);
    }
    $store = [];
    foreach($indexer as $key => $subindexer) {
        $store = array_merge($store, storeFromIndex($marr[$key], $subindexer));
    }
    return $store;
}

然后像这样称呼它:

$store = storeFromIndex($marr, $indexer);

根据给出的示例数据, $ store 将是:

array (
  'variable_name_1' => '2014-12-31',
  'variable_name_2' => '2015-01-02',
)

答案 1 :(得分:1)

这里我建议不要维护索引器,可以使用迭代器并使用关联键创建新数组。

例如,请看下面的解决方案:

$array = array
(
    "Order" => array
    (
        "Details" => array
        (
            "Document" => array
            (
                "Number" => "1585636772",
                "Date" => "2014-12-31"
            ),
            "Delivery" => array
            (
                "Date" => "2015-01-02",
                "TrackingCode" => "5703",
                "Name" => "Example Name",
                "Address" => "Example Address"
            )
        )
    )
);


$new_array = array();
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));

foreach ($iterator as $key => $value) {
    $keys = array();
    $keys[] = $key;
    for ($i = $iterator->getDepth() - 1; $i >= 0; $i--) {
        $keys[] = $iterator->getSubIterator($i)->key();
    }
    $key_paths = array_reverse($keys);
    $new_array[implode('_', $key_paths)] = $value;
}

print_r($array);
print_r($new_array);

<强>输出:

Array
(
    [Order] => Array
        (
            [Details] => Array
                (
                    [Document] => Array
                        (
                            [Number] => 1585636772
                            [Date] => 2014-12-31
                        )

                    [Delivery] => Array
                        (
                            [Date] => 2015-01-02
                            [TrackingCode] => 5703
                            [Name] => Example Name
                            [Address] => Example Address
                        )

                )

        )

)
Array
(
    [Order_Details_Document_Number] => 1585636772
    [Order_Details_Document_Date] => 2014-12-31
    [Order_Details_Delivery_Date] => 2015-01-02
    [Order_Details_Delivery_TrackingCode] => 5703
    [Order_Details_Delivery_Name] => Example Name
    [Order_Details_Delivery_Address] => Example Address
)