Shihas
Shihas

Reputation: 814

Array to JSON using PHP in Codeigniter

I have an array like below:

 Array ( [0] => Array ( [SI] => 1 
                        [name] => Nick 
                        [location] => Russia
                        [year] => 2011 ) 
        [1] => Array ( [SI] => 8 
                       [name] => Mike 
                       [location] => Russia 
                       [year] => 2011 )
        [2] => Array ( [SI] => 2 
                       [name] => Tom 
                       [location] => Russia 
                       [year] => 2010 )
        [3] => Array ( [SI] => 6 
                       [name] => Duke 
                       [location] => Russia 
                       [year] => 2010 ) )     

Current JSON format:

 {
"name": "Amalians",
"img": "https:\/\/dl.dropboxusercontent.com\/u\/19954023\/marvel_force_chart_img\/marvel.png",
"children": [
    {
        "name": "2011"
    },
    {
        "children": [
            {
                "SI": "1"
            }
        ]
    },
    {
        "children": [
            {
                "name": "Nick"
            }
        ]
    },
    {
        "children": [
            {
                "location": "Russia"
            }
        ]
    },
    {
        "name": "2011"
    },
    {
        "children": [
            {
                "SI": "8"
            }
        ]
    },
    {
        "children": [
            {
                "name": "Mike"
            }
        ]
    },
    {
        "children": [
            {
                "location": "Russia"
            }
        ]
    },
    {
        "name": "2010"
    },
    {
        "children": [
            {
                "SI": "2"
            }
        ]
    },
    {
        "children": [
            {
                "name": "Tom"
            }
        ]
    },
    {
        "children": [
            {
                "location": "Russia"
            }
        ]
    },
    {
        "name": "2010"
    },
    {
        "children": [
            {
                "SI": "6"
            }
        ]
    },
    {
        "children": [
            {
                "name": "Duke"
            }
        ]
    },
    {
        "children": [
            {
                "location": "Russia"
            }
        ]
    }
  ]
}

Desired JSON format:

 {
   "name": "marvel",
   "img": "https://dl.dropboxusercontent.com/u/19954023/marvel_force_chart_img/marvel.png",
   "children": [
    {
       "name": "2011",
       "children": [
        {
           "SI": "1",
           "name": "Nick", 
           "location": "Russia"
        },
        {
           "SI": "8",
           "name": "Mike", 
           "location": "Russia"
        }
       ]
     },
     {
       "name": "2010",
       "children": [
        {
           "SI": "2",
           "name": "Tom", 
           "location": "Russia"
        },
        {
           "SI": "6",
           "name": "Duke", 
           "location": "Russia"
        }
       ]
      }
    ]
 }    

CODE:

    $data['name'] = "Amalians";
    $data['img'] = "https://dl.dropboxusercontent.com/u/19954023/marvel_force_chart_img/marvel.png";

    foreach($people as $row)
    {

            $data['children'][]['name'] = $row['year'];
            $data['children'][]['children'][]['SI'] = $row['SI'];
            $data['children'][]['children'][]['name'] = $row['name'];
            $data['children'][]['children'][]['location'] = $row['location'];
    }
    echo "<pre>";
    echo json_encode($data,JSON_PRETTY_PRINT);
    echo "</pre>"; exit();     

NOTE: $people is the array defined above.
Please kindly help me to do this. I have been working on this for last two days and till this moment I couldn't find any solution. Thanks

Upvotes: 2

Views: 769

Answers (3)

Ashu
Ashu

Reputation: 1320

    <?php
    $array =  Array(Array( "SI" => 1, 
                            "name" => Nick, 
                            "location" => Russia,
                            "year" => 2011), 
                    Array ( "SI" => 8, 
                           "name" => Mike ,
                           "location" => Russia ,
                           "year" => 2011),
                    Array ( "SI" => 2, 
                           "name" => Tom, 
                           "location" => Russia, 
                           "year" => 2010 ),
                    Array ( "SI" => 6, 
                           "name" => Duke, 
                           "location" => Russia ,
                           "year" => 2010 ) );
    $json_final['child'] = array();
    $temp = array();
    foreach($array as $arr)
    {
        $flag=0;
        if(!empty($json_final['child']))
        {
            foreach($json_final['child'] as $name_data)
            {
                if($name_data['name'] == $arr['year'])
                {
                    $flag=1;

array_push($name_data['child_sub'],array("SI"=>$arr['SI'],"name"=>$arr['name'],"location"=>$arr['location']));
                    array_push($temp, $name_data);
                }
            }
        }
        if($flag == 0)
        {
            array_push($json_final['child'],array("name"=>$arr['year'],"child_sub"=>array(array("SI"=>$arr['SI'],"name"=>$arr['name'],"location"=>$arr['location']))));
        }   
    }
    $json_final['child'] = $temp;
    //print_r($json_final);
    echo json_encode($json_final,true)

    ?>

Upvotes: 0

Atural
Atural

Reputation: 5439

i guess the simplest way is to group your data

$arrData = [
    [
        "SI" => 1,
        "name" => "Nick",
        "location" => "Russia",
        "year" => 2011
    ],
    [
        "SI" => 2,
        "name" => "Mike",
        "location" => "Russia",
        "year" => 2011
    ],
    [
        "SI" => 3,
        "name" => "Tom",
        "location" => "Russia",
        "year" => 2010
    ],
    [
        "SI" => 4,
        "name" => "Duke",
        "location" => "Russia",
        "year" => 2010
    ],
];

$arrGroupedData = [];


foreach($arrData AS $row)
{
    $arrGroupedData[$row['year']][] = [ "SI" => $row['SI'],  "name" => $row['name'], "location" => $row['location']];

}

$arrGroupFormattedData = [];

foreach($arrGroupedData AS $key => $arrGroup)
{
    $arrGroupFormattedData[] = ["name" => $key, "children" => $arrGroup];
}

$data = [
    "name" => "Amalians",
    "img" => "https://dl.dropboxusercontent.com/u/19954023/marvel_force_chart_img/marvel.png",
    "children" => $arrGroupFormattedData
];

echo json_encode($data,JSON_PRETTY_PRINT);

Upvotes: 1

Ravi Sachaniya
Ravi Sachaniya

Reputation: 1633

You can try this:

Here you need to group the array by key year.

$array = array();
foreach ($people as $val) {
    $array[$val['year']][] = $val;
}

$data['name'] = "Amalians";
$data['img'] = "https://dl.dropboxusercontent.com/u/19954023/marvel_force_chart_img/marvel.png";

$i = 0;
foreach ($array as $key => $value) {
    foreach ($value as $key2 => $value2) {
        $data['children'][$i]['name'] = $value2['year'];
        $data['children'][$i]['children'][$key2]['SI'] = $value2['SI'];
        $data['children'][$i]['children'][$key2]['name'] = $value2['name'];
        $data['children'][$i]['children'][$key2]['location'] = $value2['location'];
    }
    $i++;
}

echo "<pre>";
echo json_encode($data, JSON_PRETTY_PRINT);
echo "</pre>";
exit();

Upvotes: 1

Related Questions