Dynamic Variable gets an Array Value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tyler Harder
    New Member
    • Nov 2010
    • 11

    Dynamic Variable gets an Array Value

    Hello,
    I'm having some trouble creating a dynamic variable that will load an array value:

    $this_content_j son["content"][0]["popularity "]

    When I use the above line 'hard coded', it works. But when I use the code below as a dynamic variable, it fails:

    ${ 'this_content_j son["content"][0]["popularity "]' }

    Any help would be muchos appreciados.

    Tyler
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    what is so dynamic about the second example?

    Are you trying to display it in a string?

    Dan

    Comment

    • Tyler Harder
      New Member
      • Nov 2010
      • 11

      #3
      Hi Dan,
      Thanks for your reply. Once I get it working, I'm going to start changing the bits inside to access different array contents, which is my reason for using a dynamic array.

      For example, I may do:
      ${ 'this_content_j son[$a][$b][$c]' }

      My issue at the moment is that the code I first posted isn't even accessing the basic contents in the original variable, so I think I'm making some rookie mistake.

      This works, and echo's the value:
      echo $this_content_j son["content"][0]["popularity "];

      But this doesn't:
      echo ${ 'this_content_j son["content"][0]["popularity "]' };

      Any idea how I can properly recreate the variable in dynamic notation?

      Tyler

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        I am bewildered as to what you are trying to do.
        Code:
        //This works, and echo's the value:
        echo $this_content_json["content"][0]["popularity"];
        This is legal syntax but I would like to see some justification for the need of a 3d array?
        Code:
        //But this doesn't:
        echo ${ 'this_content_json["content"][0]["popularity"]' };
        This is incorrect syntax, but I can't correct it because I don;t see what you are trying to achieve.
        I am not sure what you mean by dynamic variable, are you trying to use variable variables?

        Comment

        • Tyler Harder
          New Member
          • Nov 2010
          • 11

          #5
          Hi Dan, thanks for staying with me here. Let me try again...

          1) I have an multidimensiona l array of data called "$this_content_ json".

          2) I would like to access single pieces of data in that array using the standard way to do so - $this_content_j son[$a][$b][$c] - where $a, $b, and $c are the names of the various keys in the array.

          3) I am being handed those keys in a an unconventional format - a string that looks like: "[content][0][popularity]". I need to use that string variable to access the respective item in the array.

          In summary, I have two things to work with - a string that looks like "[content][0][popularity]" and a var called "$this_content_ json" that holds the content. The string changes depending on the instructions I am passed. I want to combine the two so I can access the relevant information in the array.

          Kapiche?

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            So this is a string and not an array
            Code:
            "[content][0][popularity]"
            Where content is a key, popularity is a key and likewise 0 in the array $this_content_j son.
            And you want the value at reference $this_content_j son['content'][0]['popularity'].
            I see the problem. I suppose you could split the string using strtok or explode (or is it implode??)
            so $a = content,$b = 0,$c = popularity.
            Then reference the array with $this_content_j son[$a][$b][$c].
            But using the string directly? try one or all of these
            Code:
            echo $this_content_json.'["content"][0]["popularity"]';
            
            $element = 'this_content_json["content"][0]["popularity"]';
            echo $$element;
             
            $element = 'this_content_json';
            $element .= '["content"][0]["popularity"]';
            echo $$element;
            Or variations thereof. I am sure there is a correct format but it may need trial and error.

            Comment

            • Tyler Harder
              New Member
              • Nov 2010
              • 11

              #7
              Thanks Code Green,
              We`re on the same page now. I tested all three of the above proposed solutions, and none printed the variable.

              I`m surprised none of these notations works. I have tried many additional variations myself with no luck. I thought I would at least be able to stumble on by luck if not skill...

              I understand I can parse the string as suggested (strtok, explode, etc..), and will probably have to do so - it just seems like the long way to do it. I`m sure somehow there`s got to be a way to access it via the dynamic var format.

              Anyhow thanks kindly for your ideas.

              Tyler

              Comment

              • code green
                Recognized Expert Top Contributor
                • Mar 2007
                • 1726

                #8
                Actually the second two of my examples are wrong.
                Don't know what I was thinking, but I have simply created variables of that name.

                Comment

                • Tyler Harder
                  New Member
                  • Nov 2010
                  • 11

                  #9
                  Yeah unfortunately the first one didn`t work either. I guess I`ll hack the parts apart.

                  Comment

                  • code green
                    Recognized Expert Top Contributor
                    • Mar 2007
                    • 1726

                    #10
                    I am persevering with this because I'm sure I have done similar
                    OK this is a string
                    Code:
                    $ref = '["content"][0]["popularity"]';
                    That needs resolving as an array element.
                    This is the array $this_content_j son;
                    This doesn't work
                    Code:
                    echo $this_content_json.'["content"][0]["popularity"]';
                    Because '["content"][0]["popularity "]' is still a string, it has not been resolved.
                    What about this
                    Code:
                    $element = $this_content_json.$ref; 
                    echo $element;

                    Comment

                    • Tyler Harder
                      New Member
                      • Nov 2010
                      • 11

                      #11
                      Hey, thanks.

                      Unfortunately no dice. It prints out this instead of the proper data:

                      Array["content"][0]["popularity "]

                      As you can see the word Array is actually spelled out as a string (as php tends to do if you echo an array), followed by the string contents of the second part. I`m not sure why it`s not resolving to the array data instead.

                      Comment

                      • Tyler Harder
                        New Member
                        • Nov 2010
                        • 11

                        #12
                        Hey Code Green,
                        Thanks again for your efforts. While the above did not work, I found an explanation as to why:



                        It seems that is pretty much MUST be exploded before a string can be used to access a variable in the way I had hoped. So... not really possible after all.

                        Tyler

                        Comment

                        Working...