我需要帮助比较一个将要分配到多维数组中的值,其中有几个值与最后指定的值一起提供。
首先,数组是这样的:
$schedule = array(
0 => array(//day
0 => array(//session 1
0 => array( //room 405
0 => "5" //id from arr2
),
1 => array( //room 406
0 => "7"
),
2 => array( //room 407
0 => ""
),
3 => array( //room 408
0 => ""
)
)
)
$idAndNeighbour = array(
0 => array(
'id'=>5,
'neighbour1' => 4,
'neighbour5' => 22,
),
1 => array(
'id'=>7,
'neighbour1' => 9,
'neighbour5' => 24,
),
2 => array(
'id'=>9,
'neighbour1' => 7,
'neighbour5' => 23,
),
3 => array(
'id'=>14,
'neighbour1' => 8,
'neighbour5' => 26,
)
)
要分配给$ schedule的下一个id是9,所以我需要获取最后分配的id,查看$ idAndNeighbour中哪个id与最后分配的id匹配,并将id 9与最后分配的id的邻居进行比较,如果9匹配任何最后分配的id的邻居然后跳过9然后下一个要分配的id是14。
我该如何解决这个问题?
注意:我不应该在$ idAndNeighbour中进行比较,因为它会不断跳过id 9和其他与邻居值匹配的id
答案 0 :(得分:0)
如果您询问如何查找叶子节点中是否有9,则需要array_walk_recursive。
以下是您的数据示例:
$schedule = array(
0 => array(//day
0 => array(//session 1
0 => array( //room 405
0 => "5" //id from arr2
),
1 => array( //room 406
0 => "7"
),
2 => array( //room 407
0 => ""
),
3 => array( //room 408
0 => ""
)
)
)
);
$allids = array();
array_walk_recursive($schedule, function ($value, $key) use (&$allids) {
if ($value) $allids[] = $value;
});
var_export($allids);
if (array_search(9, $allids) === false) {
echo "\nIt's not there.\n";
} else echo "\nIt's there.\n";
这会产生:
array (
0 => '5',
1 => '7',
)
It's not there.