2

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"
   }
]}
3
  • decode into an array, use a for loop, push into new container array, encode it again Commented Feb 3, 2016 at 2:46
  • I not really understanding can you show me example? Commented Feb 3, 2016 at 2:52
  • most likely you'll need to use json_encode and json_decode, a for loop also Commented Feb 3, 2016 at 3:06

5 Answers 5

1

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"
        }
    ]
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, i get the result i want.
@hherger : $tarray = json_decode($sjson);//Its giving error here you have to use object array like this $tarray =(object) json_decode($sjson);
@Vihaysinh: json_decode() usually returns a Json object per default. Otherwise the syntax $tarray->name[...] would be erroneous. What exactly is the error message you are receiving?
1

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

?>

3 Comments

Hi, i cannot get the result. Server error was shown.
my data is retrieved from database using array then use json_encode before echo out, so is this the problem cause it got error?
What is the error exactly? Please update your code with the error message and relevant code.
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;

Comments

0

Simply you can use array_map like as

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

So your code looks like as

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

Output:

{
    "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"
        ]
    ]
}

Comments

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

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.