dpp66953 2016-02-23 12:27
浏览 139
已采纳

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

I have a large array converted from a JSON structure with an unknown number of elements and sublevels.

Something like:

$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"
            )
        )
    )
);

And on the other hand, I have an array of items I need to compare and find out if they are in the array above. This "indexer" will always mirror the same structure above (it's generated before the comparison step) because I thought that would help me ensure a proper comparison in an easier way.

Something like:

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

I'm not entirely sure how best to compare these. I have looked into array_walk_recursive() which only returns the leaf values and I have tried to write my own attempts at a basic recursive function that would perform a foreach() which would then try to do something like:

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

So that at the end I would have a basic array that stored all values found on $marr under an alias that was listed on $indexer. Like this:

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

This has been a headache for two days now and I can't seem to figure out the best way to go through this. I'm trying to walk through $indexer to reach its ending, obtain the "variable name", and then compare with $marr to store its data, but I always seem to lose the parent nodes of $indexer while trying to do this recursively. I would appreciate any advice at all.

  • 写回答

2条回答 默认 最新

  • dor65412 2016-02-23 12:39
    关注

    You could use this recursive function:

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

    And then call it like this:

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

    With the example data given, $store will be:

    array (
      'variable_name_1' => '2014-12-31',
      'variable_name_2' => '2015-01-02',
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果