fill an array with default values if it does not exist

0

Hello Good evening, I have this code

$total = array();
$months = array(1,2,3,4,5,6,7,8,9,10,11,12);

foreach ($months as $value) {
    foreach ($total_clients as $clientes) {
        if($value == $clientes->month){
            $total[$value] = (int)$clientes->total;
        } else {
            $total[$value] = 0;
        }
    }
}

The variable $total_clientes has these values

$total_clients = array(
    array(
        'month' => 5
        'total => 6
    ),
    array(
        'month' => 7
        'total => 12
    ),
    array(
        'month' => 11
        'total => 89
    ),
)

What I need is that the array $total be filled only with the values of $total_clientes depending on the index in $months taking as reference the key 'month' of the array $total_clients

something like that $total[0,0,0,0,6,0,12,0,0,0,89,0];

only this gives me 'Array

(
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 89
[12] => 0
)

'

I can not get it to fill properly, I made prints of the loops but they do not work the way I need to, they can give me a hand, thank you.

I need that to be able to pass it to a customer graph for months.

    
asked by Ivo Facundo 10.11.2017 в 06:20
source

2 answers

1

Only the one with the value 11 is being saved because the 11 is in the last position of the array. You are traversing the array to the end, and even if you save the values 6 and 12, then you are setting them to 0 again.

$total = array();
$months = array(1,2,3,4,5,6,7,8,9,10,11,12);

foreach ($months as $value) {
    $bool = false;
    //Recorremos todo el array
    foreach ($total_clients as $clientes) {
        //Comprobamos si el mes coincide. Si coincide sólo guardamos su valor de momento
        if($value == $clientes->month){
            $bool = true;
            $valor = (int)$clientes->total;
        } 
    }
    //Una vez recorrido el array entero, miramos si alguno ha coincidido o no con la variable $bool
    //Si coincidia, asignamos el valor guardado en $valor
    if ($bool == true){
        $total[$value] = $valor;
    } 
    //En caso de que no existiese ese mes en $total_clients le asignamos un 0
    else {
        $total[$value] = 0;
    }
}

This can help you, setting the value after going through all the records in the array.

    
answered by 10.11.2017 в 07:28
0

Based on your code, you can always make the value start at 0 for each month. In the subsequent comparison, you can reassign the value if necessary. The else clause is no longer necessary.

Within if, you can add the break statement, this way the execution of the second foreach will stop and the first one will move on to the next iteration.

$total = array();
$months = array(1,2,3,4,5,6,7,8,9,10,11,12);

foreach($months as $value){
    $total[$value] = 0;
    foreach ($total_clients as $clientes){
        if($value == $clientes['month']){
            $total[$value] = (int)$clientes['total'];
            break;
        }
    }
}

Greetings.

    
answered by 10.11.2017 в 10:23