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.

0

6 Answers 6

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

Done!

Sign up to request clarification or add additional context in comments.

6 Comments

@Eineki It's short, explicit and light on cycles. Takes 0.02 milliseconds as in 0.00002 seconds. Not too shabby :)
I would have preferred array_unshift($exploded,'');unset($exploded[0]);, shorter and clearer to read (to me, at least)
@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 :)
@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 - I disagree. I believe that most questions presented here at StackOverflow are XY problems, 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. :)
|
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);
}

Comments

1
$somearray = explode(",",$somestring);

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

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

1 Comment

That won't work. All of the elements of $somearray will be the same.
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

Comments

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);

1 Comment

In your first try, array_flip() will lose data in this case if you have any repeated data in your values.
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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.