2

I have an array of objects as bellow:

$ARR =[
{"id":55,"timetable_id":1,"day":"Sat","checkin":"07:00:00","checkout":"11:00:00","isActive":1,"created_at":"2017-12-28 19:40:23","updated_at":"2017-12-28 19:40:23"},
{"id":54,"timetable_id":1,"day":"Fri","checkin":"07:00:00","checkout":"11:00:00","isActive":1,"created_at":"2017-12-28 19:40:23","updated_at":"2017-12-28 19:40:23"},
{"id":53,"timetable_id":1,"day":"Thu","checkin":"07:00:00","checkout":"11:00:00","isActive":1,"created_at":"2017-12-28 19:40:23","updated_at":"2017-12-28 19:40:23"},
{"id":52,"timetable_id":1,"day":"Wed","checkin":"07:00:00","checkout":"11:00:00","isActive":1,"created_at":"2017-12-28 19:40:23","updated_at":"2017-12-28 19:40:23"},
{"id":51,"timetable_id":1,"day":"Tue","checkin":"14:00:00","checkout":"17:00:00","isActive":1,"created_at":"2017-12-28 19:40:23","updated_at":"2017-12-28 19:40:23"},
{"id":50,"timetable_id":1,"day":"Tue","checkin":"07:00:00","checkout":"11:00:00","isActive":1,"created_at":"2017-12-28 19:40:23","updated_at":"2017-12-28 19:40:23"},
{"id":49,"timetable_id":1,"day":"Mon","checkin":"14:00:00","checkout":"16:00:00","isActive":1,"created_at":"2017-12-28 19:40:23","updated_at":"2017-12-28 19:40:23"},
{"id":48,"timetable_id":1,"day":"Mon","checkin":"07:00:00","checkout":"11:00:00","isActive":1,"created_at":"2017-12-28 19:40:23","updated_at":"2017-12-28 19:40:23"}
];

And I want to get any array items by the specific key value. For example I want to get the items of the key day that equal to Tue, so my array result will look like:

$RESULTS=[
    {"id":51,"timetable_id":1,"day":"Tue","checkin":"14:00:00","checkout":"17:00:00","isActive":1,"created_at":"2017-12-28 19:40:23","updated_at":"2017-12-28 19:40:23"},
    {"id":50,"timetable_id":1,"day":"Tue","checkin":"07:00:00","checkout":"11:00:00","isActive":1,"created_at":"2017-12-28 19:40:23","updated_at":"2017-12-28 19:40:23"},
    ];

What I have tried:

$RESULTS = [];
foreach($ARR as $item){
   if($item->day == 'Tue'){
     $RESULTS[] = $item;
   }
}

Technically, it is working as expected, but it needs to write many line of code. Is there any other PHP functions that support this operation? I tried array_slice and array_search but no luck.

4
  • You call that "many lines of code"? Commented Dec 29, 2017 at 3:34
  • @Phil It's many lines of code if the OP has to repeat it over and over for different conditions. Commented Dec 29, 2017 at 3:50
  • @bishop I mean, you could encapsulate it into a function (which I figured OP already knows). You could also make it one line if you really wanted ~ foreach($ARR as $i) if($item->day==='Tue') $RESULTS[]=$item; Commented Dec 29, 2017 at 3:54
  • 1
    Interestingly, I assumed the OP hadn't considered a function: worrying over line count seemed the Y of the X-Y problem: "I have to do this many times and I don't want to repeat a lot of code: how can I do this in one line". Commented Dec 29, 2017 at 3:57

2 Answers 2

3

There's absolutely nothing wrong with your implementation. The only other alternative I can think of is array_filter

$RESULTS = array_filter($ARR, function($item) {
    return $item->day === 'Tue';
});

There's a good chance your original approach is more performant than this though, especially across such a small array.

Note also that keys are preserved with array_filter so you'd end up with indexes 4 and 5. You can use array_values to re-index the array

$RESULTS = array_values($RESULTS);
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, for wishing PHP implemented short closures.
@bishop yup. Arrow functions / lambdas would sure be nice. Would also be nice if the array_* functions could work on the collection in parallel. For large data sets, we might actually see some performance benefit.
2

Several ways to do this, but: if you need to do the same kind of thing many times, that calls for a function:

function pluck(array $ARR, $key, $value) {
    $RESULTS = [];
    foreach ($ARR as $item){
        if ($item->$key == $value) {
            $RESULTS[] = $item;
        }
    }
    return $RESULTS;
}

Which you may then call as:

$RESULTS = pluck($ARR, 'day', 'Tue');
$RESULTS = pluck($ARR, 'isActive', 1);
// etc...

As many times as needed, without all the duplicated code. That said, of course, you may not want a strict == comparison, in which case you might opt for a function that's more flexible. For example, $value might be a regular expression, and your function could be modified so:

        if (preg_match($value, $item->$key) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.