用PHP将数组组织成嵌套数组
有人知道如何转换这个吗用PHP将数组组织成嵌套数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,有人知道如何转换这个吗 $events = array( array("type" => "off-site", "title" => "aaa", "nid" => "11"), array("type" => "off-site", "title" => "bbb", "nid" => "22"), array("type" => "installation", "title" => "ccc", "nid" => "33"), arra
$events = array(
array("type" => "off-site", "title" => "aaa", "nid" => "11"),
array("type" => "off-site", "title" => "bbb", "nid" => "22"),
array("type" => "installation", "title" => "ccc", "nid" => "33"),
array("type" => "opening", "title" => "ddd", "nid" => "44"),
array("type" => "opening", "title" => "eee", "nid" => "55"),
array("type" => "opening", "title" => "fff", "nid" => "66")
);
进入这个
$events_processed = array(
"off-site" => array(
array(
"title" => "aaa",
"nid" => "11"
),
array(
"title" => "bbb",
"nid" => "22"
)
),
"installation" => array(
array(
"title" => "ccc",
"nid" => "33"
)
),
"opening" => array(
array(
"title" => "ddd",
"nid" => "44"
),
array(
"title" => "eee",
"nid" => "55"
),
array(
"title" => "fff",
"nid" => "66"
)
)
);
使用php
我已经尝试过从不同的帖子中应用不同的方法,但没有成功。
我需要嵌套数组,这样我就可以按“类型”对数组重新排序。您好,只需循环并重新组织
$result = array();
foreach($events as $event){
$key = $event['type'];
unset($event['type']);
$result[$key][] = $event;
}
print_r($result);
以下是我所做的:
$array_processed = array();
foreach( $events as $evt){
$array_processed[$evt['type']][] = array('title'=>$evt['title'], 'nid'=>$evt['nid']);
}
print_r($array_processed);
您可以输入下面的代码,请注意,您当前的输出无效,因为您错过了数组索引 例1
$events_processed = array();
foreach($events as $options)
{
$events_processed[$options['type']][] = array("title"=>$options['title'],"nid"=>$options['nid']);
}
var_dump($events_processed);
或
示例2(@dleiftah建议)
两个结果都是这样的,但第二个更灵活
array
'off-site' =>
array
0 =>
array
'title' => string 'aaa' (length=3)
'nid' => string '11' (length=2)
1 =>
array
'title' => string 'bbb' (length=3)
'nid' => string '22' (length=2)
'installation' =>
array
0 =>
array
'title' => string 'ccc' (length=3)
'nid' => string '33' (length=2)
'opening' =>
array
0 =>
array
'title' => string 'ddd' (length=3)
'nid' => string '44' (length=2)
1 =>
array
'title' => string 'eee' (length=3)
'nid' => string '55' (length=2)
2 =>
array
'title' => string 'fff' (length=3)
'nid' => string '66' (length=2)
没有内置的函数可以做到这一点。您必须循环此数组并创建一个新数组
$events=array(contents-of-array);
$proc_events=array(
'off-site' => array(),
'installation' => array(),
'opening' => array()
);
foreach($events as $key => $value)
{
switch($value['type'])
{
case 'off-site':
$proc_events['offsite']=array('title' => $value['title'], 'nid' => $value['nid']);
break;
case 'installation':
$proc_events['installation']=array('title' => $value['title'], 'nid' => $value['nid']);
break;
case 'opening':
$proc_events['opening']=array('title' => $value['title'], 'nid' => $value['nid']);
break;
}
}
上述方法应该有效。执行此操作的最短代码应该是:
foreach ($events as $event)
$events_processed[$event['type']][] = array('title' => $event['title'], 'nid' => $event['nid']);
以下是我能够做到的:
<?php
$events = array(
array("type" => "off-site", "title" => "aaa", "nid" => "11"),
array("type" => "off-site", "title" => "bbb", "nid" => "22"),
array("type" => "installation", "title" => "ccc", "nid" => "33"),
array("type" => "opening", "title" => "ddd", "nid" => "44"),
array("type" => "opening", "title" => "eee", "nid" => "55"),
array("type" => "opening", "title" => "fff", "nid" => "66")
);
foreach ($events as $event) {
$events_processed[$event['type']][] = array('title' => $event['title'],
'nid' => $event['nid']
);
}
echo '<pre>';
print_r($events_processed);
?>
你试过使用吗?或者只是在数组中循环可能更容易。但是,如果事件数组中添加了更多信息,则必须修改此代码以保留它…我同意@dleiftah需要修改。。。已更新替代方案的应答器
<?php
$events = array(
array("type" => "off-site", "title" => "aaa", "nid" => "11"),
array("type" => "off-site", "title" => "bbb", "nid" => "22"),
array("type" => "installation", "title" => "ccc", "nid" => "33"),
array("type" => "opening", "title" => "ddd", "nid" => "44"),
array("type" => "opening", "title" => "eee", "nid" => "55"),
array("type" => "opening", "title" => "fff", "nid" => "66")
);
foreach ($events as $event) {
$events_processed[$event['type']][] = array('title' => $event['title'],
'nid' => $event['nid']
);
}
echo '<pre>';
print_r($events_processed);
?>
Array
(
[off-site] => Array
(
[0] => Array
(
[title] => aaa
[nid] => 11
)
[1] => Array
(
[title] => bbb
[nid] => 22
)
)
[installation] => Array
(
[0] => Array
(
[title] => ccc
[nid] => 33
)
)
[opening] => Array
(
[0] => Array
(
[title] => ddd
[nid] => 44
)
[1] => Array
(
[title] => eee
[nid] => 55
)
[2] => Array
(
[title] => fff
[nid] => 66
)
)
)