数组排序 - php将数组键值相同的数组合并数组为一个数组
程序员文章站
2022-06-12 09:52:53
...
Array (
[question_id] => Array ( [0] => 4 [1] => 4 [2] => 4 [3] => 4 [4] => 4 [5] => 4 [6] => 4 [7] => 4 )
[result_branch] => Array ( [0] => 126 [1] => 130[2] => 134 [3] => 1232 [4] => 128134 [5] => 16 [6] => 128134 [7] => 12136
[text] => Array ( [0] => 3213 [1] => qweq [2] => wdas [3] => d [4] => cxzc [5] => xzczx [6] => czx [7] => saed )
)
输出:
Array (array([question_id]=>4,[result_branch]=>126,[text]=>3213 ),
array([question_id]=>4,[result_branch]=>130,[text]=>qweq ),
array([question_id]=>4,[result_branch]=>134 ,[text]=>wdas ),
array([question_id]=>4,[result_branch]=>1232 ,[text]=>d ),
...
array([question_id]=>4,[result_branch]=>12136 ,[text]=>saed ),
)
回复内容:
Array (
[question_id] => Array ( [0] => 4 [1] => 4 [2] => 4 [3] => 4 [4] => 4 [5] => 4 [6] => 4 [7] => 4 )
[result_branch] => Array ( [0] => 126 [1] => 130[2] => 134 [3] => 1232 [4] => 128134 [5] => 16 [6] => 128134 [7] => 12136
[text] => Array ( [0] => 3213 [1] => qweq [2] => wdas [3] => d [4] => cxzc [5] => xzczx [6] => czx [7] => saed )
)
输出:
Array (array([question_id]=>4,[result_branch]=>126,[text]=>3213 ),
array([question_id]=>4,[result_branch]=>130,[text]=>qweq ),
array([question_id]=>4,[result_branch]=>134 ,[text]=>wdas ),
array([question_id]=>4,[result_branch]=>1232 ,[text]=>d ),
...
array([question_id]=>4,[result_branch]=>12136 ,[text]=>saed ),
)
$arr = Array (
'question_id' => Array ( 4, 4, 4, 4, 4),
'result_branch' => Array (126, 130, 134 ,1232 ,128134),
'text' => Array (3213,'qweq', 'wdas', 'd ','cxzc' )
);
$idNum = count($arr['question_id']);
$branchNum = count($arr['result_branch']);
$textNum = count($arr['text']);
$countMax = ($idNum > $branchNum ? $idNum : $branchNum) > $textNum ? ($idNum > $branchNum ? $idNum : $branchNum) : $textNum;
$result = array();
for($i=0; $i isset($arr['question_id'][$i]) ? $arr['question_id'][$i] : '',
'result_branch' => isset($arr['result_branch'][$i]) ? $arr['result_branch'][$i] : '',
'text' => isset($arr['text'][$i]) ? $arr['text'][$i] : ''
);
$result[] = $temp;
}
echo "";
print_r($result);
echo "
";
简单的写了一段,感觉不是很好。
推荐阅读
-
php二维数组按某个键值排序的实例讲解
-
php中有关合并某一字段键值相同的数组合并的改进
-
将一维或多维的数组连接成一个字符串的php代码
-
php数组怎么根据其中的一个值进行排序?解决方法
-
php下将多个数组合并成一个数组的方法与实例代码
-
PHP中通过加号合并数组的一个简单方法分享
-
php判断一个数组是否为有序的方法
-
PHP实现统计一个数字在排序数组中出现次数的方法
-
5位评委对参赛选手进行打分,将所有的打分结果存储到对应类型的数组中, 将所有的评分结果去除一个最低分,去除一个最高分,然后获取的平均分数为 选手的最终得分.设计程序,用键盘输入5位评委的评分,并打印输
-
c语言将一个数组中的值按逆序重新存放。例如,原来顺序为8,6,5,4,1.要求改为1,4,5,6,8.