Returning an array object item

Asked

Viewed 196 times

2

Well I need to go through an array and return a property of the object of that item in question.

My array has this format:

Arquivo mongodb array de links

I tried to solve this with the following function:

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 (isset($newArray[0])) {
        return $newArray[0]['url'];
    } else {
        return null;
    }
}

And to call her I use:

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

Where $links is my image array, $trip is the day I need to pick up, and 'week' is the day of operation I need to pick up.

My mistake happens when for example the item is the [3] of the array, when it is the first everything works more from the position 1 nothing works more.

My return with var_dump is

Object(stdClass)#632 (4) { ["week"]=> string(77) "/horario-de-onibus-010-bela-vista-santa-Ruth-destino-santa-Ruth-em-dias-uteis" ["Saturday"]=> NULL ["Sunday"]=> NULL ["changeDestiny"]=> NULL }

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

The return of all the 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" } } 
  • It is not in the return the problem? if you don’t have to return the object?

  • Doesn’t the array_filter require true or false ? To return that position ? I wonder why javascript works this way perfectly.

  • I updated my reply @Virgilionovic you can see that it returns the objects but note that each object is in a position of the array, all of them have 1 position plus this position is in different places.

  • You need to bring the data in what way?: this I did not understand?

  • @Virgilionovic in case need to bring only url which is an object item.

  • Can this result have duplication? or only one item?

  • 1

    There is no duplication, at the time of creating took care so that there is no such thing as, has only one combo of each week, casing, tripb, Saturday, casing, tripb, Sunday, casing, tripb

Show 2 more comments

1 answer

2

I was able to solve using the current() it returns the first position of an array so it stayed this way:

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 in this way it has solved my problem. The ideal in this case is a junction with array_map and array_filter to stay robust.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.