I have been tearing through the PHP books trying to find out how to do what I'm trying to do.

I have a multidimensional array:

Array ( [errorCode] => 0 [errorMessage] => [results] => Array ( [http://the_url_being_shortened] => Array ( [hash] => CRWYp [shortKeywordUrl] => [shortUrl] => http://bit.ly/18jXxp [userHash] => 18jXxp ) ) [statusCode] => OK ) 

I want to grab the value for the key [shortUrl] and write it into a document.

I have assigned the array to a variable, "$url". I have tried to do stuff like:

print_r($url['results']['shortUrl']);

but that doesn't get me to the value.

How do I parse the array to get to this value, in the above example, "http://bit.ly/18jXxp" so I can add it to my fwrite statement?

Thanks
spivey

    The problem is the array you are starting with. IT looks like it is using "[http://the_url_being_shortened]" as the key for the nested array that contains shortURL. You might want to revisit the code that generates your multidimensional array and sort that out.

      The result print_r() returned would have been formatted like this:

      Array
      (
      	[errorCode] => 0
      	[errorMessage] =>
      	[results] => Array
      		(
      			[http://the_url_being_shortened] => Array
      				(
      					[hash] => CRWYp
      					[shortKeywordUrl] =>
      					[shortUrl] => http://bit.ly/18jXxp
      					[userHash] => 18jXxp
      				)
      		)
      	[statusCode] => OK
      )

      (If you're displaying it in a browser, use <pre></pre> tags or just view the page source itself instead of the rendered HTML).

      Which shows that, as sneakyimp says, that the 'shortUrl' key is nested three levels deep: $url['results']['http://the_url_being_shortened']['shortUrl'].

        Hi,
        u could try this:

        $count = count ($YourArray);
        for ($i=0; $i<$count; $i++){
        $countmore=count($YourArray[0]);
        for ($j=0; $j < $countmore; $j++){
        //print ($YourArray[$i][$j] . "<br> ");

        $text =$YourArray[$i][$j];
        //echo $text ;
        $str = stristr ($text, "SearchedValue");
        echo $str ;

        Hope this could be helpful.

          Weedpacket;10912066 wrote:

          Which shows that, as sneakyimp says, that the 'shortUrl' key is nested three levels deep: $url['results']['http://the_url_being_shortened']['shortUrl'].

          Hm. I tried that, but using the variable for the url when it is passed from the form input (a user inputs a url in a form:

          $formurl = $_POST['input_url'];

          So I tried

          $url['results'][$formurl]['shortUrl']

          but that didn't work.

            When one level fails, the best thing to do is start stepping through them.

            <pre><?php
            echo 'Posted URL: '.$_POST['input_url']."\n\n";
            
            $formUrl = $_POST['input_url'];
            
            print_r($url)."\n";
            print_r($url['results'])."\n";
            print_r($url['results'][$formUrl])."\n";
            print_r($url['results'][$formUrl]['shortUrl'])."\n";
            ?></pre>

            Something somewhere along the way is not equalling what you think it does. I'm guessing that the posted URL and the url used for the key are different.

              bpat1434, I had to go back and make sure I was grabbing the submitted value. I got it and it works. Thanks!

              spivey

                Write a Reply...