1

I have the following array

$priceYear = Array ( 
       [AZ] => Array 
             ( 
               [1] => 2020 
               [2] => 2020 
             ) 
       [BY] => Array 
             ( 
               [0] => 2020 
               [1] => 2020 
             ) 
       [CX] => Array 
             ( 
               [1] => 2020 
               [2] => 2020 
               [3] => 2020 
             ) 
       [DW] => Array 
             ( 
               [106] => 2019 
               [107] => 2019 
               [108] => 2019
             )
      ) 

And another array with this

$array = Array ( 
       [0] => Array 
             ( 
               [YEAR] => 2018 
               [VALUE_AMDON] => 55 
             ) 
       [1] => Array 
             ( 
               [YEAR] => 2019
               [VALUE_AMDON] => 57
             ) 
       [2] => Array 
             ( 
               [YEAR] => 2020 
               [VALUE_AMDON] => 59
             ) 
      ) 

And I want to replace with the VALUE_AMDON if value from $priceYear == YEAR

So the output should look like this

$priceYear = Array ( 
                    [AZ] => Array 
                          ( 
                            [1] => 59 
                            [2] => 59
                          ) 
                    [BY] => Array 
                          ( 
                            [0] => 59
                            [1] => 59 
                          ) 
                    [CX] => Array 
                          ( 
                            [1] => 59
                            [2] => 59 
                            [3] => 59 
                          ) 
                    [DW] => Array 
                          ( 
                            [106] => 57
                            [107] => 57 
                            [108] => 57
                          )
             ) 

I'm doing it like this

function replace($var){
    global $array;
    for ($u=0; $u <sizeof($array) ; $u++) { 
        if ($var == $array[$u]['YEAR']){
            return $array[$u]['VALUE_AMDON'];
        }else{
            return $var;
        }
    }
}

foreach ($priceYear as $key => $value) {
    $priceYear[$key] = array_map('replace', $value);
}

But unfortunately it is not working, it is returning the initial array

If someone can help me, looks like the error is very dumb but i'm not seeing it :c

0

2 Answers 2

1

You can also do it with array functions like array_column and array_search check below:

$array_year = array_column($array, "YEAR");
foreach($priceYear as $key => $val){
    foreach($val as $innerkey => $innerval){
        // Below If year exist in $array_year function return key of $array_year else return false
        $isExistKey = array_search($innerval, $array_year);
        if($isExistKey !== FALSE){
            $priceYear[ $key ][ $innerkey ] = $array[ $isExistKey ]["VALUE_AMDON"];
        }
    }
}
echo "<pre>";
print_r($priceYear);

Check the output here: https://paiza.io/projects/gUNihGow6-CWO0eqWpEtxg?language=php

Sign up to request clarification or add additional context in comments.

Comments

0

Because there is no direct link between the keys of the arrays, the easiest way to do this is to use nested loops.

You just need to loop through your $priceYear array and check each of the "year" value acainst the YEAR value in $array. If they match, you can replace just that value in your $priceYear array.

The code below is commented to tell you what each line is doing:

// 1. loop through the nested arrays in $priceYear
foreach ($priceYear as $key => $price_years_array) {
    foreach ($price_years_array as $index => $price_years_value) {

        // 2. Create a variable to store the new value for the year in this $priceYear array, 
        // initialised to 0 (your specified default)
        $new_year = 0;

        // 3. check each year in your $array
        foreach ($array as $replacement_values){

            // 4. if $replacement_values has a YEAR and VALUE_AMDON...
            //    and $price_years_value matches the value in YEAR...
            if( $replacement_values['YEAR'] && $replacement_values['VALUE_AMDON']
                && $replacement_values['YEAR'] == $price_years_value){

                    // 5. store this in our $new_year variable
                    $new_year = $replacement_values['VALUE_AMDON'];
             }
        }
        
        // 6. after checking all years in $array, it we found a match it will be in $new_year
        // otherwise it will still be 0. 
        // Now simple set this value  - we can access this directly using $key and $index
        $priceYear[$key][$index] = $new_year;
    }
}
var_dump($priceYear);

Sample Data using years that don't exist:

$priceYear = array(
    'AZ' => array( 1 => 2021, 2 => 2020), // <-- 2021 doesn't exist in $array
    'BY' => array( 0 => 2020, 1 => 2016), // <-- 2016 doesn't exist in $array
    'CX' => array(1 => 2020, 2 => 2020, 3 => 2020),
    'DW' => array(106 => 2019, 107 => 2019, 108 => 2019)
);

$array = array(
    array('YEAR' => 2018, 'VALUE_AMDON' => 55),
    array('YEAR' => 2019, 'VALUE_AMDON' => 57),
    array('YEAR' => 2020, 'VALUE_AMDON' => 59)
);

Output:

Array
(
    [AZ] => Array
        (
            [1] => 0       // <-- value is 0 because year didn't exist in $array
            [2] => 59
        )
    [BY] => Array
        (
            [0] => 59
            [1] => 0       // <-- value is 0 because year didn't exist in $array
        ) 
    [CX] => Array
        (
            [1] => 59
            [2] => 59
            [3] => 59
        )
    [DW] => Array
        (
            [106] => 57
            [107] => 57
            [108] => 57
        )
)

5 Comments

Ty, how I make that if $replacement_values['YEAR'] != $price_years_value or even that YEAR doesn't exist $priceYear = 0
@Legion If none of the values of YEAR exist in $priceYear, then no change is made. At the moment this code uses $priceYear as the basis and only changes the year in it if it has a match in $array - otherwose nothing changes. So you want to change the year in $priceYear it to the value in $array if a match is found, and 0 otherwise - is that correct?
@Legion I've updated my answer with detailed comments and sample data to show it working
@Legion No problem! Your question showed a decent attempt at doing it yourself so that gives us something to work with and so you have a better chance if getting a solution here :) Just for future, it's best to include all requirements in the question (such as the need for 0 for non-matching years) so that you get the answer you need first time - people might not be online after they answer to see any changes :)
Yes, you are right that was my bad, I glad that you help me, ty :3

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.