Reputation: 473
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.
Upvotes: 0
Views: 150
Reputation: 549
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']);
}
?>
Upvotes: 1
Reputation: 7447
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;
}
}
Upvotes: 0