简体   繁体   中英

Replacing values of an array (php)

i have a question that is burning my head:

i have 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 to change the value each 10 keys into '0000'

But my head is absoutely shut down today... can you help me?

Thanks!!

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

Job done.

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

Probably a better way to do this with an array function, but off the top of my head

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

If you want every 10th to be turned into 0000, you can do that with a for loop. This can also take into account that the amount of your of values can change.

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

EDIT:

People are really sensitive, so i modified the code to not kill kittens anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM