Answer the question
In order to leave comments, you need to log in
How to delete an array within an array by a specific value?
I have an array like this:
$results = [
['order_id' => 'first', 'id'=> 1],
['order_id' => 'second', 'id'=> 4],
['order_id' => 'third', 'id'=> 7]
];
//Прогоняю в цикле с условием, что будет удален подмассив в котором 'id' равен 4.
foreach ($results as $item){
foreach ($item as $value){
}
if ($item['id'] == 4)
unset($item);
}
print_r ($results);
['order_id' => 'second', 'id'=> 4]
he had to leave.
Answer the question
In order to leave comments, you need to log in
a normal answer to this question
, otherwise right now they will come running with links, or out - filters with a subvert.
foreach ($results as $key => $item) {
if ($item['id'] === 4) {
unset($results[$key]);
}
}
Using array_filter:
<?php
$results = [
['order_id' => 'first', 'id'=>1],
['order_id' => 'second', 'id'=>4],
['order_id' => 'third', 'id'=>7]
];
$filtered = array_filter(
$results,
function($el) {
return $el['id'] != 4;
}
);
var_export($filtered);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question