将一个多维数组附加到同一键上的另一个多维数组

发布于 2024-11-27 19:40:38 字数 2308 浏览 0 评论 0原文

我有两个数组

$input = Array
(
    [0] => Array
        (
            [section] => 725
            [location] => New Building - 26
            [rows] => DDD
            [seats] => DDD
        )

    [1] => Array
        (
            [section] => 721
            [location] => Helipad - II
            [rows] => R1,R2
            [seats] => S1,S2
        )

    [2] => Array
        (
            [section] => 724
            [location] => NDTV Times
            [rows] => R1,R2,R3
            [seats] => S1,S2,S3
        )
);

,下面是第二个数组

$extra = Array
(
    [0] => dry||Obstacles Present||yes
    [1] => wet||Not Find||no
    [2] => icy||||yes
    [3] => 
)

,我需要下面所需的数组:

$output =Array
(
    [0] => Array
        (
            [section] => 725
            [location] => New Building - 26
            [rows] => DDD
            [seats] => DDD
            [conditions] => dry
            [obstacles] => Obstacles Present
            [normal_lighting] => yes
        )

    [1] => Array
        (
            [section] => 721
            [location] => Helipad - II
            [rows] => R1,R2
            [seats] => S1,S2
            [conditions] => wet
            [obstacles] => Not Find
            [normal_lighting] => no
        )

    [2] => Array
        (
            [section] => 724
            [location] => NDTV Times
            [rows] => R1,R2,R3
            [seats] => S1,S2,S3
            [conditions] => icy
            [obstacles] => 
            [normal_lighting] => yes
        )
)

我执行了以下序列来获取所需的数组:

foreach($extra as $efk=>$efv)
{
    if(!empty($efv)) {
        $arr_field_value[] = explode("||", $efv);
    }
}
$arr_key = array('conditions','obstacles','normal_lighting');
foreach($arr_field_value as $fv)
{
    $arr_extra_field[]=array_combine($arr_key,$fv);
}

foreach($input as $k=>$v)
{   
    $output[]=array_merge($v,$arr_extra_field[$k]);
}
echo "<pre>"; print_r($output);

我知道这确实是一个漫长的方法,请建议我任何明智的方法来做到这一点。

您可以查看工作演示

谢谢。

i have two array

$input = Array
(
    [0] => Array
        (
            [section] => 725
            [location] => New Building - 26
            [rows] => DDD
            [seats] => DDD
        )

    [1] => Array
        (
            [section] => 721
            [location] => Helipad - II
            [rows] => R1,R2
            [seats] => S1,S2
        )

    [2] => Array
        (
            [section] => 724
            [location] => NDTV Times
            [rows] => R1,R2,R3
            [seats] => S1,S2,S3
        )
);

Below is second array

$extra = Array
(
    [0] => dry||Obstacles Present||yes
    [1] => wet||Not Find||no
    [2] => icy||||yes
    [3] => 
)

and i need Desired array below :

$output =Array
(
    [0] => Array
        (
            [section] => 725
            [location] => New Building - 26
            [rows] => DDD
            [seats] => DDD
            [conditions] => dry
            [obstacles] => Obstacles Present
            [normal_lighting] => yes
        )

    [1] => Array
        (
            [section] => 721
            [location] => Helipad - II
            [rows] => R1,R2
            [seats] => S1,S2
            [conditions] => wet
            [obstacles] => Not Find
            [normal_lighting] => no
        )

    [2] => Array
        (
            [section] => 724
            [location] => NDTV Times
            [rows] => R1,R2,R3
            [seats] => S1,S2,S3
            [conditions] => icy
            [obstacles] => 
            [normal_lighting] => yes
        )
)

i did following sequence to get the desired array:

foreach($extra as $efk=>$efv)
{
    if(!empty($efv)) {
        $arr_field_value[] = explode("||", $efv);
    }
}
$arr_key = array('conditions','obstacles','normal_lighting');
foreach($arr_field_value as $fv)
{
    $arr_extra_field[]=array_combine($arr_key,$fv);
}

foreach($input as $k=>$v)
{   
    $output[]=array_merge($v,$arr_extra_field[$k]);
}
echo "<pre>"; print_r($output);

I know this is really a lengthy way,Please suggest me any Smart way to do so.

You can see the WORKING DEMO

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一刻暧昧 2024-12-04 19:40:38
foreach($extra as $key => $val){
    if($val !== ''){
        list($conditions, $obstacles, $normal_lighting) = explode('||', $val);
        $input[$key]['conditions'] = $conditions;
        $input[$key]['obstacles'] = $obstacles;
        $input[$key]['normal_lighting'] = $normal_lighting;
    }
}
foreach($extra as $key => $val){
    if($val !== ''){
        list($conditions, $obstacles, $normal_lighting) = explode('||', $val);
        $input[$key]['conditions'] = $conditions;
        $input[$key]['obstacles'] = $obstacles;
        $input[$key]['normal_lighting'] = $normal_lighting;
    }
}
秉烛思 2024-12-04 19:40:38

我认为您正在寻找 array_merge_recursive()

foreach ($extra as $key => &$extra_elem) {
    if (!$extra_elem) {
        unset($extra[$key]);
        continue;
    }
    $extra_elem = array_combine(array(
        'conditions',
        'obstacles',
        'normal_lighting',
    ), explode('||', $extra_elem));
}
$desired = array_merge_recursive($input, $extra);

I think you are looking for array_merge_recursive()

foreach ($extra as $key => &$extra_elem) {
    if (!$extra_elem) {
        unset($extra[$key]);
        continue;
    }
    $extra_elem = array_combine(array(
        'conditions',
        'obstacles',
        'normal_lighting',
    ), explode('||', $extra_elem));
}
$desired = array_merge_recursive($input, $extra);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文