dongmangji0950 2014-07-31 09:12
浏览 127
已采纳

循环遍历多维数组并查找匹配值

I'm looking for a way to loop through a multidimensional array and with an if (or simular) find matching values if key master is true.

The array:

    Array
(
    [0] => Array
        (
            [namn] => Vardagsrum
            [IP] => 192.168.1.232
            [rincon] => RINCON_000E58A64F1601400
            [rincon_live] => RINCON_000E58A64F1601400
            [master] => 1
        )

    [1] => Array
        (
            [namn] => Badrum
            [IP] => 192.168.1.137
            [rincon] => RINCON_B8E937580A5801400
            [rincon_live] => RINCON_000E58A64F1601400
            [slave] => 1
        )

    [2] => Array
        (
            [namn] => Kök
            [IP] => 192.168.1.134
            [rincon] => RINCON_000E58F8C9E001400
            [rincon_live] => RINCON_000E58A64F1601400
            [slave] => 1
        )

)

If i got a master => true (in this case vardagsrum) I would like the loop to match all slave => true rincon_live values against the masters rincon value.

I dont know if that makes any sense at all but if I got one or more masters, I want to check the slaves rincon against the masters to see which slaves is connected to which master and after that do something.

Thanks for the help!

  • 写回答

1条回答 默认 最新

  • dpi10335 2014-07-31 09:22
    关注

    You'll probably want to use 2 loops. One to build the structure and another to do something with it. Quick mockup(haven't written PHP in a while):

    <?php
    $result = array();
    foreach ($array as $item) {
        if (!isset($result[$item['rincon']])) {
            $result[$item['rincon']] = array(
                'master' => null,
                'slaves' => array(),
            );
        }
        if (isset($item['master']) && $item['master']) {
            // master
            $result[$item['rincon']]['master'] = $item;
        } else {
            // probably slave, might want to rewrite the else to an elseif
            $result[$item['rincon']]['slaves'][] = $item;
        }
    }
    ?>
    

    Now $result will contain an array with the rincon as a key and master/slaves in them like this:

    <?php
    $result = array(
        'RINCON_000E58A64F1601400' => array(
            'master' => array(
                'namn' => 'foo',
                'IP' => '127.0.0.1',
                ...
            ),
            'slaves' => array(
                array(
                    'namn' => 'foo',
                    'IP' => '127.0.0.1',
                    ...
                ),
                array(
                    'namn' => 'bar',
                    'IP' => '127.0.0.1',
                    ...
                ),
            )
        )
    )
    

    and I assume you know how to loop through that

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP