-2

I wrote the following code:

$text ="";
$lat = 0;
$long = 0;

foreach ($tweets->{'statuses'} as $value) {
    if($value->{'geo'}!=null){
        $text = $value->{'text'};
        $prop = get_object_vars($value->{'geo'});
        $lat = $prop['coordinates'][0];
        $long = $prop['coordinates'][1];
    }
}

I want to create an array of objects so then I will be able to convert it to JSON using json_encode, I want to have something like as my final result:

{{text:"Something",lat:23.231,long:2312321},{text:"SomethingElse",lat:33.231,long:412321}}

So I want to create an array of objects, each object will have text,lat and long. How can I achieve that?

3
  • There is a perfectly good manual for PHP. Read it. Commented May 25, 2014 at 9:32
  • This question appears to be off-topic because it is about not having read the manual. Commented May 25, 2014 at 9:33
  • Actually not even searching the site that has a very good explanation of how to come closer to the wanted result - next to reading the PHP manual naturally. Commented May 25, 2014 at 9:34

1 Answer 1

0
$results = array();

foreach ($tweets->{'statuses'} as $value) 
{
    if($value->{'geo'}!=null)
    {    
        $prop = get_object_vars($value->{'geo'});

        $results []= array(
           'text' => $value->{'text'},
           'lat' => $prop['coordinates'][0],
           'long' => $prop['coordinates'][1]
        );
    }
}

return json_encode($results);
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, although there's quite a few redundant braces in here (e.g. $tweets->{'statuses'}) copied from the OP's post. I'd usually point that out to the OP.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.