0

This seems like such a rookie question but I'm just banging my head against the keyboard here and I can't find anything answered already that gets me moving forward.

This is my PHP:

<?php
header('Content-Type: text/plain; charset=utf-8;'); 
$url = file_get_contents("https://example.org/subdomain/api.php?t=t&q=maria");
print_r(json_decode($url));
?> 

This is the results:

stdClass Object
(
    [apiVersion] => 1
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 47
                    [by] => 5
                    [title] => WATER
                    ...
                 )
            [1] => stdClass Object
                (
                    [id] => 45
                    [by] => 5
                    [title] => HEARTH
                    ...
                 )
      ...

How could I extract and echo the property title?

I tried this without success (error 500, blank page)

<?php
header('Content-Type: text/plain; charset=utf-8;'); 

$url = 'https://example.org/subdomain/api.php?t=t&q=maria';
$json = file_get_contents($url);

$answersArray = Array();
for($i=0;$i<count($jsonArray['data']);$i++){
    array_push($answersArray,$jsonArray['data'][$i]['title']);
}

UPDATE

I tried also with this code, still no luck

<?php
header('Content-Type: text/plain; charset=utf-8;'); 

$url = 'https://example.org/subdomain/api.php?t=t&q=maria';
$json = file_get_contents($url);

$answersArray = Array();
for($i=0;$i<count($jsonArray['data']);$i++){
    array_push(echo $answersArray,$jsonArray['data'][$i]->title);

}
foreach($answersArray as $answer) {
    echo $answer;
}
?>
8
  • 1
    1) You need to decode your json data 2) Simply loop through your subArray data 3) Return the property title for each object: $titles = array_map(function($v){return $v->title;}, json_decode($json)["data"]); Commented Sep 10, 2015 at 15:38
  • @Rizier123 Thanks, I tried with this but it's still not working $json = file_get_contents($url); $data = json_decode($json); foreach($data as $key => $subarray) { $titles = array_map(function($v){return $v->title;}, json_decode($json)["data"]); } Commented Sep 10, 2015 at 16:13
  • Just use the code above no foreach loop. Commented Sep 10, 2015 at 16:15
  • @Rizier123 I tried but I get 500 internal server error Commented Sep 10, 2015 at 16:22
  • 1
    Add error reporting: ini_set("display_errors", 1); error_reporting(E_ALL); at the top of your file(s) and tell us if you get any + check your error logs and tell what you get. (Also what is the output of: echo PHP_VERSION; ?) Commented Sep 10, 2015 at 17:27

4 Answers 4

3

The problem is that you're trying to access an object property like an array:

array_push($answersArray,$jsonArray->data[$i]['title']);

should be:

array_push($answersArray,$jsonArray->data[$i]->title);

To output the titles later you can use the following:

foreach($answersArray as $answer) {
    echo $answer;
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks there are no more errors but I'm unable to echo...how could I display only each title?
@Simone Where are you trying to echo? Your code only shows that you are storing each title in the $answersArray. You can loop that to echo each title.
I tried outside the loop to echo $answersArray but the results is the word "array"
@Simone I've added a simple example for echoing each title from the array.
no data shown, only a white page, and from console I got 500 server error...I updated the question with my code
|
2

Basic steps

  • Decode the JSON.
  • Get array at 'data' property.
  • Iterate the 'data' array.
  • Load title property for each 'data' item into new array.

Code example:

<?php
header('Content-Type: text/plain; charset=utf-8;');

$url = 'https://example.org/subdomain/api.php?t=t&q=maria';
$json = file_get_contents($url);
$obj = json_decode($json);
$data = $obj->data; // get data array
$titles = array();

foreach ($data as $item) {
    $titles[] = $item->title;
}
print_r($titles);
?>

5 Comments

Hi, I don't know why but with this solution I get the 500 internal server error
GET example.org/subdomain/prova.php 500 (Internal Server Error) ...prova.php:1
Who upvoted this? You don't access a property with . in PHP
@Rizier123 Stupid typo for me, been doing a lot of javascripting lately :). It is now corrected. Concept remains the same.
thanks, I just used echo implode(',', $titles); to display results in comma separated list
1

Your errors lies in the utilization of the json_decode function... you need to specify true as the second argument to actually get an array instead of an object.

<?php
header('Content-Type: text/plain; charset=utf-8;'); 
$url = file_get_contents("https://example.org/subdomain/api.php?t=t&q=maria");
print_r(json_decode($url, true));

And then, if you use the foreach, or better, array_column (php 5.5), you can fetch the title property of each object.

<?php
header('Content-Type: text/plain; charset=utf-8;'); 
$url = file_get_contents("https://example.org/subdomain/api.php?t=t&q=maria");
$response = json_decode($url, true);

$titles = array_column($response['data'], 'title');
var_dump($titles);

2 Comments

your solution give me the word "NULL" displaying in page, nothing else
Sorry, made a typo, which I just fixed !
0
<?php 
 $json=file_get_contents("https://2b4bxi178h.execute-api.us-east-2.amazonaws.com/dev/h");
      $weather=json_decode($json,true);   


          foreach ([''] as $key => $value) {
             $temperature= $value['Robot_No']['Table_Clean'];
             echo "$temperature";
           }
?>

1 Comment

Code-only answers are not particularly helpful. Please include a brief description of how this code solves the problem.

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.