dpztth71739 2018-06-12 08:22
浏览 31
已采纳

将多维数组与PHP中的另一个数组进行比较,并从多维数组中获取值

I have an multidimensional array from an XML file which I want to compare with a normal array. I need to compare the [tag] name in the multidimensional array with the name in the other array and get value from the multidimensional array that belongs to the tag.

Array
(
    [0] => Array
        (
            [tag] => DOCUMENT
            [type] => open
            [level] => 1
        )

    [1] => Array
        (
            [tag] => SENDERID
            [type] => complete
            [level] => 2
            [value] => TEST
        )

    [2] => Array
        (
            [tag] => SENDERSHORTNAME
            [type] => complete
            [level] => 2
        )

    [3] => Array
        (
            [tag] => RECIPIENTID
            [type] => complete
            [level] => 2
            [value] => VGLEE
        )
)

Second array which I need to compare it with the multidimensional array:

$compare_array = array('DOCUMENT', 'SENDERID', 'SENDERSHORTNAME', 'RECIPIENTID');

Now I want to check if the key from $compare_array is matched in the multidimensional array. If so, I want to grab the value from the multidimensional array and make a variable with the name from the compare_array and append the value to the variable.

I made a for loop:

for($i = 0; $i < $count; $i++){
    if($values[$i]['tag'] == 'SENDERID'){
        $SENDER = $values[$i]['value'];
    }
    if($values[$i]['tag'] == 'RECIPIENTID'){
        $RECIPIENTID = $values[$i]['value'];
    }
    if($values[$i]['tag'] == 'IREF'){
        $IREF = $values[$i]['value'];
    }
    if($values[$i]['tag'] == 'DOCUMENTNUMBER'){
        $DOCUMENTNUMBER = $values[$i]['value'];
    }
}
  • 写回答

3条回答 默认 最新

  • doubiao9775 2018-06-12 08:46
    关注

    For your example data, you could use array_reduce and use in_array to check if the tag is present in $compare_array

    If you must make a variable with the name from the $compare_array and append the value to the variable you might use extract with a flag that suits your expectations.

    The value is not present in all the example data so you might also check if that exists.

    $compare_array = array('DOCUMENT', 'SENDERID', 'SENDERSHORTNAME', 'RECIPIENTID');
    $result = array_reduce($arrays, function($carry, $item) use ($compare_array) {
        if(isset($item["value"]) && in_array($item["tag"], $compare_array, true)) {
            $carry[$item["tag"]] = $item["value"];
        }
        return $carry;
    });
    extract($result, EXTR_OVERWRITE);
    
    echo $SENDERID;
    echo $RECIPIENTID;
    

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧