1

I want to create JSON Data similar to below format :

 {
    "data": [
        {
            "varient": {
                "0": "12121221",
                "1": "22122111"
            },
            "site": "google",
            "click": "yes"
        },
        {
            "varient": {
                "0": "33333",
                "1": "443434"
            },
            "site": "yahoo",
            "click": "no"
        }
    ]
}

I know json_encode use for creating json

but I dont how to create above json format like foreach and array_merge etc [PHP CODE]

My PHP Code

$datalist = array();
$datalist[] = array("site" => "google","click" => "yes");
$datalist[] = array("site" => "yahoo" ,"click" => "no" );
$fulljson=array_merge($datalist);
$return = array('data'=>$fulljson);
echo json_encode ($return);

But How can i insert varient data

varient":{"0":"12121221","1":"22122111"}

7
  • So just create the structure as you want. Commented Feb 28, 2014 at 8:14
  • json_encode handles multi-dimensional arrays, you don't need to do any for loops Commented Feb 28, 2014 at 8:16
  • Here's a trick; var_export(json_decode($json, true)); to see how you can recreate that array structure in php. Commented Feb 28, 2014 at 8:16
  • @anurupr check now edited question Commented Feb 28, 2014 at 8:23
  • @satish sharma why are you consider as duplicate ? this is different json format Commented Feb 28, 2014 at 8:25

6 Answers 6

2

Something like this if I understood code correctly :

$varient["0"] = 12121221;
$varient["1"] = 22122111;

$data["varient"] = $varient;
$data["site"] = "google";
$data["click"] = "yes";

$result["data"][]=$data;


$varient["0"] = 33333;
$varient["1"] = 443434;

$data["varient"] = $varient;
$data["site"] = "yahoo";
$data["click"] = "no";

$result["data"][]=$data;

echo json_encode($result);
0

Use PHP's json_encode, like this:

<?php
$arr = array(
    array(
        "region" => "valore",
        "price" => "valore2"
    ),
    array(
        "region" => "valore",
        "price" => "valore2"
    ),
    array(
        "region" => "valore",
        "price" => "valore2"
    )
);

echo json_encode($arr);
?>
0

You just want to use

json_encode(array('data'=>$your array name));
3
  • @Melody also specify from where did you fetching those data so that we can can explain you. Commented Feb 28, 2014 at 8:17
  • Check now edited post....I am fetch varient variable using Foreach loop Commented Feb 28, 2014 at 8:33
  • I think you must want to use this type of code it will do your work echo json_encode(array('data'=>$fulljson)); Commented Feb 28, 2014 at 8:38
0

If I understand your question, you want to iterate over the JSON data. Use

json_decode($json, true) 

to convert the json into a PHP array.

0

Use json_decode:

$json = '{ "data": [ { "varient": { "0": "12121221", "1": "22122111" }, "site": "google", "click": "yes" }, { "varient": { "0": "33333", "1": "443434" }, "site": "yahoo", "click": "no" } ] }';

$object = json_decode($json);

echo $json->data[0]->varient;

Not 100% sure if this is working but it should, try it and tell me.

1
  • thnx for replay ..but how can combine all data using foreach and array_merger Commented Feb 28, 2014 at 8:27
0

You need to set the varient index in $datalist after populating the varient array

     $varient = array();
     // set the array using your for loop
     ....
         $varient[] = array('0'=>'value1','1'=>'value2');
     ....

     $datalist = array();

since you don't have a for loop in your question

     // set each index of $datalist with the appropriate index of $varient array
     $datalist[] = array("site" => "google","click" => "yes",'varient',$varient[0]);
     $datalist[] = array("site" => "yahoo" ,"click" => "no" ,'varient',$varient[1]);

     $fulljson=array_merge($$datalist);
     $return = array('data'=>$fulljson);
     echo json_encode ($return);
1
  • i suggest you use a for loop for setting $datalist . it will make the code more readable and decrease the number of lines as well. Commented Feb 28, 2014 at 8:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.