PHP Ferrari
PHP Ferrari

Reputation: 15616

Populate a 2d array from a flat array and another 2d array

I am getting two arrays

$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],
];

and I want to populate a larger 2d array with the same structure as my 2d input array, but with all of the years from the first array.

If year found then a new index will be set name conv_value contain value from 2nd array index conv_value and if not found then conv_value will be empty.

Desired result:

[
    ['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' => ''],
];

I scripted the following but I am struggling after writing the nested loops.

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>';
        }
    }
}

Upvotes: 0

Views: 240

Answers (3)

mickmackusa
mickmackusa

Reputation: 47894

Convert your 2d array to a flat associative lookup map, then loop over the years and populate the new 2d array rows with either mapped values or fall back values. Demo

$map = array_column($cc, 'conv_value', 'year');
var_export(
    array_map(
        fn($y) => ['year' => $y, 'conv_value' => $map[$y] ?? ''],
        $years
    )
);

Upvotes: 0

PHP Ferrari
PHP Ferrari

Reputation: 15616

It is long code but I wrote it & it solved my problem:

<?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'];
                    }
                }
                ?>

Upvotes: 0

Brian
Brian

Reputation: 8616

Try using in_array...

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

Upvotes: 1

Related Questions