index.php
<?php
$a = array(
1 => array(1, 0),
2 => array(2, 1),
3 => array(3, 1),
4 => array(4, 1),
5 => array(5, 0),
6 => array(6, 0),
7 => array(7, 0),
8 => array(8, 5),
9 => array(9, 5),
);
$b = array();
foreach ($a as $key => $value1) {
//value1の2番目が0だったら処理を通す
if ($value1[1] == 0) {
$b[] = $value1;
//valueの1の1番目とvalue2の2番目が一緒だったら処理を通す
foreach ($a as $key2 => $value2) {
if ($value1[0] == $value2[1]) {
$b[] = $value2;
}
}
}
}
$a = $b;
print_r($a);
結果
php.php
Array
(
[0] => Array
(
[0] => 1
[1] => 0
)
[1] => Array
(
[0] => 2
[1] => 1
)
[2] => Array
(
[0] => 3
[1] => 1
)
[3] => Array
(
[0] => 4
[1] => 1
)
[4] => Array
(
[0] => 5
[1] => 0
)
[5] => Array
(
[0] => 8
[1] => 5
)
[6] => Array
(
[0] => 9
[1] => 5
)
[7] => Array
(
[0] => 6
[1] => 0
)
[8] => Array
(
[0] => 7
[1] => 0
)
)