Baspa
Baspa

Reputation: 1168

Convert JSON data from multiple nested array to PHP variables

I have a JSON array in an array:

{
 layouts: [
    {
      w: 6,
      h: 4,
      x: 0,
      y: 0,
      i: "n0",
      minW: 1.5,
      minH: 1,
      maxH: 1000,
      moved: false,
      static: false,
      widget: "Clock"
    },
    {
      w: 2,
      h: 2,
      x: 0,
      y: 4,
      i: "n1",
      minW: 1,
      minH: 1,
      maxH: 1000,
      moved: false,
      static: false,
      widget: "Weather"
     }
  ]
}

I need to store each widget layout in a database. When I'm trying to execute the query It returns an error that the values are empty. I tried some examples from here but they don't work for me.

function:

public function store(Request $request)
{

    $JSON = json_decode($request);

    foreach (array($JSON) as $data) {

        $i = 0;

        $w = $data[$i]['w'];
        $h = $data[$i]['h'];
        $x = $data[$i]['x'];
        $y = $data[$i]['y'];
        $i = $data[$i]['i'];
        $minW = $data[$i]['minW'];
        $minH = $data[$i]['minH'];
        $maxH = $data[$i]['maxH'];
        $moved = $data[$i]['moved'];
        $static = $data[$i]['static'];
        $type = $data[$i]['type'];

        DB::table('widgets')->insert(
            ['w' => $w,
             'h' => $h,
             'x' => $x,
             'y' => $y,
             'i' => $i,
             'minW' => $minW,
             'minH' => $minH,
             'maxH' => $maxH,
             'moved' => $moved,
             'static' => $static,
             'type_widget' => $type]
        );

        $i++;

    }

    return response()->json(201);
}

Could anyone tell me what is the correct way to loop through the data to save it?

Upvotes: 0

Views: 106

Answers (3)

sietse85
sietse85

Reputation: 1506

When you succeed with Sven Hakvoorts answer, here is a shorter version of your foreach

$i = 0;
$data = json_decode($request, true);
while (isset($data['layouts'][$i])) {
    DB::table('widgets')->insert($data['layouts'][$i]);
    $i++;
}

Even shorter (and best way):

foreach ($data['layouts'] as $row) {
    DB::table('widgets')->insert($row);
}

I think you will have to rename just one column to make this short code work: type_widget to type, this way your fields and column names are equal which results in a lot shorter code as you can see.

Upvotes: 1

Sven Hakvoort
Sven Hakvoort

Reputation: 3621

You need to use json_decode($array, true) the second argument (true) indicates that the input string needs to be converted to an associative array, resulting in the correct parsing of your JSON.

You can also extract the JSON directly from the Request with $request->all(). I would recommend this approach as this is a lot easier and the array will always be correctly parsed. Decoding the Request object could result in a different array formatting than you originally submitted (contain data of the Request object itself in addition to your data).

Upvotes: 3

sangamesh
sangamesh

Reputation: 21

1.

$JSON = json_decode($request);

change it to

$JSON = json_decode($request,true);

2. remove $i=0 from foreach loop and place it at befor foreach loop

Upvotes: 0

Related Questions