1

i have two different arrays

$arr1=Array ( 'latitude' => 9.9252007, 'longitude' => 78.1197754, 'title' => 'Madurai' ) ;
$arr2=Array ( 'latitude' => 9.2323, 'longitude' => 78.23233, 'title' => 'Peraiyur' ) ;

i need to combine the both arrays into single and need to convert into json by json_encode, so my code is here to merge the array and parsed into json

$obj['markers']=(object) array( $arr1, $arr2 );

echo json_encode($obj);

the output is follows

 {"markers":
 {"0":{"latitude":9.9252007,"longitude":78.1197754,"title":"Madurai"},
  "1":{"latitude":9.2323,"longitude":78.23233,"title":"Peraiyur"}} }

but i need it to be like follow

{"markers":[
  { "latitude":9.9252007, "longitude":78.1197754, "title":"Madurai"  },
  { "latitude":9.2323, "longitude":78.23233, "title":"Peraiyur" }
]}

how to remove them?

2
  • Hey, why the down vote here?? can yo explain ? Commented Feb 21, 2014 at 11:07
  • 2
    Why would you create the $new_Arr then...anyway, you may simply try json_encode(array("markers"=>array($arr1,$arr2))); Commented Feb 21, 2014 at 11:07

5 Answers 5

1

Why casting into object? If you just remove the (object) casting it will be done!

$obj['markers']=array( $arr1, $arr2 );

result:

{"markers":[{"latitude":9.9252007,"longitude":78.1197754,"title":"Madurai"},{"latitude":9.2323,"longitude":78.23233,"title":"Peraiyur"}]}
Sign up to request clarification or add additional context in comments.

Comments

1
$obj['markers']=array( $arr1, $arr2 );

Comments

0

This object:

{"markers":[
  { "latitude":9.9252007, "longitude":78.1197754, "title":"Madurai"  },
  { "latitude":9.2323, "longitude":78.23233, "title":"Peraiyur" }
]}

is identical to:

print_r(json_encode(array('markers' => array($arr1, $arr2))));

Therefore, no need to do any additional complicated coding.

Comments

0

try this

$arr1=Array ( 'latitude' => 9.9252007, 'longitude' => 78.1197754, 'title' => 'Madurai' ) ;
$arr2=Array ( 'latitude' => 9.2323, 'longitude' => 78.23233, 'title' => 'Peraiyur' ) ;
$obj['markers']=array($arr1, $arr2);
echo json_encode($obj);

output :

{"markers":[{"latitude":9.9252007,"longitude":78.1197754,"title":"Madurai"},{"latitude":9.2323,"longitude":78.23233,"title":"Peraiyur"}]}

Comments

0

This is how it must be done:

$obj['markers']=array(
        array(
        'latitude' => $entity->getLat(),
        'longitude' => $entity->getLng(),
        'title' => $entity->getTitle(),
        )
    );
$json = json_encode($obj);
$response = new Response($json);
$response->headers->set('Content-Type', 'application/json');
return $response;

It will produce this json file:

{"markers":[{"latitude":"36.80610237001900","longitude":"10.17517220741000","title":"Mon annonce"}]}

Comments

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.