I need to remove the array index which contains the key ‘parentType’ value is COMPONENT. I am having 4 indexed array and in that I have 2 index with key [parentType] => SITE So I need only that indexed array. I need to filter only if the [parentType] => SITE then that array I need it.
I am having the input array like this:-
$arr = Array
(
[0] => Array
(
[parentType] => SITE
[name] => PV Meter
)
[1] => Array
(
[parentType] => COMPONENT
[name] => Inverter-01
)
[3] => Array
(
[parentType] => SITE
[name] => Inverter-02
)
[4] => Array
(
[parentType] => COMPONENT
[name] => Inverter-02
)
);
I have tried with array_diff and unset($array[‘parentType’]) and
foreach($components_id as $value=>$val)
{
echo "<br> Array[".$value."]=".$val;
}
but I did not get the exact solution.
I need a output like this:-
$arr = Array
(
[0] => Array
(
[parentType] => SITE
[name] => PV Meter
)
[1] => Array
(
[parentType] => SITE
[name] => Inverter-02
)
);
2
Answers
You can use array_splice for remove element by index but it’s not recommend because we need to deal with index changing after remove one element.
I suggest to filter result to new array is better.
My method:
You can use the array_filter method to create a new filtered array: