0

I have an array $d_visitors = array_count_values($d_visitors);

array:7 [▼
  2 => 4
  5 => 1
  8 => 2
  3 => 1
  1 => 2
  9 => 3
  0 => 2
]

I'm trying to loop through that array 24 times, and check if the key matches, and store its value.

$dv = [];
for ($i = 0; $i < 24; $i++){
    foreach ($d_visitors as $k =>$v) {
        if($i == $k ){
            $dv[$i] = $v;
        }else{
            $dv[$i] = 0;
        }
    }
}

I'm trying to print out something like this:

array:24 [▼
  0 => 2
  1 => 2
  2 => 4
  3 => 1
  4 => 0
  5 => 1
  6 => 0
  7 => 0
  8 => 2
  9 => 3
  10 => 0
  11 => 0
  12 => 0
  13 => 0
  14 => 0
  15 => 0
  16 => 0
  17 => 0
  18 => 0
  19 => 0
  20 => 0
  21 => 0
  22 => 0
  23 => 0
]

But I kept getting this:

array:24 [▼
  0 => 2
  1 => 0
  2 => 0
  3 => 0
  4 => 0
  5 => 0
  6 => 0
  7 => 0
  8 => 0
  9 => 0
  10 => 0
  11 => 0
  12 => 0
  13 => 0
  14 => 0
  15 => 0
  16 => 0
  17 => 0
  18 => 0
  19 => 0
  20 => 0
  21 => 0
  22 => 0
  23 => 0
]
4
  • 1
    Don't need to put else part. Commented Apr 4, 2016 at 14:16
  • @Yash : By removing it, I got this array:7 [▼ 0 => 2 1 => 2 2 => 4 3 => 1 5 => 1 8 => 2 9 => 3 ] My goal is to get 24 of them. :) Commented Apr 4, 2016 at 14:19
  • Look like @u_mulder got my point. Commented Apr 4, 2016 at 14:22
  • In the else part you still iterate over values from $d_visitors that don't match your condition Commented Apr 4, 2016 at 14:22

3 Answers 3

2

Try this way:

$dv = [];
for ($i = 0; $i < 24; $i++){
    $dv[$i] = 0;
    if (isset($d_visitors[$i])) {
        $dv[$i] = $d_visitors[$i];
    }
}

More simplified is:

$dv = [];
for ($i = 0; $i < 24; $i++){
    $dv[$i] = isset($d_visitors[$i])? $d_visitors[$i] : 0;
}

The problem in your code is in line $dv[$i] = 0; as it sets to zero $dv[$i] which earlier has been set.

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

Comments

1

You need to use one flag variable. Your code is also correct.

Try like this:

$dv = [];
        $flag = 0;
        for ($i = 0; $i < 24; $i++){
            $flag = 0;
            foreach ($d_visitors as $k =>$v) {
                if($i == $k ){
                    $dv[$i] = $v;
                    $flag = 1;
                }
            }
            if($flag == 0){
                $dv[$i] = 0;
            }
        }

Comments

1

Another way of asking your process is:

How can I:

  1. merge a default array with another array based on keys
  2. and ksort() the result array?

While you can achieve your desired result using a foreach() loop and a condition statement on each iteration, I can show you a more concise, simple approach:

This is your new array of data: $array=[2=>4,5=>1,8=>2,3=>1,1=>2,9=>3,0=>2];

There are two ways to set up your default data array:

$defaults=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; // 24 elements with 0 value

or more elegantly:

$defaults=array_fill(0,24,0);

Then you only need to call array_replace() to overwrite the default values with the new values. This will keep the keys in ASC order.

var_export(array_replace($defaults,$array));

You can even nest the function calls and avoid adding variable names to the global scope like this:

$d_visitors=array_replace(array_fill(0,24,0),array_count_values($d_visitors)));

Done -- merged and ksorted.


To show some other ways that programmers might try to combine the two arrays (fruitlessly or illogically), here is a demonstration.

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.