Json 数组到数组
Json array to array
我有一个来自 laravel 的 JSON 数组,如下所示:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[_id] => MongoDB\BSON\ObjectID Object
(
[oid] => 5a15e52b5bd98b7a0040fac8
)
[UnitPrice] => 18
[UnitsInStock] => 39
)
[1] => Array
(
[_id] => MongoDB\BSON\ObjectID Object
(
[oid] => 5a15e52b5bd98b7a0040fac9
)
[UnitPrice] => 19
[UnitsInStock] => 17
)
[2] => Array
(
[_id] => MongoDB\BSON\ObjectID Object
(
[oid] => 5a15e52b5bd98b7a0040faca
)
[UnitPrice] => 10
[UnitsInStock] => 13
)
)
)
如何只将UnitPrice
和UnitsInStock
作为数组?有没有不用循环的方法?
我想这样使用它:
...
->dataset('UnitPrice', [5,20,100,...]) // array's values = [5,20,100,...]
->dataset('UnitsInStock', [52,120,100,...]) // array's values = [52,120,100,...]
Laravel 有一个 lists method to get array for a given column. Otherwise, you can use the array_column 方法 PHP。
$unitPrices = array_column($items, 'UnitPrice');
我有一个来自 laravel 的 JSON 数组,如下所示:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[_id] => MongoDB\BSON\ObjectID Object
(
[oid] => 5a15e52b5bd98b7a0040fac8
)
[UnitPrice] => 18
[UnitsInStock] => 39
)
[1] => Array
(
[_id] => MongoDB\BSON\ObjectID Object
(
[oid] => 5a15e52b5bd98b7a0040fac9
)
[UnitPrice] => 19
[UnitsInStock] => 17
)
[2] => Array
(
[_id] => MongoDB\BSON\ObjectID Object
(
[oid] => 5a15e52b5bd98b7a0040faca
)
[UnitPrice] => 10
[UnitsInStock] => 13
)
)
)
如何只将UnitPrice
和UnitsInStock
作为数组?有没有不用循环的方法?
我想这样使用它:
...
->dataset('UnitPrice', [5,20,100,...]) // array's values = [5,20,100,...]
->dataset('UnitsInStock', [52,120,100,...]) // array's values = [52,120,100,...]
Laravel 有一个 lists method to get array for a given column. Otherwise, you can use the array_column 方法 PHP。
$unitPrices = array_column($items, 'UnitPrice');