我们正在创建待办事项任务。 我们添加了列表项以将所有待办事项添加到一个数组中。 在前面,我们需要收集在一个数组中。 现在我们需要创建单独的数组,例如年份组。 年组插入月份数据。 我的代码在下面。
这是我的PHP数组代码。
array(
array(
'title' => 'Test 2019',
'todo_title' => 'Test 2019',
'todo_strtotime' => '1548892800',
'todo_date' => 'January 31, 2019',
'todo_year' => '2019',
'todo_month' => '1',
'todo_unique_id' => '819097135',
'todo_status' => '0',
),
array(
'title' => 'Test new data',
'todo_title' => 'Test new data',
'todo_strtotime' => '1550620800',
'todo_date' => 'February 20, 2019',
'todo_year' => '2019',
'todo_month' => '2',
'todo_unique_id' => '1833135764',
'todo_status' => '0',
),
array(
'title' => 'Loriem ipsum',
'todo_title' => 'Loriem ipsum',
'todo_strtotime' => '1552953600',
'todo_date' => 'March 19, 2019',
'todo_year' => '2019',
'todo_month' => '3',
'todo_unique_id' => '1501977081',
'todo_status' => '0',
),
array(
'title' => 'asdfasf',
'todo_title' => 'asdfasf',
'todo_strtotime' => '1579910400',
'todo_date' => 'January 25, 2020',
'todo_year' => '2020',
'todo_month' => '1',
'todo_status' => '1',
'todo_unique_id' => '1573546118',
),
array(
'title' => 'Hello world',
'todo_title' => 'Hello world',
'todo_strtotime' => '1580428800',
'todo_date' => 'January 31, 2020',
'todo_year' => '2020',
'todo_month' => '1',
'todo_status' => '1',
'todo_unique_id' => '314611084',
),
array(
'title' => 'Test',
'todo_title' => 'Test',
'todo_strtotime' => '1582156800',
'todo_date' => 'February 20, 2020',
'todo_year' => '2020',
'todo_month' => '2',
'todo_unique_id' => '184291872',
'todo_status' => '0',
),
array(
'title' => 'Hello world',
'todo_title' => 'Hello world',
'todo_strtotime' => '1583452800',
'todo_date' => 'March 6, 2020',
'todo_year' => '2020',
'todo_month' => '3',
'todo_status' => '1',
'todo_unique_id' => '383849492',
)
)
如何检索要收集的数组列表,因为年份数组推送月份数组代码如下。
array(
'2019' => array(
'1' => array(
array(
'title' => 'Test 2019',
'todo_title' => 'Test 2019',
'todo_strtotime' => '1548892800',
'todo_date' => 'January 31, 2019',
'todo_year' => '2019',
'todo_month' => '1',
'todo_unique_id' => '819097135',
'todo_status' => '0',
),
),
'2' => array(
array(
'title' => 'Test new data',
'todo_title' => 'Test new data',
'todo_strtotime' => '1550620800',
'todo_date' => 'February 20, 2019',
'todo_year' => '2019',
'todo_month' => '2',
'todo_unique_id' => '1833135764',
'todo_status' => '0',
),
),
'3' => array(
array(
'title' => 'Loriem ipsum',
'todo_title' => 'Loriem ipsum',
'todo_strtotime' => '1552953600',
'todo_date' => 'March 19, 2019',
'todo_year' => '2019',
'todo_month' => '3',
'todo_unique_id' => '1501977081',
'todo_status' => '0',
),
),
),
'2020' => array(
'1' => array(
array(
'title' => 'asdfasf',
'todo_title' => 'asdfasf',
'todo_strtotime' => '1579910400',
'todo_date' => 'January 25, 2020',
'todo_year' => '2020',
'todo_month' => '1',
'todo_status' => '1',
'todo_unique_id' => '1573546118',
),
),
'2' => array(
array(
'title' => 'Test',
'todo_title' => 'Test',
'todo_strtotime' => '1582156800',
'todo_date' => 'February 20, 2020',
'todo_year' => '2020',
'todo_month' => '2',
'todo_unique_id' => '184291872',
'todo_status' => '0',
),
),
'3' => array(
array(
'title' => 'Hello world',
'todo_title' => 'Hello world',
'todo_strtotime' => '1583452800',
'todo_date' => 'March 6, 2020',
'todo_year' => '2020',
'todo_month' => '3',
'todo_status' => '1',
'todo_unique_id' => '383849492',
)
),
)
)
我如何在 PHP 中像这段代码一样检索。
你可以用简单的foreach来做到这一点
$newarray = array();
foreach($data as $key=>$value){
$newarray[$value['todo_year']][] = $value;
}
print_r($newarray);
完整示例
<?php
$todo_data = array(
array(
'title' => 'Test 2019',
'todo_title' => 'Test 2019',
'todo_strtotime' => '1548892800',
'todo_date' => 'January 31, 2019',
'todo_year' => '2019',
'todo_month' => '1',
'todo_unique_id' => '819097135',
'todo_status' => '0',
),
array(
'title' => 'Test new data',
'todo_title' => 'Test new data',
'todo_strtotime' => '1550620800',
'todo_date' => 'February 20, 2019',
'todo_year' => '2019',
'todo_month' => '2',
'todo_unique_id' => '1833135764',
'todo_status' => '0',
),
array(
'title' => 'Loriem ipsum',
'todo_title' => 'Loriem ipsum',
'todo_strtotime' => '1552953600',
'todo_date' => 'March 19, 2019',
'todo_year' => '2019',
'todo_month' => '3',
'todo_unique_id' => '1501977081',
'todo_status' => '0',
),
array(
'title' => 'asdfasf',
'todo_title' => 'asdfasf',
'todo_strtotime' => '1579910400',
'todo_date' => 'January 25, 2020',
'todo_year' => '2020',
'todo_month' => '1',
'todo_status' => '1',
'todo_unique_id' => '1573546118',
),
array(
'title' => 'Hello world',
'todo_title' => 'Hello world',
'todo_strtotime' => '1580428800',
'todo_date' => 'January 31, 2020',
'todo_year' => '2020',
'todo_month' => '1',
'todo_status' => '1',
'todo_unique_id' => '314611084',
),
array(
'title' => 'Test',
'todo_title' => 'Test',
'todo_strtotime' => '1582156800',
'todo_date' => 'February 20, 2020',
'todo_year' => '2020',
'todo_month' => '2',
'todo_unique_id' => '184291872',
'todo_status' => '0',
),
array(
'title' => 'Hello world',
'todo_title' => 'Hello world',
'todo_strtotime' => '1583452800',
'todo_date' => 'March 6, 2020',
'todo_year' => '2020',
'todo_month' => '3',
'todo_status' => '1',
'todo_unique_id' => '383849492',
)
);
$new_todo_data = [];
foreach ($todo_data as $item){
$new_todo_data[$item['todo_year']][] = $item;
}
print_r($new_todo_data);