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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?