Suraj Roy Suraj Roy - 1 year ago 94
JSON Question

How to convert numerically indexed array to associative array

I have this


how to convert it to


or convert it to vice versa

Answer Source
$arr = json_decode('{"1":{"name":"cat"},"2":{"name":"elephant"}}');
$result = array();
foreach($arr as $item) {
    $data = ['name' => $item->name];
    $result[] = $data;

$json = json_encode($result);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download