PHP数组覆盖值但需要添加到数组
数组:
Array
(
[0] => Array
(
[0] => Service Simulators
[1] => Array
(
[0] => 1.php
[1] => Title 1
)
)
[1] => Array
(
[0] => Service Simulators
[1] => Array
(
[0] => 2.php
[1] => Title 2
)
)
[2] => Array
(
[0] => Service Simulators
[1] => Array
(
[0] => 3.php
[1] => Title 3
)
)
[3] => Array
(
[0] => Service Simulators
[1] => Array
(
[0] => 4.php
[1] => Title 4
)
)
[4] => Array
(
[0] => Service Simulators
[1] => Array
(
[0] => 5.php
[1] => Title 5
)
)
[5] => Array
(
[0] => Trouble Shooting
[1] => Array
(
[0] => a.php
[1] => Title A
)
)
[6] => Array
(
[0] => Trouble Shooting
[1] => Array
(
[0] => b.php
[1] => Title B
)
)
[7] => Array
(
[0] => Deprecated
[1] => Array
(
[0] => 123.php
[1] => Title 123
)
)
[8] => Array
(
[0] => Admin Only
[1] => Array
(
[0] => admin.php
[1] => Title Admin
)
)
)
我需要什么
Array
(
[0] => Array
(
[0] => Service Simulators
[1] => Array
(
[0] => 1.php
[1] => Title 1
[3] => 2.php
[4] => Title 2
[5] => 3.php
[6] => Title 3
[7] => 4.php
[8] => Title 4
[9] => 5.php
[10] => Title 5
)
)
[1] => Array
(
[0] => Trouble Shooting
[1] => Array
(
[0] => a.php
[1] => Title A
[3] => b.php
[4] => Title B
)
)
[2] => Array
(
[0] => Deprecated
[1] => Array
(
[0] => 123.php
[1] => Title 123
)
)
[3] => Array
(
[0] => Admin Only
[1] => Array
(
[0] => admin.php
[1] => Title Admin
)
)
)
发生了
Array
(
[0] => Array
(
[0] => Service Simulators
[1] => Array
(
[0] => 5.php
[1] => Title 5
)
)
[1] => Array
(
[0] => Trouble Shooting
[1] => Array
(
[0] => b.php
[1] => Title B
)
)
[2] => Array
(
[0] => Deprecated
[1] => Array
(
[0] => 123.php
[1] => Title 123
)
)
[3] => Array
(
[0] => Admin Only
[1] => Array
(
[0] => admin.php
[1] => Title Admin
)
)
)
什么我正在尝试,它来自查询结果集
$tmp_arr = array();
while($row = mysql_fetch_row($result))
{
// $row[1] = groupname
// $row[2] = pagename
// $row[3] = pagetitle
// Push to array
$build_arr_items = array($row[2],$row[3]);
array_push($tmp_arr, array($row[1], $build_arr_items)); // This builds the first example
//$tmp_arr[$row[1]]=$build_arr_items; // this is the overwrite example
}
任何想法,在正确的方向上有帮助吗?
The array:
Array
(
[0] => Array
(
[0] => Service Simulators
[1] => Array
(
[0] => 1.php
[1] => Title 1
)
)
[1] => Array
(
[0] => Service Simulators
[1] => Array
(
[0] => 2.php
[1] => Title 2
)
)
[2] => Array
(
[0] => Service Simulators
[1] => Array
(
[0] => 3.php
[1] => Title 3
)
)
[3] => Array
(
[0] => Service Simulators
[1] => Array
(
[0] => 4.php
[1] => Title 4
)
)
[4] => Array
(
[0] => Service Simulators
[1] => Array
(
[0] => 5.php
[1] => Title 5
)
)
[5] => Array
(
[0] => Trouble Shooting
[1] => Array
(
[0] => a.php
[1] => Title A
)
)
[6] => Array
(
[0] => Trouble Shooting
[1] => Array
(
[0] => b.php
[1] => Title B
)
)
[7] => Array
(
[0] => Deprecated
[1] => Array
(
[0] => 123.php
[1] => Title 123
)
)
[8] => Array
(
[0] => Admin Only
[1] => Array
(
[0] => admin.php
[1] => Title Admin
)
)
)
What I need
Array
(
[0] => Array
(
[0] => Service Simulators
[1] => Array
(
[0] => 1.php
[1] => Title 1
[3] => 2.php
[4] => Title 2
[5] => 3.php
[6] => Title 3
[7] => 4.php
[8] => Title 4
[9] => 5.php
[10] => Title 5
)
)
[1] => Array
(
[0] => Trouble Shooting
[1] => Array
(
[0] => a.php
[1] => Title A
[3] => b.php
[4] => Title B
)
)
[2] => Array
(
[0] => Deprecated
[1] => Array
(
[0] => 123.php
[1] => Title 123
)
)
[3] => Array
(
[0] => Admin Only
[1] => Array
(
[0] => admin.php
[1] => Title Admin
)
)
)
Whats happening
Array
(
[0] => Array
(
[0] => Service Simulators
[1] => Array
(
[0] => 5.php
[1] => Title 5
)
)
[1] => Array
(
[0] => Trouble Shooting
[1] => Array
(
[0] => b.php
[1] => Title B
)
)
[2] => Array
(
[0] => Deprecated
[1] => Array
(
[0] => 123.php
[1] => Title 123
)
)
[3] => Array
(
[0] => Admin Only
[1] => Array
(
[0] => admin.php
[1] => Title Admin
)
)
)
What I'm trying, it's coming from a query result set
$tmp_arr = array();
while($row = mysql_fetch_row($result))
{
// $row[1] = groupname
// $row[2] = pagename
// $row[3] = pagetitle
// Push to array
$build_arr_items = array($row[2],$row[3]);
array_push($tmp_arr, array($row[1], $build_arr_items)); // This builds the first example
//$tmp_arr[$row[1]]=$build_arr_items; // this is the overwrite example
}
any thoughts, help in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否已与该数组结构结婚了?您可能会更好地使用以组名称为键的数组,即:
因为这样您就可以像这样编写代码:
这种方式看起来也不太混乱。
Are you married to that array structure? You might be better served by an array keyed by the group name ie:
Because then you could code like so:
This way also seems a bit less messy.
修复了 Pivotal 的示例以保持与操作类似的结构,但通过命名键设置每个:
Fixed pivotal's example to keep a similar structure to the op, but setting each by named key:
好吧,我想我找到了有用的东西。我刚刚颠倒了关键索引关系
OK I think I found something that works. I just reversed the Key Index relations