我的阵列:
<?php
$hoppa = array
(
"0" => array
("0","0","0","0","0","0","0","0","0","0"),
"1" => array
("0","0","0","0","0","0","0","0","0","0"),
"2" => array
("1","0","0","1","0","0","0","0","0","0"),
"3" => array
("1","0","0","1","0","1","1","1","1","0"),
"4" => array
("1","1","1","1","0","0","0","0","1","0"),
"5" => array
("1","0","0","1","0","1","1","1","1","0"),
"6" => array
("1","0","0","1","0","1","0","0","1","0"),
"7" => array
("1","0","0","1","0","1","1","1","1","0"),
"8" => array
("0","0","0","0","0","0","0","0","0","0"),
"9" => array
("0","0","0","0","0","0","0","0","0","0")
);
?>
我的阵列输出:
Array
(
[0] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[1] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[2] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[3] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 0
)
[4] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 1
[9] => 0
)
[5] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 0
)
[6] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 0
[7] => 0
[8] => 1
[9] => 0
)
[7] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 0
)
[8] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[9] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
)
结果将给出0(零)
echo $hoppa[1][1];
它将给出1(一)作为结果
echo $hoppa[2][0];
我不希望在结果中显示0(零)值。我希望php脚本只返回1作为结果。应该使用哪些功能?或者你可以给我一个样品吗?
答案 0 :(得分:2)
使用:
foreach($hoppa as $k => $v) {
$hoppa[$k] = array_filter($v);
}
这导致:
php > print_r($hoppa);
Array
(
[0] => Array
(
)
[1] => Array
(
)
[2] => Array
(
[0] => 1
[3] => 1
)
[3] => Array
(
[0] => 1
[3] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
)
[4] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[8] => 1
)
[5] => Array
(
[0] => 1
[3] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
)
[6] => Array
(
[0] => 1
[3] => 1
[5] => 1
[8] => 1
)
[7] => Array
(
[0] => 1
[3] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
)
[8] => Array
(
)
[9] => Array
(
)
)