0

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
1
  • 1
    Your code works fine for any amount of elements inside the node array. Why do you think it wouldn't if there were more or less values in there...? Commented May 29, 2017 at 23:26

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, using your updated version of code, how do i find which nodes have [isActivated] => 1 and [state] => 10
@Ashley I have updated my code sample, to show how you would perform that

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.