0

Hi I have a PHP array with a variable number of keys (keys are 0,1,2,3,4.. etc) I want to process the first value differently, and then the rest of the values the same. What's the best way to do this?

6 Answers 6

3
$first = array_shift($array);
// do something with $first

foreach ($array as $key => $value) {
    // do something with $key and $value
}
Sign up to request clarification or add additional context in comments.

1 Comment

I thought about this as an answer but decided against it because it would modify the input array. If this does not matter and the array can safely be modified, this is the best answer and you shall have a +1 :-)
3

I would do this:

$firstDone = FALSE;
foreach ($array as $value) {
  if (!$firstDone) {
    // Process first value here
    $firstDone = TRUE;
  } else {
    // Process other values here
  }
}

...but whether that is the best way is debatable. I would use foreach over any other method, because then it does not matter what the keys are.

Comments

2

Here is one way:

$first = true;
foreach($array as $key => $value) {
    if ($first) {
        // something different
        $first = false;
    }
    else {
        // regular logic
    }
}

1 Comment

Have a +1 then, I don't penalise for slow typing ;-)
1
   $i = 0;
   foreach($ur_array as $key => $val) {
      if($i == 0) {
         //first index

      }
      else {
         //do something else
      }

      $i++;
   }

Comments

0

I would do it like this if you're sure the array contains at least one entry:

processFirst($myArray[0]);

for ($i=1; $i<count($myArray); $1++)
{
    processRest($myArray[$i]);
}

Otherwise you'll need to test this before processing the first element

Comments

0

I've made you a function!

function arrayCallback(&$array) {
    $callbacks = func_get_args(); // get all arguments
    array_shift($callbacks); // remove first element, we only want the callbacks

    $callbackindex = 0;

    foreach($array as $value) {
        // call callback
        $callbacks[$callbackindex]($value);

        // make sure it keeps using last callback in case the array is bigger than the amount of callbacks
        if(count($callbacks) > $callbackindex + 1) {
            $callbackindex++;
        }
    }
}

If you call this function, it accepts an array and infinite callback arguments. When the array is bigger than the amount of supplied functions, it stays at the last function.

You can simply call it like this:

arrayCallback($array, function($value) {
    print 'callback one: ' . $value;
}, function($value) {
    print 'callback two: ' . $value;
});

EDIT

If you wish to avoid using a function like this, feel free to pick any of the other correct answers. It's just what you prefer really. If you're repeatedly are planning to loop through one or multiple arrays with different callbacks I suggest to use a function to re-use code. (I'm an optimisation freak)

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.