0

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>';
        }
    }
}
1
  • No part of this question Needs Debugging Details. Come on. This question is sufficiently clear. If you don't like the coding attempt from 2012, then edit it out. The question will still be completely clear without the coding attempt. Commented Nov 22, 2024 at 11:30

3 Answers 3

1

Try using in_array...

if (in_array($cc[$y]['year'], $years)) {
Sign up to request clarification or add additional context in comments.

1 Comment

as an alternative, i'd suggest isset over in_array (in case any of the arrays is large, speed difference might be significant) - just need to $flip_years = array_flip($years) first...
0

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

Comments

0

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
    )
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.