Replacing array values โ€‹โ€‹(php)

I have a question that burns my head:

i has an array $x_axis[] filled with 357 values

 $x_axis[0] = '1234' $x_axis[1] = '2345' ..... $x_axis[356] = '678' 

What I need to do is change the value every 10 keys to "0000"

But my head is completely turned off today ... can you help me?

Thanks!!

+4
source share
6 answers

If you want every tenth to be converted to 0000, you can do this with a for loop. This may also take into account that the number of your values โ€‹โ€‹may change.

 $length = count($x_axis); for($i=0;$i<$length;$i+=10) { if($i%10==0) { $x_axis[$i] = '0000'; } } 

EDIT:

People are very sensitive, so I changed the code so as not to kill kittens anymore.

-2
source
 $length = count($x_axis); for ($i=0; $i<$length; $i+=10) { $x_axis[$i] = "0000"; } 
+5
source
 for ($i = 10; isset($x_axis[$i]); $i += 10) { $x_axis[$i] = '0000'; } 

The task is completed.

+1
source
 foreach(range(0, count($x_axis), 10) as $i) { $x_axis[$i] = '0000'; } 
+1
source
 array_walk($x_axis, function(&$v, $k) { if($k % 10 == 0) $v = '0000'; }); 
+1
source

Probably the best way to do this is with an array function, but from the top of the head

 $arrayLen = count($x_axis) for($index=0; $index<$arrayLen; $index+=10) { $x_axis{$index] = '0000'; } 
0
source

Source: https://habr.com/ru/post/1383694/


All Articles