i have some array
Array
(
[data] => Array
(
[0] => Array
(
[0] => 1
[1] => MOUSE
[2] => 0
[3] => 1
)
[1] => Array
(
[0] => 2
[1] => LAPTOP
[2] => 0
[3] => 0
)
[3] => Array
(
[0] => 4
[1] => PHONE
[2] => 0
[3] => 0
)
)
)
i need to remove "0" values from the array so the result should be.
Array
(
[data] => Array
(
[0] => Array
(
[0] => 1
[1] => MOUSE
[2] => 0
[3] => 1
)
)
)
how to remove it with a dynamic code? because it will execute large of array. an i stuck in it for days
This filterArray
function should work.
It just cheks the arrays inside the main array you submit (which in this case is $array["data"]), then checks the third position in each child array, and returns it if the value is not zero.
$array = array(
"data" => array(
array(
"0" => 1,
"1" => "MOUSE",
"2" => 0,
"3" => 1
),
array(
"0" => 2,
"1" => "LAPTOP",
"2" => 0,
"3" => 0
),
array(
"0" => 4,
"1" => "PHONE",
"2" => 0,
"3" => 0
)
)
);
function filterArray($elem) {
if (is_array($elem)) {
return (bool) $elem[3];
}
return false;
}
$array["data"] = array_filter($array["data"], "filterArray");
var_dump($array);
Here the output for your array:
array(1) {
["data"] => array(1) {
[0] => array(4) {
[0] => int(1)
[1] => string(5) "MOUSE"
[2] => int(0)
[3] => int(1)
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.