Returning object item from an array

2

Well, I need to loop through an array and return an object's property from that item.

My array has this format:

Itriedtosolvethiswiththefollowingfunction:

publicfunctionreturnObjectByDayAndTrip($links,$trip,$dayOperation){$newArray=array_filter($links,function($obj)use($trip,$dayOperation){if($obj['trip']==$trip&&$obj['dayOperation']===$dayOperation){returntrue;}else{returnfalse;}});if(isset($newArray[0])){return$newArray[0]['url'];}else{returnnull;}}

Andtocallheruse:

$link->week=$this->returnObjectByDayAndTrip($links,$trip,'week');

Where$linksismyimagearray,$tripisthedayIneedtoget,and'week'isthedayofoperationIneedtoget.

Myerrorhappenswhenforexampletheitemisthe[3]ofthearray,whenitisthefirsteverythingworksmorefromtheposition 1 nothing works anymore.

My return with var_dump is

  

object (stdClass) # 632 (4) {["week"] = > string (77) "/ schedule-of-bus-010-bela-vista-santa-ruth-destination-santa-ruth-in-us-days" ["saturday"] = > NULL ["sunday"] = > NULL ["changeDestiny"] = > NULL}

I need to return the url of the object based on the particularity passed to function. In my array, you will necessarily have only 1 items or none that will satisfy the information you entered, ie always have a result or none.

The return of all times I've called is like this:

array(1) { [0]=> array(4) { ["_id"]=> object(MongoDB\BSON\ObjectId)#477 (1) { ["oid"]=> string(24) "5b3f74ad6ae83d00223504e8" } ["url"]=> string(77) "/horario-de-onibus-010-bela-vista-santa-ruth-destino-santa-ruth-em-dias-uteis" ["dayOperation"]=> string(4) "week" ["trip"]=> string(5) "tripA" } } 

array(1) { [2]=> array(4) { ["_id"]=> object(MongoDB\BSON\ObjectId)#479 (1) { ["oid"]=> string(24) "5b3f74ad6ae83d00223504e6" } ["url"]=> string(73) "/horario-de-onibus-010-bela-vista-santa-ruth-destino-santa-ruth-no-sabado" ["dayOperation"]=> string(8) "saturday" ["trip"]=> string(5) "tripA" } } 

array(1) { [4]=> array(4) { ["_id"]=> object(MongoDB\BSON\ObjectId)#481 (1) { ["oid"]=> string(24) "5b3f74ad6ae83d00223504e4" } ["url"]=> string(74) "/horario-de-onibus-010-bela-vista-santa-ruth-destino-santa-ruth-no-domingo" ["dayOperation"]=> string(6) "sunday" ["trip"]=> string(5) "tripA" } } 

array(1) { [1]=> array(4) { ["_id"]=> object(MongoDB\BSON\ObjectId)#478 (1) { ["oid"]=> string(24) "5b3f74ad6ae83d00223504e7" } ["url"]=> string(77) "/horario-de-onibus-010-bela-vista-santa-ruth-destino-bela-vista-em-dias-uteis" ["dayOperation"]=> string(4) "week" ["trip"]=> string(5) "tripB" } } 
    
asked by anonymous 09.07.2018 / 15:08

1 answer

2

I was able to resolve using current() it returns the first position of an array so it looks like this:

public function returnObjectByDayAndTrip($links, $trip, $dayOperation)
{
    $newArray = array_filter($links, function ($obj) use ($trip, $dayOperation) {
        if ($obj['trip'] == $trip && $obj['dayOperation'] === $dayOperation) {
            return true;
        } else {
            return false;
        }
    });


    if (count($newArray) >= 1) {
        return current($newArray)['url'];
    } else {
        return null;
    }
}

There is probably a better way to do it, and I even accept this new methods but this way it solved my problem. The ideal here is a join with array_map and array_filter to be robust.

    
09.07.2018 / 15:47