PHP数组中如何把字段名相同的组成一个数组?在线等。。。。
ringa_lee
ringa_lee 2017-04-10 18:02:00
[PHP讨论组]

我想把字段p_id相同的变成一个多维数组,怎么处理?还是说在查询数据的时候就能进行处理吗?

Array
(

[0] => Array
    (
        [id] => 11
        [name] => 奥巴马
        [winning] => 5
        [title_honor] => 5
        [photo] => 
        [classid] => 10186
        [site_id] => 313
        [listorder] => 0
        [p_id] => 5
    )

[1] => Array
    (
        [id] => 10
        [name] => 习近
        [winning] => 三等奖
        [title_honor] => 荣誉科学
        [photo] => themes/313/userfiles/images/2016/12/6/source/8ops1ho3dm9i04u.jpg
        [classid] => 10186
        [site_id] => 313
        [listorder] => 0
        [p_id] => 6
    )

[2] => Array
    (
        [id] => 9
        [name] => 奥巴马
        [winning] => 二等
        [title_honor] => 2013年度同济大学医学院“特色导师”称号
        [photo] => themes/313/userfiles/images/2016/12/6/source/u0g8xhd1gzphkpf.jpg
        [classid] => 10186
        [site_id] => 313
        [listorder] => 0
        [p_id] => 5
    )

[3] => Array
    (
        [id] => 8
        [name] => 奥巴马
        [winning] => 一等奖
        [title_honor] => 先锋队
        [photo] => themes/313/userfiles/images/2016/12/6/source/qtqukvv4birtkdc.jpg
        [classid] => 10186
        [site_id] => 313
        [listorder] => 0
        [p_id] => 5
    )

)

ringa_lee
ringa_lee

ringa_lee

全部回复(4)
伊谢尔伦
$arr = [....]; //你上面的数组
$newArr = [];
foreach ($arr as $item) {
    $newArr[$item['p_id']][] = $item;
}
print_r($newArr);
PHP中文网

用array_column就可以了

迷茫

array_column 才是最佳选择
<?php
// 表示由数据库返回的可能记录集的数组
$a = array(
array(

'id' => 5698,
'first_name' => 'Bill',
'last_name' => 'Gates',

),
array(

'id' => 4767,
'first_name' => 'Steve',
'last_name' => 'Jobs',

)
array(

'id' => 3809,
'first_name' => 'Mark',
'last_name' => 'Zuckerberg',

)
);

$last_names = array_column($a, 'last_name');
print_r($last_names);
?>
输出:
Array
(
[0] => Gates
[1] => Jobs
[2] => Zuckerberg
)

迷茫
$arr = [
    0 => [
        'id'          => 11,
        'name'        => '奥巴马',
        'winning'     => 5,
        'title_honor' => 5,
        'photo'       => '',
        'classid'     => 10186,
        'site_id'     => 313,
        'listorder'   => 0,
        'p_id'        => 5,
    ],

    '1' => [
        'id'          => 10,
        'name'        => '习近',
        'winning'     => '三等奖',
        'title_honor' => '荣誉科学',
        'photo'       => 'themes/313/userfiles/images/2016/12/6/source/8ops1ho3dm9i04u.jpg',
        'classid'     => 10186,
        'site_id'     => 313,
        'listorder'   => 0,
        'p_id'        => 6,
    ],

    '2' => [
        'id'          => 9,
        'name'        => '奥巴马',
        'winning'     => '二等',
        'title_honor' => '2013年度同济大学医学院“特色导师”称号',
        'photo'       => 'themes/313/userfiles/images/2016/12/6/source/u0g8xhd1gzphkpf.jpg',
        'classid'     => 10186,
        'site_id'     => 313,
        'listorder'   => 0,
        'p_id'        => 5,
    ],

    '3' => [
        'id'          => 8,
        'name'        => '奥巴马',
        'winning'     => '一等奖',
        'title_honor' => '先锋队',
        'photo'       => 'themes/313/userfiles/images/2016/12/6/source/qtqukvv4birtkdc.jpg',
        'classid'     => 10186,
        'site_id'     => 313,
        'listorder'   => 0,
        'p_id'        => 5,
    ],
];

$column = array_column($arr, 'p_id');
echo '<pre>';
var_export($column);
  • 输出结果如下

  • array (

  • 0 => 5,

  • 1 => 6,

  • 2 => 5,

  • 3 => 5,

  • )

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号