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>";
        }
    }
}
Link to comment
Share on other sites

 

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.