2

I have a string that will be exploded to get an array, and as we know, the output array key will start from 0 as the key to the first element, 1 for the 2nd and so on.

Now how to force that array to start from 1 and not 0?

It's very simple for a typed array as we can write it like this:

array('1'=>'value', 'another value', 'and another one');

BUT for an array that is created on the fly using explode, how to do it?

Thanks.

ghoti
  • 45,319
  • 8
  • 65
  • 104
medk
  • 9,233
  • 18
  • 57
  • 79

6 Answers6

20
$exploded = explode('.', 'a.string.to.explode');
$exploded = array_combine(range(1, count($exploded)), $exploded);
var_dump($exploded);

Done!

CodeAngry
  • 12,760
  • 3
  • 50
  • 57
  • @Eineki It's short, explicit and light on cycles. Takes 0.02 milliseconds as in 0.00002 seconds. Not too shabby :) – CodeAngry Oct 30 '12 at 22:55
  • I would have preferred `array_unshift($exploded,'');unset($exploded[0]);`, shorter and clearer to read (to me, at least) – Eineki Oct 30 '12 at 23:02
  • @Eineki Maybe... but what is he wants to re-base starting with 10? It's easier to alter my code to move base value the way he needs it. I write reusable code. I never focus on the exact problem. I try to satisfy all needs :) – CodeAngry Oct 30 '12 at 23:04
  • @Claudrian: I prefer to solve a problem a time and generalize the solution only when needed (for a generalized version of this problem see in my solution down here). By the way, I won't call this code reusable as is not a function/method, it is not parametrized and you have to modify it in order to use it elsewhere. Alas, is not time of flames, your solution is quit smart, I just stated it was convoluted to me and pointed out a more straightforward way to accomplish the task, nothing more. – Eineki Oct 30 '12 at 23:32
  • 1
    @Eineki - I disagree. I believe that *most* questions presented here at StackOverflow are [XY problems](http://mywiki.wooledge.org/XyProblem), and that generalized solutions have greater long-term benefit to future viewers of the site. Sure, you can solve *just* this problem, but what's the point of that? I don't want to see your one-off answers. I want answers that I can use in *my* code. Without having to wait for answers to another silly duplicate question. That said ... I like your answer. :) – ghoti Oct 31 '12 at 01:16
  • @ghoti You know, I'm just a programmer, not a mind reader. If a user can't write a proper question it *his* fault, not mine. My responses aren't one-off ones usually (they are almost all functions for this reason). They are not optimized or over generalized as premature optimization is evil. If you can't adapt my code to your needs then you can use the code as a hint to craft your own, or ask for a different solution if you are lazy or unable to cope with the code. Just my two cents.. :) – Eineki Nov 02 '12 at 16:08
3

Just use a separator to create a dummy element in the head of the array and get rid of it afterwards. It should be the most efficient way to do the job:

function explode_from_1($separator, $string) {
    $x = explode($separator, $separator.$string);
    unset($x[0]);
    return $x;
}

a more generic approach:

function explode_from_x($separator, $string, $offset=1) {
    $x = explode($separator, str_repeat($separator, $offset).$string);
    return array_slice($x,$offset,null,true);
}
Eineki
  • 14,773
  • 6
  • 50
  • 59
1
$somearray = explode(",",$somestring);

foreach($somearray as $key=>$value)
{
   $otherarray[$key+1] = $value;
}

well its dirty but isn't that what php is for...

nathan hayfield
  • 2,627
  • 1
  • 17
  • 28
1

Nate almost had it, but needed a temporary variable:

$someArray = explode(",",$myString);
$tempArray = array();

foreach($someArray as $key=>$value) {
   $tempArray[$key+1] = $value;
}
$someArray = $tempArray;

codepad example

SomeKittens
  • 38,868
  • 19
  • 114
  • 143
1
$array = array('a', 'b', 'c', 'd');

$flip = array_flip($array);
foreach($flip as &$element) {
    $element++;
}
$normal = array_flip($flip);
print_r($normal);

Try this, a rather funky solution :P

EDIT: Use this instead.

$array = array('a', 'b', 'b', 'd');
$new_array = array();

$keys = array_keys($array);
for($i=0; $i<count($array); $i++) {
    $new_array[$i+1] = $array[$i];
}
print_r($new_array);
Flosculus
  • 6,880
  • 3
  • 18
  • 42
  • 1
    In your first try, `array_flip()` will lose data in this case if you have any repeated data in your values. – ghoti Oct 30 '12 at 22:45
0

I agree with @ghoti that this task is probably an XY Problem. I can't imagine a valid/professional reason to start keys from 1 -- I've never needed this functionality in over 10 years of development. I'll offer a compact looping approach, but I'll probably never need it myself.

After instatiating a counter which is one less than the desired first key, you can use a body-less foreach() as a one-liner.

Code: (Demo)

$i = 0;
$result = [];
foreach (explode('.', 'a.string.to.explode') as $result[++$i]);
var_export($result);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136