如何使用索引作为键将PHP数组转换为关联数组?

时间:2022-09-26 08:21:42

I have the following array:

我有以下数组:

$foo = ["hello", "hi", [5, 10]];

I want to convert this to an associative array like so:

我想把它转换成一个关联数组

$foo = [
   "0" => "hello",
   "1" => "hi",
   "2" => [5, 10],
];

How can I do this?

我该怎么做呢?

UPDATE

The reason I want to do this is because when I execute the shuffle method, I want to know what the original index was.

我这样做的原因是,当我执行shuffle方法时,我想知道最初的索引是什么。

For example shuffle($foo) might return:

例如,shuffle($foo)可能返回:

$foo = [
   "1" => "hi",
   "2" => [5, 10],
   "0" => "hello",
];

4 个解决方案

#1


2  

This is your array:

这是你的数组:

$foo = ["hello", "hi", [5, 10]];

It's already associative. PHP adds keys for you, so it's the same as doing:

它已经关联。PHP为您添加键,与以下操作相同:

$foo = [0 => "hello", 1 => "hi", 2 => [5, 10]];

If you use string keys that are numbers, PHP converts them to numbers for you.

如果你使用数字的字符串键,PHP将它们转换为数字。

Your array doesn't need to be "associative" to make shuffle behave as you want. All PHP arrays are associative. You are looking for the wrong solution to your problem.

您的数组不需要“关联”才能使shuffle按照您的意愿运行。所有PHP数组都是关联的。你在寻找解决你问题的错误方法。

#2


2  

(See Andrea's answer, this is incorrect)

(看Andrea的回答,这是错误的)

The only transformation from the previous state is that the keys are strings, rather than integers.

前一状态的唯一转换是键是字符串,而不是整数。

$keys = array_map('strval', array_keys($foo));
$values = array_values($foo);
$foo = array_combine($keys, $values);

#3


1  

There is no need to do something like this

没有必要做这样的事

The values are already "indexed". If you run

这些值已经被“索引”。如果你运行

$foo = ["hello", "hi", [5, 10]];
echo $foo[0];

You will get hello (starting from 0)

您将获得hello(从0开始)

#4


0  

Don't know what you're trying to achive, but as I see you want to convert your array indexes into strings:

不知道你想要的是什么,但是我看到你想要把你的数组索引转换成字符串:

$_foo = [
   "0" => "hello",
   "1" => "hi",
   "2" => [5, 10],
];
$foo = [];

foreach($_foo as $key => $value) {
    $foo[(string) $key] = $value;
}

var_dump($foo);

But I think that's just redicilous, to convert array indexes into strings. I don't know if you're aware, but you already can access your array with indexes: 0, 1 or 2.

但是我认为把数组索引转换成字符串是有意义的。我不知道你是否知道,但是你已经可以用索引访问你的数组:0,1或2。

var_dump($foo[1]); // hi

#1


2  

This is your array:

这是你的数组:

$foo = ["hello", "hi", [5, 10]];

It's already associative. PHP adds keys for you, so it's the same as doing:

它已经关联。PHP为您添加键,与以下操作相同:

$foo = [0 => "hello", 1 => "hi", 2 => [5, 10]];

If you use string keys that are numbers, PHP converts them to numbers for you.

如果你使用数字的字符串键,PHP将它们转换为数字。

Your array doesn't need to be "associative" to make shuffle behave as you want. All PHP arrays are associative. You are looking for the wrong solution to your problem.

您的数组不需要“关联”才能使shuffle按照您的意愿运行。所有PHP数组都是关联的。你在寻找解决你问题的错误方法。

#2


2  

(See Andrea's answer, this is incorrect)

(看Andrea的回答,这是错误的)

The only transformation from the previous state is that the keys are strings, rather than integers.

前一状态的唯一转换是键是字符串,而不是整数。

$keys = array_map('strval', array_keys($foo));
$values = array_values($foo);
$foo = array_combine($keys, $values);

#3


1  

There is no need to do something like this

没有必要做这样的事

The values are already "indexed". If you run

这些值已经被“索引”。如果你运行

$foo = ["hello", "hi", [5, 10]];
echo $foo[0];

You will get hello (starting from 0)

您将获得hello(从0开始)

#4


0  

Don't know what you're trying to achive, but as I see you want to convert your array indexes into strings:

不知道你想要的是什么,但是我看到你想要把你的数组索引转换成字符串:

$_foo = [
   "0" => "hello",
   "1" => "hi",
   "2" => [5, 10],
];
$foo = [];

foreach($_foo as $key => $value) {
    $foo[(string) $key] = $value;
}

var_dump($foo);

But I think that's just redicilous, to convert array indexes into strings. I don't know if you're aware, but you already can access your array with indexes: 0, 1 or 2.

但是我认为把数组索引转换成字符串是有意义的。我不知道你是否知道,但是你已经可以用索引访问你的数组:0,1或2。

var_dump($foo[1]); // hi