Q:

How to create an array of objects in php

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?

A:

$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);

