Home > Backend Development > PHP Tutorial > PHP array reorganization

PHP array reorganization

WBOY
Release: 2016-08-18 09:15:45
Original
1297 people have browsed it

I have an array like this
array(97) {
[0] => array(5) {

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

}
[1] => array(5) {

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

}
[2] => array(5) {

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

}
[3] => array(5) {

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

}
[4] => array(5) {

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

}

Hope to convert it into an array like this

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

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

}

}

Can anyone help me solve this problem? Thank you very much

Reply content:

I have an array like this
array(97) {
[0] => array(5) {

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

}
[1] => array(5) {

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

}
[2] => array(5) {

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

}
[3] => array(5) {

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

}
[4] => array(5) {

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

}

Hope to convert it into an array like this

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

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

}

}

Can anyone help me solve this problem? Thank you very much

The whole rough but clear way is as follows

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

$new = $array[0];
$new[0]['flow_name'] = $tmp;</code>
Copy after login

Haha...

Try to use PHP’s built-in functions to be more efficient

<code class="php">$temp = $arr[0];
$temp['flow_name'] = join('---', array_column($arr, 'flow_name'));</code>
Copy after login

This is not simple

<code>
// 先把flow_name全部拿出来

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

$flow_name = join('---', $tmp_arr[]);</code>
Copy after login

<code>$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;</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template