欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP数组重组

程序员文章站 2022-05-08 20:56:34
...
我有这样一个数组
array(97) {
[0] => array(5) {
["job_id"] => string(1) "1"
["job_name"] => string(18) "内资公司注册"
["work_name"] => string(12) "公司注册"
["flow_name"] => string(15) "预约核名号"
["work_id"] => string(1) "1"

}
[1] => array(5) {

["job_id"] => string(1) "1"
["job_name"] => string(18) "内资公司注册"
["work_name"] => string(12) "公司注册"
["flow_name"] => string(18) "办理名称核准"
["work_id"] => string(1) "1"

}
[2] => array(5) {

["job_id"] => string(1) "1"
["job_name"] => string(18) "内资公司注册"
["work_name"] => string(12) "公司注册"
["flow_name"] => string(15) "预约受理号"
["work_id"] => string(1) "1"

}
[3] => array(5) {

["job_id"] => string(1) "1"
["job_name"] => string(18) "内资公司注册"
["work_name"] => string(12) "公司注册"
["flow_name"] => string(18) "办理三证合一"
["work_id"] => string(1) "1"

}
[4] => array(5) {

["job_id"] => string(1) "1"
["job_name"] => string(18) "内资公司注册"
["work_name"] => string(12) "公司注册"
["flow_name"] => string(12) "刻章备案"
["work_id"] => string(1) "1"

}

希望转换成这样的数组

array{
[0] => array(5) {

["job_id"] => string(1) "1"
["job_name"] => string(18) "内资公司注册"
["work_name"] => string(12) "公司注册"
["flow_name"] => string(12) "预约核名号---办理名称核准---预约受理号---办理三证合一----刻章备案"
["work_id"] => string(1) "1"

}

}

有大神朋友帮帮忙解决么 感激不尽

回复内容:

我有这样一个数组
array(97) {
[0] => array(5) {

["job_id"] => string(1) "1"
["job_name"] => string(18) "内资公司注册"
["work_name"] => string(12) "公司注册"
["flow_name"] => string(15) "预约核名号"
["work_id"] => string(1) "1"

}
[1] => array(5) {

["job_id"] => string(1) "1"
["job_name"] => string(18) "内资公司注册"
["work_name"] => string(12) "公司注册"
["flow_name"] => string(18) "办理名称核准"
["work_id"] => string(1) "1"

}
[2] => array(5) {

["job_id"] => string(1) "1"
["job_name"] => string(18) "内资公司注册"
["work_name"] => string(12) "公司注册"
["flow_name"] => string(15) "预约受理号"
["work_id"] => string(1) "1"

}
[3] => array(5) {

["job_id"] => string(1) "1"
["job_name"] => string(18) "内资公司注册"
["work_name"] => string(12) "公司注册"
["flow_name"] => string(18) "办理三证合一"
["work_id"] => string(1) "1"

}
[4] => array(5) {

["job_id"] => string(1) "1"
["job_name"] => string(18) "内资公司注册"
["work_name"] => string(12) "公司注册"
["flow_name"] => string(12) "刻章备案"
["work_id"] => string(1) "1"

}

希望转换成这样的数组

array{
[0] => array(5) {

["job_id"] => string(1) "1"
["job_name"] => string(18) "内资公司注册"
["work_name"] => string(12) "公司注册"
["flow_name"] => string(12) "预约核名号---办理名称核准---预约受理号---办理三证合一----刻章备案"
["work_id"] => string(1) "1"

}

}

有大神朋友帮帮忙解决么 感激不尽

整个比较粗但一目了然的方式,如下

$tmp = '';
foreach ($array as $key=>$val){
    $tmp .= $val['flow_name'].'---';
}
$tmp = rtrim($tmp, '---');

$new = $array[0];
$new[0]['flow_name'] = $tmp;

哈哈……

尽量使用PHP自带函数,效率能高点

$temp = $arr[0];
$temp['flow_name'] = join('---', array_column($arr, 'flow_name'));

这还不简单


// 先把flow_name全部拿出来

// 临时数组
$tmp_arr = [];
foreach($data as $value){
    // push到临时数组
    $tmp_arr[] = $value['flow_name'];
}

$flow_name = join('---', $tmp_arr[]);

$data = [
    [
        'flow_name' => '预约合约号'
    ],
    [
        'flow_name' => '办理名称核准'
    ],
    [
        'flow_name' => '预约受理号'
    ],
    [
        'flow_name' => '办理三证合一'
    ],
    [
        'flow_name' => '刻章备案'
    ]
];
$data = array_map(function ($item)
{
    return $item['flow_name'];
}, $data);
echo join('---',$data);
exit;
相关标签: php