Ashley
Ashley

Reputation: 71

foreach loop on JSON array

Thanks for taking the time to look at my question.

Hope this makes sense...

Im looking to extract data from a JSON array.

In the array below I am wanting to loop through the nodes tab at Response>data>buckets>Equippable>1>items>0>nodes to see if its active and if so what state it is.

Problem is the 'nodes' section can be different lenghts, this example has 12 results but the next my only have 8 etc

For a fixed result i would use this

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

how would i do the foreach loop when i dont know how many results there will be there?

  [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

Upvotes: 0

Views: 121

Answers (1)

FrenchMajesty
FrenchMajesty

Reputation: 1139

Your loop is fine, it should iterate over all the nodes however many there is.

BTW: You should break it out into a variable to make your code less strenuous on your eyes:

$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

Upvotes: 2

Related Questions