dougong1031 2014-06-14 07:13
浏览 31

如何获取数组中下一个相同键的值

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条回答 默认 最新

  • dongque5529 2014-06-14 07:22
    关注

    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; 
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化