Jump to content

Weird variable issue


DaveyK

Recommended Posts

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>";
        }
    }
}

 

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]

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.