[英]build dynamic associative multi-dimensional array
I have seen a lot of questions regarding multi-dimensional arrays, and how to build them, and whilst trying some of the answers I still get an error. 我已经看到了很多有关多维数组,如何构建它们的问题,并且在尝试某些答案的同时仍然遇到错误。 So I am thinking what I am trying to do is not really possible, and perhaps I should use JSON instead?
因此,我认为我尝试做的事情实际上是不可能的,也许我应该改用JSON?
I have data from a source which is returned as a JSON object. 我有来自JSON对象返回的数据。 It has lots data in it, of which i only need a small amount.
它有很多数据,我只需要少量。 Now I need it sorted into dates:
现在我需要将其分类为日期:
$temp_array = array();
foreach ($data as $booking_id => $booking) {
$setdate = '';
$b = $this->getBooking($booking_id);
foreach ($b as $k => $v) {
$setdate = date('d-m-Y', $booking['start_date']);
if(!in_array($setdate, $temp_array)) {
$temp_array = array('date' => $setdate);
}
$temp_array['date'][] = array($k => $v);
}
}
I need the date to be the first node of the array, and the data to be in the second node. 我需要日期为数组的第一个节点,而数据为第二个节点。 When a new date is found in the iteration, it creates another new node:
在迭代中找到新日期时,它将创建另一个新节点:
[0] => 1/10/16
[0] => array( ['key'] =>['value'])
[1] => array( ['key'] =>['value'])
[2] => array( ['key'] =>['value'])
[1] => 7/10/16
[0] => array( ['key'] =>['value'])
[1] => array( ['key'] =>['value'])
[2] => array( ['key'] =>['value'])
[2] => 14/10/16
[0] => array( ['key'] =>['value'])
[1] => array( ['key'] =>['value'])
[2] => array( ['key'] =>['value'])
[3] => 21/10/16
[0] => array( ['key'] =>['value'])
[1] => array( ['key'] =>['value'])
[2] => array( ['key'] =>['value'])
I seem unable to get anywhere with it as the errors I get refer to [] operator not supported for strings
or Warning: Attempt to modify property of non-object in
depending on what other examples on here I have found. 我似乎无法获得任何结果,因为我所指的错误是
[] operator not supported for strings
或Warning: Attempt to modify property of non-object in
根据此处找到的其他示例Warning: Attempt to modify property of non-object in
。
I think the question here, is that the data is not in any order, so it may populate node [0], and then [3], and then back to [0], and so on. 我认为这里的问题是数据不是按任何顺序排列的,因此它可能会填充节点[0],然后填充[3],然后填充到[0],依此类推。
Any ideas what on earth I am doing wrong ? 任何想法我到底在做什么错?
Thanks 谢谢
Addy 阿迪
Update 更新资料
Using this code, which is the middle foreach: 使用此代码,这是每个步骤的中间内容:
$b = $this->getBooking($booking_id);
foreach ($b as $k => $v) {
$setdate = date('d-m-Y', strtotime($booking['date_desc']));
if(!in_array($setdate, $temp_dates)) {
array_push($temp_dates, $setdate);
$temp_array[]['date'] = $setdate;
}
}
Gives me this result: 给我这个结果:
Array ( [0] => Array ( [date] => 22-10-2016 )
[1] => Array ( [date] => 01-10-2016 )
[2] => Array ( [date] => 08-10-2016 )
[3] => Array ( [date] => 15-10-2016 )
)
Which is correct. 哪个是对的。 So now, I need to add, each time a date arrives which $tem_array has a heading for, add another array:
所以现在,我需要添加一次,每次到达$ tem_array有标题的日期时,添加另一个数组:
Array ( [0] => Array ( [date] => 22-10-2016 ) => [0] => $data
[1] => $data
[2] => $data
[1] => Array ( [date] => 01-10-2016 ) => [0] => $data
[1] => $data
[2] => $data
[2] => Array ( [date] => 08-10-2016 ) => [0] => $data
[3] => Array ( [date] => 15-10-2016 ) => [0] => $data
[1] => $data
)
Hope this explains it a little better. 希望这能更好地解释它。 I have the first bit correct, but when a date is already in there, I need to move up a node.
我的第一点正确,但是当日期已经在那里时,我需要向上移动一个节点。
I think I was over complicating it. 我想我太复杂了。 This is the final code which did want I wanted.
这是我想要的最终代码。
function sortcf($data) {
$temp_array = array();
$temp_dates = array();
foreach ($data as $booking_id => $booking) {
$b = $this->getBooking($booking_id);
$setdate = date('d-m-Y', strtotime($booking['date_desc']));
$temp_array[$setdate][] = ($b);
}
$this->wcg_am_data = $temp_array;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.