Jump to content

Recommended Posts

To directly answer your question: it's because of the array. It doesn't resolve $$something where $something is an array element as you have, to a variable variable. 

 

Now, why are you doing it this anyway? I've not once had to use a variable variable and have always advised to avoid it as there are cleaner methods.

Edited by cpd

I need to access some variable inside an array, based on another variable... I know its really stupid and not that nice but lets just say thats what I need.

 

What is the cleaner method then? Ive done the google research, but I mostly find how to echo an array or something :/

Edited by DaveyK

I am building an app where the content of that page is defined by "cards" or tiles. Regardless, I store the title to that page in the database. For, say, a user profile I COULD store "User profile" as the pge title, but instead I want to show the actual username. So, in the database, I store a string that I can replace. Something along the lines of :name.

 

So on the page itself, I make the PHP recognize that string and let it find the variable to match, which means I have to dig deep into an array which holds all of the cards and the relevant information. I could try to move the actual info much higher in the array, but I am always required to find the variable inside an array...

 

CASE: I want a dynamic page header where the database is (obviously) rather static. ":name" should be replaced to "Davey Kulter" where that name (which is SURPRISINGLY my name) is stored inside an array.

there's no point in using variable variables for sets of data. you simply use an array.

 

what part of the 'structure[0]' in the first post is dynamic? the array name or the array index? if it's the array name, use one two dimensional array with variables for both indexes. if it's the [0] index, simply use a variable for the index.

Are you programming procedurally? If not I suggest you look at using a different data structure. 

 

If you are, use a two dimensional array as already suggested.

 

E.g.

$a = array(
   "tile1" => array(
      "card1", "card2", "card3"
   ),
   "tile2" => array(
      ...
   )
);

If you are about suggest an ORM , I am not gonna do that. I only just started using PDO and I really dont feel like two those two things at a time right now.

 

Here is an example of $structure:

Array
(
  [0] => Array
  (
    [structure] => row
    [cards] => Array
    (
      [0] => Array
      (
        [view_id] => 4
        [card_id] => 4
        [status] => 1
        [name] => city_profile_general_info
        [title] => Information
        [card_min_status] => 1
        [cache_time] => 1800
        [col_span] => 2
        [order_by] => 1
        [info] => Array
        (
          [city] => Array
          (
              [id] => 9
              [type] => city
              [title] => Paris
              [city] => Array
                  (
                      [id] => 9
                      [name] => Paris
                  )

              [state] => Array
                  (
                      [id] => 
                      [name] => 
                  )

              [country] => Array
                  (
                      [id] => 76
                      [name] => France
                  )

              [img_src] => http://trekeffect.images.user_images.s3.amazonaws.com/4_93.jpg
              [url] => /show.php?type=city&id=9
              [master_url] => /location/france/paris/9
          )
        )
      )
    )
  )
)

so in the database I store something like :title, so now I want the php to replace :title with the city title "$structure[0][cards][0][info][city][title]".

Okay, I am experiencing a really weird where this gives different results:

$str = 'structure[0]';
var_dump(isset($structure[0])); // bool(true)
var_dump(isset($$str)); // bool(false)

Does anyone know how this could be caused? Am I missing something?!

 

To answer the original question: structure[0] is not a variable, so you cannot use it as a variable variable.

 

If you post the actual array, we can help.

 

 

<?php
$structure = array('foo');
$str = 'structure[0]';
var_dump($structure[0]); // bool(true)
var_dump($$str); // bool(false)
?>

 

string(3) "foo"

PHP Notice:  Undefined variable: structure[0] in C:\wamp\www\c.php on line 5

PHP Stack trace:

PHP   1. {main}() C:\wamp\www\c.php:0

 

Notice: Undefined variable: structure[0] in C:\wamp\www\c.php on line 5

 

Call Stack:

    0.0004     230856   1. {main}() C:\wamp\www\c.php:0

 

NULL

[/code]

If you are about suggest an ORM , I am not gonna do that. I only just started using PDO and I really dont feel like two those two things at a time right now.

 

Here is an example of $structure:

Array
(
  [0] => Array
  (
    [structure] => row
    [cards] => Array
    (
      [0] => Array
      (
        [view_id] => 4
        [card_id] => 4
        [status] => 1
        [name] => city_profile_general_info
        [title] => Information
        [card_min_status] => 1
        [cache_time] => 1800
        [col_span] => 2
        [order_by] => 1
        [info] => Array
        (
          [city] => Array
          (
              [id] => 9
              [type] => city
              [title] => Paris
              [city] => Array
                  (
                      [id] => 9
                      [name] => Paris
                  )

              [state] => Array
                  (
                      [id] => 
                      [name] => 
                  )

              [country] => Array
                  (
                      [id] => 76
                      [name] => France
                  )

              [img_src] => http://trekeffect.images.user_images.s3.amazonaws.com/4_93.jpg
              [url] => /show.php?type=city&id=9
              [master_url] => /location/france/paris/9
          )
        )
      )
    )
  )
)

so in the database I store something like :title, so now I want the php to replace :title with the city title "$structure[0][cards][0][info][city][title]".

 

And what is the problem with writing that out?

 

Are you stuck on using a loop to do it for each one? This is not a problem that requires variable variables.

Edited by Jessica

If you're using PDO you can use a different data structure. What you have at the minute is really confusing and can be massively simplified. I'd be happy to assist if you like...

 

And no I wasn't about to suggest PDO, that's an object for interfacing with a database... 

Edited by cpd

Well, right now I am using PDO like I used mysql_*, except for the binding of params. Dont get me wrong, I want the array to look like that because each key of structure is a part of the structure I am building inside the page.

 

This is very confusing <_>

And what is the problem with writing that out?

 

Are you stuck on using a loop to do it for each one? This is not a problem that requires variable variables.

 

No, the issue is that I have that array structure with the variables I need to display my cards. All that works, no worries.

 

But I also have a page title COMPLETELY seperate from the cards. I want that title to be defined from the variables inside the structure array. How can I access an array when I create it from a string <_>

Let me visualize this for you guys. This is the screen shot:

 

http://easycaptures.com/fs/uploaded/654/2329582622.jpg

 

$structure:

Array
(
  [0] => Array
  (
    [structure] => row
    [cards] => Array
    (
      [0] => Array
      (
        [view_id] => 1
        [card_id] => 1
        [status] => 1
        [name] => user_profile_general_info
        [title] => Information
        [card_min_status] => 1
        [cache_time] => 1800
        [col_span] => 2
        [order_by] => 1
        [info] => Array
        (
          [user] => Array
          (
            [id] => 1
            [email] => dkulter@gmail.com
            [name] => Davey Kulter
            [status] => 9
            [avatar] => http://trekeffect.images.user_images.s3.amazonaws.com/1_avi.jpg
            [oauth_uid] => 
            [oauth_provider] => 
            [signup_date] => 2013-05-23 03:14:42
          )
        )
      )
    )
  )
  [1] => Array
  (
    [structure] => column
    [cards] => Array
    (
      [0] => Array
      (
        [view_id] => 3
        [card_id] => 3
        [status] => 1
        [name] => user_profile_views
        [title] => Recent views
        [card_min_status] => 1
        [cache_time] => 1800
        [col_span] => 1
        [order_by] => 2
        [info] => Array
        (
          [views] => Array
          (
            [0] => Array
            (
              [id] => 12
              [type] => city
              [title] => San Francisco
              [city] => Array
              (
                [id] => 12
                [name] => San Francisco
              )
              [state] => Array
              (
                [id] => 5
                [name] => California
              )
              [country] => Array
              (
                [id] => 236
                [name] => United States
              )
              [img_src] => http://trekeffect.images.user_images.s3.amazonaws.com/4_161.jpg
              [url] => /show.php?type=city&id=12
              [master_url] => /location/united-states/san-francisco/12
              [time] => 1369825876
            )

            [1] => Array
            (
              [id] => 19
              [type] => attraction
              [title] => Mauna Kea Summit
              [city] => Array
              (
                [id] => 6
                [name] => Island of Hawaii
              )
              [state] => Array
              (
                [id] => 12
                [name] => Hawaii
              )
              [country] => Array
              (
                [id] => 236
                [name] => United States
              )
              [img_src] => http://trekeffect.images.user_images.s3.amazonaws.com/4_36.jpg
              [url] => /show.php?type=attraction&id=19
              [master_url] => /attraction/mauna-kea-summit/19
              [time] => 1369482664
            )
            [2] => Array
            (
              [id] => 60
              [type] => attraction
              [title] => Cathédrale de Notre Dame de Paris
              [city] => Array
              (
                [id] => 9
                [name] => Paris
              )
              [state] => Array
              (
                [id] => 
                [name] => 
              )
              [country] => Array
              (
                [id] => 76
                [name] => France
              )
              [img_src] => http://trekeffect.images.user_images.s3.amazonaws.com/4_111.jpg
              [url] => /show.php?type=attraction&id=60
              [master_url] => /attraction/cathédrale-de-notre-dame-de-paris/60
              [time] => 1369397312
            )
          )
        )
      )
    )
  )
  [2] => Array
  (
    [structure] => column
    [cards] => Array
    (
      [0] => Array
      (
        [view_id] => 2
        [card_id] => 2
        [status] => 1
        [name] => user_profile_treks
        [title] => Treks
        [card_min_status] => 1
        [cache_time] => 1800
        [col_span] => 1
        [order_by] => 3
        [info] => Array
        (
          [treks] => Array
          (
            [0] => Array
            (
              [trek_id] => 2
              [title] => Hawaii 2013
              [return_time] => 0
              [depart_time] => 0
              [default_trip] => 1
              [trek_status] => 2
              [member_status] => 3
              [total_activities] => 0
              [activity_types] => Array
              (
              )
              [last_activity] => Array
              (
              )
              [total_members] => 1
              [users] => Array
              (
                [0] => Array
                (
                  [id] => 1
                  [name] => Davey Kulter
                  [mail] => dkulter@gmail.com
                  [trek_status] => 3
                  [avatar] => http://trekeffect.images.user_images.s3.amazonaws.com/1_avi.jpg
                )
              )
              [url] => /show.php?type=trek&id=2
              [master_url] => /show.php?type=trek&id=2
              [cover_photo] => http://trekeffect.images.system.s3.amazonaws.com/default/trek_large.png
            )
          )
        )
      )
    )
  )
)

Am I overcomplicating this? YES. Absolutely. But I am just doing the best I can without any pro helping me. Well, up untill now :D

Just store the value in a variable.

$myTitle = $structure[0][cards][0][info][city][title];

 

Then use $myTitle. Stop trying to store the name of the array.

 

I get that, but how am I supposed to be doing this? I have absolutely no idea as to how this works.

 

I am using this code (really raw, wrote it today). I want it to convert something like "_DATA[title]" to "$structure[0][cards][0][info][city][title]"

if (strpos($page->page_title, '_') !== false)
{
  $array = explode('_', $page->page_title);
  
  unset($array[0]);
  
  foreach ($array as  $k => $var)
  {
    if (strpos($var, '['))
    {
      $array = explode('[', $var);
      
      if (in_array($array[0], $data_sets))
      {
        $variable_path = $array;
        
        unset($variable_path[0]);
        
        foreach ($variable_path as  $k => &$key)
        {
          $key = substr(trim($key), 0, -1);
        }
        
        // $str = 'structure[0]';
        // var_dump(isset($structure[0]));
        // var_dump(isset($$str));
        // print_array($structure[0]);
        // var_dump($str);
        // var_dump(isset($$str));
        if (isset($$str))
        {
          $structure['page_params'][] = array('str' => $array[0], 'var' => $$str);
        }
        // print_array($variable_path);
      }  
    }
  }
}

and, well, I am stuck. Thats why I am here.

I'm sorry, but you're not really explaining the problem, and your code is not commented, so I have no idea what it's doing.

 

Forget what you've tried already. Just try assigning the value you want to the variable you're using. Like I did above.

I think Jessica is right, and we can start down the right path by knowing what you expect in this variable:

$page->page_title

From there, I'm sure we can figure out how to get you where you're going the right way.

 

 

This is not a problem that requires variable variables.

 

 

Thankfully, in more than a decade I've yet to come across a problem that did.

$page->page_title

Is a variable I fetch from the database. Its just a string, like "Welcome!" or information. HOWEVER, since I wish to use it as a page header on other places too, such as this page (http://easycaptures..../2329582622.jpg) I want to store a string in the database which the PHP can replace with a variable.

$structure[0][cards][0][info]

That part is static, then comes the page type, in this case city

$structure[0][cards][0][info][city]

and then just check if whatever comes after ':' isset in the array, in this title

$structure[0][cards][0][info][city][title]

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