0

I am trying to make a new array ($names) that is the same size of $years array. I'm looping through the $year variable, but struggling to double "Explode" the $years_names variable (first by comma then by colon), and not sure that's the best way to go. Also because of that, I'm not able to use search_array. Since it may not have data for each year, I'd like the new array to hold a null value in the position for that year. So in my $years_names variable, I am missing data from 2010 to 2012, so indexes 0-1 should be null, and index 8 since there is no data for that 2018 either. Attached is a more or less what I'm trying to get for my $names array. Thanks!

enter image description here

$years = range(2010, 2020);

$data_years = "2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020";
$data_names = "Charlie,Lucy,Linus,Pig Pen,Snoopy,Woodstock,Peppermint Patty,Marcie";

$years_names = "2012:Charlie,2013:Lucy,2014:Linus,2015:Pig Pen,2016:Snoopy,2017:Woodstock,2019:Peppermint Patty,2020:Marcie";
$Exploded = explode(',', $years_names);
$names = [];

foreach($Exploded as $i => $item) {
     $names[$i] = explode(':', $item);
    //echo "i= {$i}<br/>";
    //echo "item= {$item}<br/>";
    //echo $names[$i][0] . ':' . $names[$i][1] . "<br/>";
}
7
  • Why is your data in this format? Commented Oct 20, 2020 at 19:19
  • The data is supplied from an API call in this way. The $years and $names array is then used in a graphing chart, so I unfortunately need to work with how the data is supplied. Commented Oct 20, 2020 at 19:29
  • This is a very odd format to use, are you sure there isn't a different format the API can supply? Commented Oct 20, 2020 at 19:30
  • There are two additional outputs with the date and name values in a string list, but I still have the same problem of having to fill the $names array. Commented Oct 20, 2020 at 19:32
  • Edited the post to add the two other values $data_years and $data_name...unfortunately that's all I have. Commented Oct 20, 2020 at 19:36

1 Answer 1

1

You can do something like this

$years = range(2010, 2020);
$years_names = "2012:Charlie,2013:Lucy,2014:Linus,2015:Pig Pen,2016:Snoopy,2017:Woodstock,2019:Peppermint Patty,2020:Marcie";

//split `$years_names` at comma
$split_values = explode(',', $years_names);

//convert values to a more usable format
$split_values = array_reduce($split_values, function($return, $item) {
    
    //explode $item (e.g `2012:Charlie`) at the colon symbol
    $explode = explode(':', $item);
    
    //separate year from name as key => value
    $return[$explode[0]] = $explode[1];

    //this return reduces the current iteration of `$split_values` to the key=>value pair
    return $return;
});

$names = [];
foreach($years as $year) {
    
    //set default value as null. If year is not found in next loop, this will remain
    $names[$year] = null;
    
    //check if year exists in $split_values
    if(array_key_exists($year, $split_values)) {
        
        //if it does, set $names[$year] to that value
        $names[$year] = $split_values[$year];
    }
    
}

print_r($names);

Output

Array
(
    [2010] => 
    [2011] => 
    [2012] => Charlie
    [2013] => Lucy
    [2014] => Linus
    [2015] => Pig Pen
    [2016] => Snoopy
    [2017] => Woodstock
    [2018] => 
    [2019] => Peppermint Patty
    [2020] => Marcie
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate. I'll give it a go, but this makes things look much more clear. Thank you.

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.