PHP-json格式的轉換

在做一個專案下碰到兩個json格式,

  1. 一個是物件:呼叫的方式是$data->key

  2. 另一個是陣列:呼叫的方式是$data['key']

一個專案最好只要有一種格式就好,如果出現兩種,就必須把一個轉成另一個,我是習慣轉成陣列格式。

$data = (array)$data; //這樣就可以強制轉成陣列



// convert json to array
$array = json_decode($json, true);
//  create a new collection instance from the array
collect($array);

Last updated

Was this helpful?