laravel 使用 foreach 创建多维 StdClass 对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19204551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create multi-dimensional StdClass Object array with foreach
提问by Nicekiwi
I am trying to create a multi-dimentional StdClass Object, but the output is always from the last loop of the first and second foreach loop, not a collection of all the loops.
我正在尝试创建一个多维 StdClass 对象,但输出始终来自第一个和第二个 foreach 循环的最后一个循环,而不是所有循环的集合。
Each day should have 3 $exercises inside it. And there's 5 days, but only 1 day with 1 exercise show up.
每天应该有 3 个 $exercises 里面。并且有 5 天,但只有 1 天出现 1 个练习。
Function & current output: http://paste.laravel.com/WIU
功能和电流输出:http: //paste.laravel.com/WIU
回答by Adrien Delessert
It looks like what's happening is that you're overwriting the days
attribute of your data
object each time you loop. Instead of a stdClass
, $data->days
should be an array, and then you should add stdClass
objects describing each day to that array...something like this (using part of your code from around line 14):
看起来发生的事情是每次循环时都覆盖对象的days
属性data
。而不是 a stdClass
,$data->days
应该是一个数组,然后你应该将stdClass
描述每一天的对象添加到该数组中......像这样的东西(使用你的第 14 行左右的部分代码):
$data->days = array(); //create the array
foreach ($jsonDays as $day)
{
$newDay = new stdClass(); //create a day object
$newDay = $day->day; //add things to the day object
...
$data->days[] = $newDay; //push the day object onto your day array.
The same approach would also work for adding multiple exercises to each day.
同样的方法也适用于每天添加多个练习。
回答by yahka mba
There is no special need for stdObjects when simple objects will serve. Here is a simple multi-dimensional object structure for storing arrays of data.
当使用简单对象时,没有特别需要 stdObjects。这是一个简单的多维对象结构,用于存储数据数组。
<?php
//create exercise array: 3 exercise rows x 5 day columns
$ex[0] = ["aa", "ab", "ac", "ad", "ae"];
$ex[1] = ["ba", "bb", "bc", "bd", "be"];
$ex[2] = ["ca", "cb", "cc", "cd", "ce"];
//create your day class for the 3 exercises
class day{
public $ex0;
public $ex1;
public $ex2;
}
//create your period (days) class for all the days
class days{
public $days;
}
//create objects for each day of exercises and store the exercises
for($i=0;$i<count($ex[0]);$i++){ //for each of 5 days
$day[$i] = new day();
$day[$i]->ex0 = $ex[0][$i];//1st exercise of the i_th day
$day[$i]->ex1 = $ex[1][$i];//2nd exercise of the i_th day
$day[$i]->ex2 = $ex[2][$i];//3rd exercise of the i_th day
}
//create an object for all the data
$days = new days;
//store the array of day objects with their data in the days object
$days->days = $day;
//confirm object creation and structure
print_r($days);
//check the json_encode result
echo "<br><br>" . (json_encode($days)) . "<br>";
?>
Result:
结果:
days Object ( [days] => Array (
[0] => day Object ( [ex0] => aa [ex1] => ba [ex2] => ca )
[1] => day Object ( [ex0] => ab [ex1] => bb [ex2] => cb )
[2] => day Object ( [ex0] => ac [ex1] => bc [ex2] => cc )
[3] => day Object ( [ex0] => ad [ex1] => bd [ex2] => cd )
[4] => day Object ( [ex0] => ae [ex1] => be [ex2] => ce ) ) )
{"days":[{"ex0":"aa","ex1":"ba","ex2":"ca"},
{"ex0":"ab","ex1":"bb","ex2":"cb"},{"ex0":"ac","ex1":"bc","ex2":"cc"},
{"ex0":"ad","ex1":"bd","ex2":"cd"},{"ex0":"ae","ex1":"be","ex2":"ce"}]}