从平面数组和另一个二维数组填充二维数组

问题描述 投票:0回答:3

我得到两个数组

$years = [
    1990,
    1991,
    1992,
    1993,
    1994,
    1995,
    1996,
    1997,
    1998,
    1999,
    2000,
    2001,
    2002,
    2003,
    2004,
    2005,
    2006,
    2007,
    2008,
    2009,
    2010,
    2011,
    2012,
    2013,
];
$cc = [
    ['year' => 2011, 'conv_value' => 80],
    ['year' => 2012, 'conv_value' => 95],
    ['year' => 2004, 'conv_value' => 60],
    ['year' => 2000, 'conv_value' => 55],
];

我想填充一个更大的二维数组,其结构与我的二维输入数组相同,但包含第一个数组的所有年份。

如果找到年份,则将设置一个新索引名称conv_value包含第二个数组索引conv_value中的值,如果未找到,则conv_value将为空。

想要的结果:

[
    ['year' => 1990, 'conv_value' => ''],
    ['year' => 1991, 'conv_value' => ''],
    ['year' => 1992, 'conv_value' => ''],
    ['year' => 1993, 'conv_value' => ''],
    ['year' => 1994, 'conv_value' => ''],
    ['year' => 1995, 'conv_value' => ''],
    ['year' => 1996, 'conv_value' => ''],
    ['year' => 1997, 'conv_value' => ''],
    ['year' => 1998, 'conv_value' => ''],
    ['year' => 1999, 'conv_value' => ''],
    ['year' => 2000, 'conv_value' => 55],
    ['year' => 2001, 'conv_value' => ''],
    ['year' => 2002, 'conv_value' => ''],
    ['year' => 2003, 'conv_value' => ''],
    ['year' => 2004, 'conv_value' => 60],
    ['year' => 2005, 'conv_value' => ''],
    ['year' => 2006, 'conv_value' => ''],
    ['year' => 2007, 'conv_value' => ''],
    ['year' => 2008, 'conv_value' => ''],
    ['year' => 2009, 'conv_value' => ''],
    ['year' => 2010, 'conv_value' => ''],
    ['year' => 2011, 'conv_value' => 80],
    ['year' => 2012, 'conv_value' => 95],
    ['year' => 2013, 'conv_value' => ''],
];

我编写了以下脚本,但在编写嵌套循环后我很挣扎。

for ($y = 0; $y < sizeof($cc); $y++){
    for ($z = 0; $z < sizeof($years); $z++){
        if ($cc[$y]['year'] == $years[$z]) {
            echo 'Hay<br>';
        } else {
            echo 'Nahee hay<br>';
        }
    }
}
php arrays multidimensional-array mapping default-value
3个回答
1
投票

尝试使用 in_array...

if (in_array($cc[$y]['year'], $years)) {

0
投票

代码很长,但我写了它并且它解决了我的问题:

<?php
                // all years                
                for ($i = 1990; $i <= (date('Y')+1); $i++){
                    $years[] = $i;
                }

                // db years
                $i=0;
                foreach ( $cur_conv as $key ){
                    $cc[$i]['year'] = $key['year'];
                    $cc[$i]['conv_value'] = $key['conv_value'];
                    $i++;
                }

                // make array which contain only years
                for($i=0; $i<sizeof($cc); $i++){
                    $db_years[] = $cc[$i]['year'];
                }

                // finally search local array in db array
                for($j=0; $j<count($years); $j++)
                {
                    $index = array_search($years[$j], $db_years);

                    if ( $index === FALSE )
                    {
                        $new_arr[$j]['year'] = $years[$j];
                        $new_arr[$j]['conv_value'] = '';

                    } else {

                        $new_arr[$j]['year'] = $cc[$index]['year'];
                        $new_arr[$j]['conv_value'] = $cc[$index]['conv_value'];
                    }
                }
                ?>

0
投票

将二维数组转换为平面关联查找映射,然后循环多年并使用映射值或回退值填充新的二维数组行。 演示

$map = array_column($cc, 'conv_value', 'year');
var_export(
    array_map(
        fn($y) => ['year' => $y, 'conv_value' => $map[$y] ?? ''],
        $years
    )
);
© www.soinside.com 2019 - 2024. All rights reserved.