0

Is there a clean elegant way to check if an array object value that has specific value "positionID = string(29)" and if its "Action" starts with for eg "(string) 1" and ends with "(string) 0".

The unlooped array looks similar to this

array (
   [0] => Object1 {
            ['PositionID'] => (string) 29
            ['Action'] => (string) 1
          }
   [1] => Object22 {
            ['PositionID'] => (string) 30
            ['Action'] => (string) 0
          }
   [2] => Object23 {
            ['PositionID'] => (string) 29
            ['Action'] => (string) 1
          }

   [3] => Object5 {
            ['PositionID'] => (string) 31
            ['Action'] => (string) 0
          }
   [2] => Object23 {
            ['PositionID'] => (string) 29
            ['Action'] => (string) 0
          }
);

I would like to find out within that array that the last "Action" occurence of "positionID= 29" is 0 or something else. At the moment I am grouping the positionId and storing them into a third array and looping it which feels like a dirty solution to me.

1
  • 1
    Do you mean the last element - or is it the last element for a specific positionID( so for positionID = 31, this is element 3)? Commented Jun 7, 2018 at 10:20

3 Answers 3

1

Take a look at end(). First item should be obvious.

$first = $array[0];
if ($first->positionId === '29' && $first->Action === '1') {
    $last = end($array);
    if ($last->positionId === '29' && $last->Action === '0' {
        // Stuff
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
<?php

$items = 
[
    [
        'position' => '17',
        'action' => '1'
    ],
    [
        'position' => '47',
        'action' => '0'
    ],
    [
        'position' => '23',
        'action' => '0'
    ]
];

foreach ($items as $k => $item)
    $items[$k] = (object) $item;

var_dump($items);

if(array_column($items, 'action', 'position')[23] === '0')
    echo "Action is '0' for the object with position 23";

Output:

array(3) {
    [0]=>
    object(stdClass)#1 (2) {
      ["position"]=>
      string(2) "17"
      ["action"]=>
      string(1) "1"
    }
    [1]=>
    object(stdClass)#2 (2) {
      ["position"]=>
      string(2) "47"
      ["action"]=>
      string(1) "0"
    }
    [2]=>
    object(stdClass)#3 (2) {
      ["position"]=>
      string(2) "23"
      ["action"]=>
      string(1) "0"
    }
  }
  Action is '0' for the object with position 23

Comments

0

You could use a combination of array_column and array_filter and you end to return the last item from the array $result:

$result = array_column(array_filter($arrays, function ($x) {
    return $x->PositionID === '29';
}), 'Action');
$lastValue = end($result);
var_dump($lastValue);

That would give you:

string(1) "0"

Then you might use it like:

if ($lastValue === "0") {
    // ...
}

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.