【发布时间】:2018-04-21 02:08:38
【问题描述】:
一个从数据库中排序的动态数组,它有一些类似的组 id 和一些不同的组 id,如下所示:
$sourcearray = array(
array(id => 1 , value => 'text'),
array(id => 1 , value => 'text'),
array(id => 1 , value => 'text'),
array(id => 1 , value => 'text'),
array(id => 2 , value => 'text'),
array(id => 3 , value => 'text'),
array(id => 4 , value => 'text'),
array(id => 4 , value => 'text')
);
我想将它们分成具有相同组 ID 的组 类似的东西:
$group1 = array(
array(id => 1 , value => 'text'),
array(id => 1 , value => 'text'),
array(id => 1 , value => 'text'),
array(id => 1 , value => 'text')
);
$group2 = array(
array(id => 2 , value => 'text')
);
$group3 = array(
array(id => 3 , value => 'text')
);
$group4 = array(
array(id => 4 , value => 'text'),
array(id => 4 , value => 'text')
);
此 ID 不是唯一的,并且此安排和组 ID 不是静态的,它们可能会有所不同,具体取决于网站管理面板设置
【问题讨论】:
-
数组上不能有多个元素具有相同的键。
-
抱歉,这些在 PHP 中根本不存在(源数组永远不会像那样)。键和索引是唯一的。 PHP 只会保留最后一个定义。
-
对不起,我忘了告诉你这是来自数据库的结果,这些键被注册为 group_id 并且它不是唯一的,我将编辑我的问题,非常抱歉造成混乱跨度>
-
你确定不是
$sourcearray = array( array(1 => 'value'),之类的吗? -
@Don'tPanic ,是的,我很困惑,赶紧写这个问题,我现在编辑它,谢谢你提醒我:)
标签: php