Hi all

I need to get an array to start at 1 instead of 0 but by using the explode() function on a string.

Is it possible?

$myarray = explode(",",$mystring);

How can I get the first value in the array to be paired with key[1]?

Thanks in advance

    The first question to address is: why? Assuming there is some good reason, you could do:

    $values = explode(',', $string);
    $keys = range(1, count($values));
    $myArray = array_combine($keys, $values);
    
      $str = array('0', '1', '2', '3');
      array_shift($str);
      echo '<pre>';
         print_r($str);
      echo '</pre>';
      

      ouput:

      Array
      (
          [0] => 1
          [1] => 2
          [2] => 3
      )
      

      EDIT - If you need to protect the original array, simply create a new variable that is equal to the orginal array and shift that new variable:

      $str = $str2 = array('0', '1', '2', '3');
      array_shift($str2);
      echo '<pre>';
         print_r($str2);
      echo '</pre>';
      

      $str still remains intact in its original form.

        After asking the same question as NogDog (why? Arrays work quite happily already), I throw in my own take:

        array_unshift($myarray, 'fnord');
        unset($myarray[0]);
        
          12 days later

          // No other functions are needed...
          // Simply pre-pend a comma
          $myarray = explode( ",", "," . $mystring);

            That still starts the array from index 0, except that one can now ignore that element.

              Write a Reply...