0

I have an array that looks like this when printed:

Array (
    [0] => stdClass Object (
        [sku] => exp_pokemonketchup_001
        [_id] =>   539be6740478ca09233ac71e
        [__v] => 0
        [type] => 3
        [status] => 1
        [_create_date] => 2014-06-14T06:06:44.365Z
        )
    [1] => stdClass Object (
        [sku] => exp_onepiecegobble_001
        [_id] => 539be6710478ca09233ac71c
        [__v] => 0
        [type] => 3
        [status] => 1
        [_create_date] => 2014-06-14T06:06:41.110Z
    )
    [2] => stdClass Object (
        [sku] => exp_sailormoongiggle_001
        [_id] => 539be66d0478ca09233ac717
        [__v] => 0
        [type] => 3
        [status] => 1
        [_create_date] => 2014-06-14T06:06:37.633Z
    )
)

I'm trying to get the sku and corresponding value for the subsequent sku during a foreach loop. In other words, I’m looping through the array, echoing the first sku, which is exp_pokemonketchup_001.

Now I also need to echo out the next sku which is exp_onepiecegobble_001 but before the loop continues to that part of the array.

2 Answers 2

1

The above can be sorted using an inbuilt PHP function get_object_vars(); In the given array i have tried recreating the array using just one field. Please see the below code. <?php $obj = array((object) array('sku' => 'exp_pokemonketchup_001'), (object) array('sku' => 'exp_onepiecegobble_001') ); foreach($obj as $k => $v){ $objDetails =get_object_vars($v); print_r($objDetails['sku']); } ?>

Sign up to request clarification or add additional context in comments.

1 Comment

get_object_vars function gets it converted to array in php.get_object_vars()
0

Try this:

Assuming $array is your current array of objects

$array_size = count($array); 

foreach ($array as $key=>$val) {

    // echo current sku
    echo $val->sku;     

    // If not the last object, echo next sku       
    if ($key < $array_size-1) {
        echo $array[$key+1]->sku; 
    }

}

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.