0

Hi i am trying to fetch single value from below Array example -

i want to fetch order_id and name -

<?php
    $url = 'http://example.com/index.php?route=feed/rest_api/orders&key=test219';

        $json = file_get_contents($url);

    echo '<pre>'; echo $json; echo '</pre>';
?>

Array showing

Array
(
    [0] => Array
        (
            [order_id] => 6
            [name] => zoraya panansar
            [status] => Complete
            [date_added] => 2014-01-24 23:06:09
            [products] => 4
            [total] => 14.8000
            [address_1] => 345 goldhawk road
            [address_2] => hammersmith
            [city] => london
            [Postcode] => w6 0wz
            [Country] => United Kingdom
            [Email] => [email protected]
            [Tele.] => 0786870150
        )

I already tried

echo $json->order_id;

and

echo $json[0]['order_id'];

but its not working... Please help

4
  • have you tried echo $json['order_id'] Commented Mar 15, 2014 at 21:08
  • I can't see you actually decoding that JSON anywhere... did you forget to post it or did you forget to add json_decode($json) ? Commented Mar 15, 2014 at 21:09
  • it looks like the output from this url is a string representation of an array in php. so basically you're getting the string when running file_get_contents. so you cannot access order_id from $json Commented Mar 15, 2014 at 21:10
  • yes i have tried with json_decode($json) Commented Mar 15, 2014 at 21:16

3 Answers 3

1

index.php is returning wrong data, if you look closer then you will see it is trying to send success = false result as json data but probably for testing purposes, it is also returning array content directly before the json data.

Besides, if this is supposed to be a json result then you need to use json_decode before using the returned json object as a class object like $json->order_id.

This is what I get when I try your current URL With the parameters you provided: Look at the last line:

Array
(
    [0] => Array
        (
            [order_id] => 6
            [name] => zoraya panansar
            [status] => Complete
            [date_added] => 2014-01-24 23:06:09
            [products] => 4
            [total] => 14.8000
            [address_1] => 345 goldhawk road
            [address_2] => hammersmith
            [city] => london
            [Postcode] => w6 0wz
            [Country] => United Kingdom
            [Email] => [email protected]
            [Tele.] => 0786870150
        )

    [1] => Array
        (
            [order_id] => 5
            [name] => ciria ann nuqui
            [status] => Complete
            [date_added] => 2014-01-24 17:58:36
            [products] => 7
            [total] => 36.4000
            [address_1] => 345 goldhawk road 
            [address_2] => hammersmith
            [city] => london
            [Postcode] => w6 0wz
            [Country] => United Kingdom
            [Email] => [email protected]
            [Tele.] => 07515732291
        )
)
{"success":false}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply. That is different query.
@user1250920, You need to check index.php no matter what the query is, it is not returning "only" json data. After fixing it then you need to use json_decode(file_get_contents($url)) to decode the text to use as json object.
@user1250920 - Because index.php in the URL doesn't return correct data - as I explained. It needs to be checked first. json_decode is the next part after fixing the problem in index.php.
0

You can not access the data that the server is returning. Instead of returning you a json object, the server is returning you the print_r() output over an array.

If its your server, then stop the print, and use json_encode() over the array before printing.

If it's not your server, then you have to parse the values from the text. You can use preg_match_all() for this.

preg_match_all("/\[order_id\] => ([^\n\r]+)/", $json, $m);
print_r($m[1]);

There may be built-in php function that can take such print_r() output and revert back into a php array, but I don't know about it!

5 Comments

yes but it showing in array - result - Array ( [0] => zoraya panansar [1] => ciria ann nuqui )
I want to show with array like - zoraya panansar <br> ciria ann nuqui
try using implode('<br>', $m[1]); or join('<br>', $m[1]);
Sorry my mistake ... Working now
can i get multiple value in same implode?
0

Change the response from the url to json (Use json_encode function)
The last Row in url script should be

    echo json_encode($some_array);

And than if you want the content:

    $array = json_decode(file_get_contents($url),TRUE);
    if(!is_array($array)){
      echo 'bad response';
      exit();
     }
     var_dump($array);
     echo $array[0]['order_id'];

1 Comment

Thanks, got the - bad response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.