4

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!!

1
  • Do you want the 1st element (index 0), 11th element (index 10), 21st element (index 20), etc. OR do you want the 10th element (index 9), 20th element (index 19), etc. to be '0000'??? Commented Nov 29, 2011 at 17:30

6 Answers 6

5
$length = count($x_axis);
for ($i=0; $i<$length; $i+=10)
{
  $x_axis[$i] = "0000";
}
Sign up to request clarification or add additional context in comments.

Comments

1
for ($i = 10; isset($x_axis[$i]); $i += 10) {
  $x_axis[$i] = '0000';
}

Job done.

Comments

1
foreach(range(0, count($x_axis), 10) as $i) {
        $x_axis[$i] = '0000';
}

Comments

1
array_walk($x_axis, function(&$v, $k) { if($k % 10 == 0) $v = '0000'; });

Comments

0

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';
}

Comments

-2

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.

5 Comments

Every answer was great, but for the purpose of what i'm needing, this was the one that fits best. Thank you all!
sorry, this is the worse answer compare to the rest
This one kills one kitten everytime it goes into loop. -1!
Screw kittens, kittens annoying anyway!
@ajreal: That's what I said in my previous somment :) Thanks for finding this out!

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.