Reputation: 5308
I have an array like below.
$a = array(
array("id" => 1, "name" => "njx", "count" => 0),
array("id" => 2, "name" => "peter", "count" => 4),
array("id" => 3, "name" => "jeffry", "count" => 2),
array("id" => 4, "name" => "adam", "count" => 6)
);
and applied filter like this.
$fa = array_filter($a, function ($item) {
return ($item["count"] > 0);
});
then i applied usort
on variable $fa
. After that i loop through $fa
and assigned some values, but they are not get reflected in variable $a
.
something like below,
usort($fa, 'comp');
foreach ($fa as &$t) {
$t["score"] = 120;
}
var_dump($a); //this doesn't contain "score" field.
So my questions is how to get filtered array with original array reference?
Upvotes: 2
Views: 3189
Reputation: 74
array_filter returns a new array and not a reference, thats why any changes applied to $fa wont be reflected in $a.
Instead of using array_filter you could use a foreach loop like this:
foreach($a as &$t) {
if($t['count'] > 0) {
$t['score'] = 120;
}
}
And then sort using usort.
Upvotes: 4