-1

I have this array that I read (using curl) in PHP it returns a string that looks like a json so I convert it into a real json.

But what ever I do to read it, does not work:

what am I doing wrong and how can I reach those values?

This is the result that I get:

Array
(
    [results] => Array
        (
            [1] => Array
                (
                    [pack_id] => HUJoUJKK673ED
                    [imre] => 87687548574
                    [imrd] => 87457654764
                    [cell_id] => 775443
                    [firm_vrs] => 2
                    [gg_yr] => 2017
                    [gg_mn] => 3
                    [gg_dy] => 20
                    [gg_hr] => 15

This is my code

<?php
    $url = $_GET["url"];
    $curlSession = curl_init();
    curl_setopt($curlSession, CURLOPT_URL, $url);
    curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
    $returnVal = curl_exec($curlSession);
    curl_close($curlSession);

    //converting the retreived "json lookalike text" in to a json:
    $decodedText = html_entity_decode($returnVal);
    $myArray = json_decode($decodedText, true);

    //this DOES work and shows the json as an array, as described above.
    echo "<pre>";
    print_r($myArray);
    echo "</pre>";

    //BUT non of the following lines work:
    $echo = sizeof($myArray->results);
    echo "<br />".$myArray->results[1]->imre;
    echo "<br />".sizeof($myArray);
    echo "<br />".sizeof($myArray->results);
    echo "<br />".sizeof($myArray->results[1]);
    echo "<br />".sizeof($myArray[1][1]);
    echo "<br />".sizeof($myArray[0]);
    echo "<br />".sizeof($myArray[0]->results);
    echo "<br />".sizeof($myArray->imre);
    echo "<br />".sizeof($myArray[0]->results);
    echo "<br />".$myArray->results['1']->imre;
    echo "<br />".$myArray[0]->results[1]->imre;
    echo "<br />".$myArray->results[1]->imre;
    echo "<br />".sizeof($myArray->results[1]);
?>
user3495363
  • 349
  • 2
  • 11
  • 1
    `$myArray['results'][1]['pack_id']` – Jonnix Mar 21 '17 at 15:11
  • I've downvoted this question, because it's really basic PHP - how to access elements of an array - and because you should really [read the manual](http://php.net/json_decode) for code that you're using, and if you didn't know what the `true` in `json_decode` meant, it suggests you're copying and pasting code without understanding it. – IMSoP Mar 21 '17 at 15:17
  • 1
    solved my problem! thanx! – user3495363 Mar 21 '17 at 15:36

1 Answers1

2

In your line $myArray = json_decode($decodedText, true); the true means the object will be converted to an associative array.

However, with $myArray->results and similar lines, you're trying to reference it like an object. Try $myArray['results'] instead.

peteredhead
  • 2,394
  • 1
  • 15
  • 21