在JSON数组上的foreach循环

时间:2017-05-29 23:23:21

标签: php arrays json foreach

感谢您抽出宝贵时间来查看我的问题。

希望这是有道理的......

我希望从JSON数组中提取数据。

在下面的数组中,我想循环通过Response> data> buckets> Equippable> 1> items> 0>节点的节点选项卡来查看它是否处于活动状态,以及它是否处于什么状态。

问题是'节点'部分可以是不同的长度,这个例子有12个结果但是接下来我只有8个等等

对于固定的结果,我会使用此

foreach ($json['Response']['data']['buckets']['Equippable']['1']['items']['0']['nodes'] as $Talentnode) {   
$nodeActive[] = $Talentnode['0']['isActivated'];
$nodeState[] = $Talentnode['0']['state'];
}

当我不知道会有多少结果时,我将如何进行foreach循环?

  [Response] => Array

        [data] => Array

                [buckets] => Array

                        [Invisible] => Array
                        [Item] => Array
                        [Equippable] => Array

                                [0] => Array
                                [1] => Array

                                        [items] => Array

                                                [0] => Array

                                                        [itemHash] => 320170738
                                                        [bindStatus] => 0
                                                        [isEquipped] => 1
                                                        [itemInstanceId] => 6917529121839082505
                                                        [itemLevel] => 50
                                                        [stackSize] => 1
                                                        [qualityLevel] => 100
                                                        [stats] => Array
                                                        [primaryStat] => Array
                                                        [canEquip] => 1
                                                        [equipRequiredLevel] => 40
                                                        [unlockFlagHashRequiredToEquip] => 2166136261
                                                        [cannotEquipReason] => 0
                                                        [damageType] => 1
                                                        [damageTypeHash] => 3373582085
                                                        [damageTypeNodeIndex] => 5
                                                        [damageTypeStepIndex] => 0
                                                        [progression] => Array
                                                        [talentGridHash] => 2047220462
                                                        [nodes] => Array

                                                                [0] => Array

                                                                        [isActivated] => 1
                                                                        [stepIndex] => 0
                                                                        [state] => 10
                                                                        [hidden] => 
                                                                        [nodeHash] => 0

                                                                [1] => Array

                                                                        [isActivated] => 
                                                                        [stepIndex] => 0
                                                                        [state] => 9
                                                                        [hidden] => 
                                                                        [nodeHash] => 1

                                                                [2] => Array

                                                                        [isActivated] => 1
                                                                        [stepIndex] => 0
                                                                        [state] => 10
                                                                        [hidden] => 
                                                                        [nodeHash] => 2

                                                                [3] => Array

                                                                        [isActivated] => 1
                                                                        [stepIndex] => 0
                                                                        [state] => 10
                                                                        [hidden] => 
                                                                        [nodeHash] => 3

                                                                [4] => Array

                                                                        [isActivated] => 
                                                                        [stepIndex] => 0
                                                                        [state] => 9
                                                                        [hidden] => 
                                                                        [nodeHash] => 4

                                                                [5] => Array

                                                                        [isActivated] => 1
                                                                        [stepIndex] => 0
                                                                        [state] => 10
                                                                        [hidden] => 
                                                                        [nodeHash] => 5

                                                                [6] => Array

                                                                        [isActivated] => 1
                                                                        [stepIndex] => 0
                                                                        [state] => 0
                                                                        [hidden] => 
                                                                        [nodeHash] => 6

                                                                [7] => Array

                                                                        [isActivated] => 1
                                                                        [stepIndex] => 0
                                                                        [state] => 13
                                                                        [hidden] => 1
                                                                        [nodeHash] => 7

                                                                [8] => Array

                                                                        [isActivated] => 
                                                                        [stepIndex] => 0
                                                                        [state] => 9
                                                                        [hidden] => 
                                                                        [nodeHash] => 8

                                                                [9] => Array

                                                                        [isActivated] => 1
                                                                        [stepIndex] => 0
                                                                        [state] => 10
                                                                        [hidden] => 
                                                                        [nodeHash] => 9

                                                                [10] => Array

                                                                        [isActivated] => 
                                                                        [stepIndex] => 0
                                                                        [state] => 9
                                                                        [hidden] => 
                                                                        [nodeHash] => 10

                                                                [11] => Array

                                                                        [isActivated] => 
                                                                        [stepIndex] => 0
                                                                        [state] => 9
                                                                        [hidden] => 
                                                                        [nodeHash] => 11

                                                        [useCustomDyes] => 1
                                                        [artRegions] => Array
                                                        [isEquipment] => 1
                                                        [isGridComplete] => 1
                                                        [perks] => Array
                                                        [location] => 1
                                                        [transferStatus] => 1
                                                        [locked] => 1
                                                        [lockable] => 1
                                                        [objectives] => Array
                                                        [state] => 1

1 个答案:

答案 0 :(得分:2)

你的循环很好,它应该迭代所有节点,但是有很多节点。

顺便说一句:你应该把它分解成一个变量,让你的代码不那么费劲:

$nodes = $json['Response']['data']['buckets']['Equippable']['1']['items']['0']['nodes'];

foreach($nodes as $talentNode) {
    // Perform operations on each nodes...

    if($talentNode['isActivated'])
          $nodesActive[] = $talentNode;

     $nodeState[] = $talentNode['state'];
}

$nodesActive; // Array of the nodes active
$nodeState; // Array with the state of each node