php爆炸并强制数组键从1开始而不是0 [英] php explode and force array keys to start from 1 and not 0

查看:332
本文介绍了php爆炸并强制数组键从1开始而不是0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,它将被分解成一个数组,并且我们知道,输出数组键将从0开始作为第一个元素的键,从1开始代表第二个元素,依此类推。

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.

现在如何强制该数组从1开始而不是从0开始?

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?

谢谢。

推荐答案

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

完成!

这篇关于php爆炸并强制数组键从1开始而不是0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆