how to split JSON array in php

时间:2016-02-03 02:42:10

标签: php arrays json

I manage to get the data I want from database using array_push and encode it into JSON in PHP. The results I get are like below,

{
"name":[
    "Lucky Draw Ticket",
    "KIP Voucher RM10",
    "KIP Voucher RM20",
    "KIP Voucher RM50"
],
"image":[
    "\/l\/u\/lucky_draw_ticket_1.jpg",
    "\/c\/a\/cash-voucher.jpg",
    "\/c\/a\/cash-voucher2.jpg",
    "\/c\/a\/cash-voucher50_1.jpg"
],
"price":[
    "50.0000",
    "1500.0000",
    "2500.0000",
    "5000.0000"
]}

But now I want to separate it into something like below, but I don't know how to do.

{
"catalog":[
   {
   "name": "Lucky Draw Ticket",
   "image": "\/l\/u\/lucky_draw_ticket_1.jpg",
   "price": "50.0000"
   },
   {
   "name": "KIP Voucher RM10",
   "image": "\/c\/a\/cash-voucher.jpg",
   "price": "1500.0000"
   },
   {
   "name": "KIP Voucher RM20",
   "image": "\/c\/a\/cash-voucher2.jpg",
   "price": "2500.0000"
   },
   {
   "name": "KIP Voucher RM20",
   "image": "\/c\/a\/cash-voucher50_1.jpg"
   "price": "5000.0000"
   }
]}

5 个答案:

答案 0 :(得分:0)

An example:

<?php

$data = json_decode('{
"name":[
    "Lucky Draw Ticket",
    "KIP Voucher RM10",
    "KIP Voucher RM20",
    "KIP Voucher RM50"
],
"image":[
    "\/l\/u\/lucky_draw_ticket_1.jpg",
    "\/c\/a\/cash-voucher.jpg",
    "\/c\/a\/cash-voucher2.jpg",
    "\/c\/a\/cash-voucher50_1.jpg"
],
"price":[
    "50.0000",
    "1500.0000",
    "2500.0000",
    "5000.0000"
]}', true);

$result = [
    'catalog' => [
    ]
];

for ($i=0; $i<=count($data); $i++) {
    $temp = [];
    array_push($temp, $data['name'][$i]);
    array_push($temp, $data['image'][$i]);
    array_push($temp, $data['price'][$i]);
    array_push($result['catalog'], $temp);
}


?>

<pre>
    <?php
        print_r($result);
    ?>
</pre>

<?php 

// Convert back to JSON
$json = json_encode($result);

?>

答案 1 :(得分:0)

Try this..

$str = '{
"name":[
    "Lucky Draw Ticket",
    "KIP Voucher RM10",
    "KIP Voucher RM20",
    "KIP Voucher RM50"
],
"image":[
    "\/l\/u\/lucky_draw_ticket_1.jpg",
    "\/c\/a\/cash-voucher.jpg",
    "\/c\/a\/cash-voucher2.jpg",
    "\/c\/a\/cash-voucher50_1.jpg"
],
"price":[
    "50.0000",
    "1500.0000",
    "2500.0000",
    "5000.0000"
]}';
$jsonArr  = json_decode($str, true);

//create array in needed format
$finalArr = array();
foreach ($jsonArr AS $key => $dataArr) {
    $count = count($dataArr);
    for ($indx = 0; $indx < $count; $indx++) {
        $finalArr['catalog'][$indx][$key] = $dataArr[$indx];
    }
}

//output
$jsonFinal = json_encode($finalArr, true);
echo $jsonFinal;

答案 2 :(得分:0)

Try this:

  • create a Json object from the Json string,
  • reorganize the object into a temporary PHP array,
  • create a Json object (string) from the PHP arrray.

Code

<?php
    $sjson='{
"name":[
    "Lucky Draw Ticket",
    "KIP Voucher RM10",
    "KIP Voucher RM20",
    "KIP Voucher RM50"
],
"image":[
    "\/l\/u\/lucky_draw_ticket_1.jpg",
    "\/c\/a\/cash-voucher.jpg",
    "\/c\/a\/cash-voucher2.jpg",
    "\/c\/a\/cash-voucher50_1.jpg"
],
"price":[
    "50.0000",
    "1500.0000",
    "2500.0000",
    "5000.0000"
]}';
$tarray = json_decode($sjson);
$newarray = array('catalog'=>array());
foreach($tarray->name as $ix => $name) {
    $newarray['catalog'][]=array(
        'name'  => $tarray->name[$ix],
        'image' => $tarray->image[$ix],
        'price' => $tarray->price[$ix],
    );
}
$sjson = json_encode($newarray);
// echo '<pre>'; var_dump($sjson); echo '</pre>';
?>

Result

{
    "catalog":[
        {
            "name":"Lucky Draw Ticket",
            "image":"\/l\/u\/lucky_draw_ticket_1.jpg",
            "price":"50.0000"
        },
        {
            "name":"KIP Voucher RM10",
            "image":"\/c\/a\/cash-voucher.jpg",
            "price":"1500.0000"
        },
        {
            "name":"KIP Voucher RM20",
            "image":"\/c\/a\/cash-voucher2.jpg",
            "price":"2500.0000"
        },
        {
            "name":"KIP Voucher RM50",
            "image":"\/c\/a\/cash-voucher50_1.jpg",
            "price":"5000.0000"
        }
    ]
}

答案 3 :(得分:0)

Use this code to get exact result what you expect..

$string = your json string.

$json_decode = json_decode($string,true);
$temp = array();
$i = 0;
foreach($json_decode as $key=>$val){
        $temp['catalog'][$i]['name'] = $json_decode['name'][$i];
        $temp['catalog'][$i]['image'] = $json_decode['image'][$i];
        $temp['catalog'][$i]['price'] = $json_decode['price'][$i];
        $i++;
}
echo "<pre>";
print_r($temp);
print_r(json_encode($temp));

答案 4 :(得分:0)

只需将array_map视为

即可
array_map(null,$jsonArr['name'],$jsonArr['image'],$jsonArr['price']);

所以你的代码看起来像

$jsonArr = json_decode($json, true);
$result['catalog'] = array_map(null,$jsonArr['name'],$jsonArr['image'],$jsonArr['price']);
echo json_encode($result);

输出:

{
    "catalog": [
        [
            "Lucky Draw Ticket",
            "\/l\/u\/lucky_draw_ticket_1.jpg",
            "50.0000"
        ],
        [
            "KIP Voucher RM10",
            "\/c\/a\/cash-voucher.jpg",
            "1500.0000"
        ],
        [
            "KIP Voucher RM20",
            "\/c\/a\/cash-voucher2.jpg",
            "2500.0000"
        ],
        [
            "KIP Voucher RM50",
            "\/c\/a\/cash-voucher50_1.jpg",
            "5000.0000"
        ]
    ]
}