我有一个包含大量记录的数组,如:
{
"ID": "38424",
"heading": "Nylies soek nuwe hoof",
"typeID": "1",
"datein": "2016-09-26 12:14:16",
"edited_datein": null,
"publishDate": "2016-09-23 00:00:00",
"category": {
"ID": "1",
"heading": "News",
"typeID": "3",
"datein": "2016-09-26 11:50:06",
"edited_datein": null,
"url": "news"
},
"authorID": "592",
"tags": "skool,school,hoof,headmaster,etienne burger"
}
我有另一个数组"列"我希望记录被"过滤"通过
{
"ID",
"heading",
"datein",
"category|heading",
"category|url"
}
我希望结果能够根据列项创建一个多维数组:
{
"ID": "38424",
"heading": "Nylies soek nuwe hoof",
"datein": "2016-09-26 12:14:16",
"category": {
"heading": "News",
"url": "news"
}
}
我如何实现这一目标?我现在完全坚持这个:(现在忙着尝试一下array_combine的黑客,但我不希望它能解决这个问题
答案 0 :(得分:0)
所以在坚持了好几个小时之后......并将其发布在这里。我找到了解决方案
$new_list = array();
foreach ($n as $record) {
$new_list[] = filter_columns($columns, $record);
}
和功能:
function filter_columns($columns, $record, $pre="") {
$return = array();
foreach ($record as $key => $value) { // loop through the fields in the record
$str = $pre.$key; // create a string of the current key
if (in_array($str,$columns)){
$return[$key] = $value;
}
if (is_array($value)){
$return[$key] = filter_columns($columns, $value,$key."|"); // if the value is an array recall the function but prepend the current key| to the key mask
}
}
return $return;
}