1

I have a json format and want to convert this to my customized format. Please help me for this conversion using PHP. Please help me out, how can i construct the above mentioned JSON array format. Here is my format:

{
"success": true,
"code": 601,
"u_id": "9",
"h_id": "5",
"data": [{
    "mp_cat": "Conveyance",
    "mp_id": "5",
    "mp_rating": "4",
    "mp_price": "630.00"
}, {
    "mp_cat": "Food & Drinks",
    "mp_id": "3",
    "mp_rating": "4",
    "mp_price": "104.55"
}, {
    "mp_cat": "Food & Drinks",
    "mp_id": "4",
    "mp_rating": "3",
    "mp_price": "450.00"
}]
}

And want this kind of format:

 {
"success": true,
"code": 601,
"u_id": "9",
"h_id": "5",
"category": [{
    "Name": "Food & Drinks",
    "data": [{
        "mp_cat": "Food & Drinks",
        "mp_id": "3",
        "mp_rating": "4",
        "mp_price": "104.55"
    }, {
        "mp_cat": "Food & Drinks",
        "mp_id": "4",
        "mp_rating": "3",
        "mp_price": "450.00"
    }]
}, {
    "Name": "Conveyance",
    "data": [{
        "mp_cat": "Conveyance",
        "mp_id": "5",
        "mp_rating": "4",
        "mp_price": "630.00"
    }]
}]
}
3
  • From where you are getting this json? Commented Jan 20, 2016 at 7:08
  • getting form a mysql query Commented Jan 20, 2016 at 7:13
  • Will you share that code @deepakbhardwaj Commented Jan 20, 2016 at 7:14

1 Answer 1

1

you can use simple foreach loop to construct your data, or reformat json. Halve a look on below solution:

$json = '{
"success": true,
"code": 601,
"u_id": "9",
"h_id": "5",
"data": [{
    "mp_cat": "Conveyance",
    "mp_id": "5",
    "mp_rating": "4",
    "mp_price": "630.00"
}, {
    "mp_cat": "Food & Drinks",
    "mp_id": "3",
    "mp_rating": "4",
    "mp_price": "104.55"
}, {
    "mp_cat": "Food & Drinks",
    "mp_id": "4",
    "mp_rating": "3",
    "mp_price": "450.00"
}]
}';

$json_array = json_decode($json, true);
$new_array = array();
$category_array = array();
foreach($json_array as $key => $array){
    if($key == 'data'){
        foreach($array as $a){
            $category_array[$a['mp_cat']]['name']= $a['mp_cat'];
            $category_array[$a['mp_cat']]['data'][]= $a;
        }
    } else{
        $new_array[$key] = $array;
    }
}

$new_array['category'] = array_values($category_array);
echo json_encode($new_array);

Output:

{
    "success": true,
    "code": 601,
    "u_id": "9",
    "h_id": "5",
    "category": [{
        "name": "Conveyance",
        "data": [{
            "mp_cat": "Conveyance",
            "mp_id": "5",
            "mp_rating": "4",
            "mp_price": "630.00"
        }]
    }, {
        "name": "Food & Drinks",
        "data": [{
            "mp_cat": "Food & Drinks",
            "mp_id": "3",
            "mp_rating": "4",
            "mp_price": "104.55"
        }, {
            "mp_cat": "Food & Drinks",
            "mp_id": "4",
            "mp_rating": "3",
            "mp_price": "450.00"
        }]
    }]
}
Sign up to request clarification or add additional context in comments.

3 Comments

How to convert this JSON [{"label":"Food & Drinks","data":"2"},{"label":"Lifestyle","data":"1"}] to [["Food & Drinks", 2],["Lifestyle", 1]] using php
please post a new question on SO

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.