Reputation: 7989
I saw many similar questions, but they seemed to be slightly different in that if any value was x, remove that element. If I've missed the Q&A, apologies.
I have an array of the form:
0 => ("A Name", 22, 33, 44, 55)
1 => ("Another", 2, 3, 4, 5)
etc
I want to essentially perform "If $array[*][2] = 33, remove that element", which in this case would remove A Name's record.
I'm relatively confident unset() & a foreach loop are required, but I'm honestly not sure how. I'm not sure how to use the foreach for a specific inner array value, but all outer array values.
EDIT: Current progress is essentially blundering around cluelessly with this:
foreach($tempArray{$i} as $key => $value) {
if ($value[2] == 33) { unset($array[$key]); }
}
Upvotes: 0
Views: 59
Reputation: 7552
You could use a different approach, taking use of php functions such array_filter
and array_column
(this one PHP 5.5 +);
$array = [
["A Name", 22, 33, 44, 55],
["Another", 33, 23, 14, 15]
];
print_r(
$array[
key(
array_filter(
array_column($array,2), function($a){
if ($a == 33)
return false;
return true;
})
)
]
);
Upvotes: 2
Reputation: 78994
Well, you sort of tried, so try this:
foreach($array as $key => $val) {
if($val[2] == 33) {
unset($array[$key]);
}
}
Not sure what the $tempArray{$i}
was. foreach
will iterate through the array where you can use the key and value.
Upvotes: 5