0

I have a foreach loop what works fine but i need to call it a few time within the same page to get different results so i thought a function would be better but i keep getting Warning: Invalid argument supplied for foreach()

heres the code that works

$weaponSlot = '1';

$talentGridHash =  $json2['Response']['data']['buckets']['Equippable'][''.$weaponSlot.'']['items']['0']['talentGridHash'];

$nodes = $json2['Response']['data']['buckets']['Equippable'][''.$weaponSlot.'']['items']['0']['nodes'];

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

if($talentNode['isActivated'] && $talentNode['state'] === 10){
      $nodesActive[] = $talentNode;

    $perkName = $json2['Response']['definitions']['talentGrids'][''.$talentGridHash.'']['nodes'][''.$talentNode['nodeHash'].'']['steps']['0']['nodeStepName'];
    $perkIcon =  $json2['Response']['definitions']['talentGrids'][''.$talentGridHash.'']['nodes'][''.$talentNode['nodeHash'].'']['steps']['0']['icon'];

    if (strpos($perkName, 'Damage') == false) { 

$perkOutput .= '<div style="border:1px solid #999; border-radius:3px; padding:1px; float:left; margin-right:8px"><img src="http://www.example.com'.$perkIcon.'" height="22" title='.$perkName.'" /></div>'; 


    }

}

}

echo $perkOutput;

heres the code into a function...have i missed something?? (BTW this is the first time ive done a function, looked at examples online)

$perkOutput = null;
function getItemPerks($weaponSlot){

$talentGridHash =  $json2['Response']['data']['buckets']['Equippable'][''.$weaponSlot.'']['items']['0']['talentGridHash'];

$nodes = $json2['Response']['data']['buckets']['Equippable'][''.$weaponSlot.'']['items']['0']['nodes'];

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

if($talentNode['isActivated'] && $talentNode['state'] === 10){
      $nodesActive[] = $talentNode;

    $perkName = $json2['Response']['definitions']['talentGrids'][''.$talentGridHash.'']['nodes'][''.$talentNode['nodeHash'].'']['steps']['0']['nodeStepName'];
    $perkIcon =  $json2['Response']['definitions']['talentGrids'][''.$talentGridHash.'']['nodes'][''.$talentNode['nodeHash'].'']['steps']['0']['icon'];

    if (strpos($perkName, 'Damage') == false) { 

$perkOutput .= '<div style="border:1px solid #999; border-radius:3px; padding:1px; float:left; margin-right:8px"><img src="http://www.example.com'.$perkIcon.'" height="22" title='.$perkName.'" /></div>'; 

    }

}

}

return $perkOutput;

}


getItemPerks(1);
0

1 Answer 1

2

$json2 is not available in the function so $nodes is undefined. You need to pass it in as an argument the same as $weaponSlot.

function getItemPerks($weaponSlot, $json){
    $nodes = $json //etc...
}

Then call: getItemPerks(1, $json2);

Or make a call to another function inside this one that fetches the JSON and returns an array:

function getItemPerks($weaponSlot){
    $json = getJSON();
    $nodes = $json //etc...
}

function getJSON(){
    $json = //get your JSON somehow
    return json_decode($json, true);
}

Then call: getItemPerks(1);

See PHP: Variable Scope

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.