I have 30 elemements from an array
array:30 [▼
0 => array:8 [▼
"agent_id" => "958e9264-8f7d-11ea-a18a-2e2ea6f9a94a"
"folder_name" => "2020-10-26-145338"
"special_result" => false
"is_special_exec" => false
"logs_path" => "/var/log/apple/f1132f00-b8fa-11e9-85e0-0050568d2010/ue3/2020-10-26-145338"
"logs_url" => "http://api:31004/apple/controller/v1/account/f1132f00-b8fa-11e9-85e0-0050568d2010/session/ue3/logs?path=%2Fvar%2Flog%2Fapple%2Ff1132f00-b8fa-11e9-85e0-0 ▶"
"result" => "PASSED"
"session_name" => "ue3"
]
...
I want to filter out only "is_special_exec" => true
I'm trying to avoid doing this
$specialLogs = [];
foreach ($sessionLogs as $log) {
if($log['special_result'] == true){
array_push($specialLogs, $log);
}
}
What is the best way to do that without effect the performance ?
reject()
orfilter()
methods: laravel.com/docs/8.x/collections#method-filter and laravel.com/docs/8.x/collections#method-reject – Tim Lewis Oct 26 '20 at 19:13What is the best way to do that without effect the performance
Foreach is good enough and it can't get any more performant. – nice_dev Oct 26 '20 at 19:27Call to a member function filter() on array
– cyber8200 Oct 26 '20 at 23:44collect($array)
, then usefilter()
. My comment is more-so if you already have a Collection, since most functions in Laravel return Collections instead of arrays. If you need to convert, thenarray_filter
is just as performant and you can go with that. – Tim Lewis Oct 27 '20 at 1:47