mac_gyver Posted June 4, 2013 Share Posted June 4, 2013 i think the answer to this vague question might be to use a reference - $somekey = 'city'; $root = &$structure[0]['cards'][0]['info'][$somekey]; // form a reference to a point in the structure echo $root['title']; // use one specific value at the referenced point // loop over all values at the referenced point foreach ($root as $key=>$value){ if(!is_array($value)){ echo "Key: $key, Value: $value<br>"; } else { echo "Key: $key, "; foreach($value as $skey=>$svalue){ echo "SubKey: $skey, SubValue: $svalue<br>"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/278716-weird-variable-issue/page/2/#findComment-1434054 Share on other sites More sharing options...
Jessica Posted June 4, 2013 Share Posted June 4, 2013 i think the answer to this vague question might be to use a reference - $somekey = 'city'; $root = &$structure[0]['cards'][0]['info'][$somekey]; // form a reference to a point in the structure echo $root['title']; // use one specific value at the referenced point // loop over all values at the referenced point foreach ($root as $key=>$value){ if(!is_array($value)){ echo "Key: $key, Value: $value<br>"; } else { echo "Key: $key, "; foreach($value as $skey=>$svalue){ echo "SubKey: $skey, SubValue: $svalue<br>"; } } } Why would you bother with three lines of code though? That's the same as just doing: echo $structure[0]['cards'][0]['info']['city']['title'] Or: echo $structure[0]['cards'][0]['info'][$somekey][$someotherkey] Quote Link to comment https://forums.phpfreaks.com/topic/278716-weird-variable-issue/page/2/#findComment-1434055 Share on other sites More sharing options...
DaveyK Posted June 5, 2013 Author Share Posted June 5, 2013 So the city part and the title part are variable, the rest is static. That makes it easy, just use the appropriate variables as keys: $structure[0][cards][0][info][$type][$property] You must already have some code to determine that you want 'city' and 'title'. Just use that same bit of code but store the results into the variables. Then to read the value, use those variables as keys. Yes, I have all that info. Thank you, that might just solve my problem. Ill let you know as soon as I tried this, probably later today. OP: If you always are going to use the first card, is there a reason you have it in an array called 'cards'? Why not just one card? I will only use the first card to replace strings on the page, but all the other cards will be displayed normally. Quote Link to comment https://forums.phpfreaks.com/topic/278716-weird-variable-issue/page/2/#findComment-1434221 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.