如何将二维数组转换为 collection laravel?
How can I convert array two dimensional to collection laravel?
我有这样的数组:
$test = array(
array(
'name' => 'Christina',
'age' => '25'
),
array(
'name' => 'Agis',
'age' => '22'
),
array(
'name' => 'Agnes',
'age' => '30'
)
);
我想改成collectionlaravel
我这样试:
collect($test)
结果并不完美。还有一个数组
我该如何解决这个问题?
collect($test)
不会将 $test
转换为集合,它 returns $test
作为集合。您需要为新变量使用它的 return 值,或覆盖现有变量。
$test = collect($test);
如果您想将单个项目转换为对象(而不是数组),就像您在下面的评论中指出的那样,那么您将需要转换它们。
$test = collect($test)->map(function ($item) {
return (object) $item;
});
分享更多的光。
集合是 "macroable",它允许您在 运行 时向集合 class 添加其他方法。根据 Laravel 对集合的解释。数组可以是维度的。使用 map 函数扩展您的集合以将子数组转换为对象
$test = array(
array(
'name' => 'Christina',
'age' => '25'
),
array(
'name' => 'Agis',
'age' => '22'
),
array(
'name' => 'Agnes',
'age' => '30'
)
);
// can be converted using collection + map function
$test = collect($test)->map(function($inner_child){
return (Object) $inner_child;
});
This will cast the inner child array into Object.
我知道已经有一段时间了,但我在 laracast 上找到了这个答案,它似乎更好地解决了这个问题,因为它使它递归。
我从 https://gist.github.com/brunogaspar/154fb2f99a7f83003ef35fd4b5655935 github 获得的这个解决方案非常有效。
\Illuminate\Support\Collection::macro('recursive', function () {
return $this->map(function ($value) {
if (is_array($value) || is_object($value)) {
return collect($value)->recursive();
}
return $value;
});
});
比你更喜欢:
$data = [
[
'name' => 'John Doe',
'emails' => [
'john@doe.com',
'john.doe@example.com',
],
'contacts' => [
[
'name' => 'Richard Tea',
'emails' => [
'richard.tea@example.com',
],
],
[
'name' => 'Fergus Douchebag', // Ya, this was randomly generated for me :)
'emails' => [
'fergus@douchebag.com',
],
],
],
],
];
$collection = collect($data)->recursive();
我有这样的数组:
$test = array(
array(
'name' => 'Christina',
'age' => '25'
),
array(
'name' => 'Agis',
'age' => '22'
),
array(
'name' => 'Agnes',
'age' => '30'
)
);
我想改成collectionlaravel
我这样试:
collect($test)
结果并不完美。还有一个数组
我该如何解决这个问题?
collect($test)
不会将 $test
转换为集合,它 returns $test
作为集合。您需要为新变量使用它的 return 值,或覆盖现有变量。
$test = collect($test);
如果您想将单个项目转换为对象(而不是数组),就像您在下面的评论中指出的那样,那么您将需要转换它们。
$test = collect($test)->map(function ($item) {
return (object) $item;
});
分享更多的光。
集合是 "macroable",它允许您在 运行 时向集合 class 添加其他方法。根据 Laravel 对集合的解释。数组可以是维度的。使用 map 函数扩展您的集合以将子数组转换为对象
$test = array(
array(
'name' => 'Christina',
'age' => '25'
),
array(
'name' => 'Agis',
'age' => '22'
),
array(
'name' => 'Agnes',
'age' => '30'
)
);
// can be converted using collection + map function
$test = collect($test)->map(function($inner_child){
return (Object) $inner_child;
});
This will cast the inner child array into Object.
我知道已经有一段时间了,但我在 laracast 上找到了这个答案,它似乎更好地解决了这个问题,因为它使它递归。 我从 https://gist.github.com/brunogaspar/154fb2f99a7f83003ef35fd4b5655935 github 获得的这个解决方案非常有效。
\Illuminate\Support\Collection::macro('recursive', function () {
return $this->map(function ($value) {
if (is_array($value) || is_object($value)) {
return collect($value)->recursive();
}
return $value;
});
});
比你更喜欢:
$data = [
[
'name' => 'John Doe',
'emails' => [
'john@doe.com',
'john.doe@example.com',
],
'contacts' => [
[
'name' => 'Richard Tea',
'emails' => [
'richard.tea@example.com',
],
],
[
'name' => 'Fergus Douchebag', // Ya, this was randomly generated for me :)
'emails' => [
'fergus@douchebag.com',
],
],
],
],
];
$collection = collect($data)->recursive();