如何用数组中的值交换键?

时间:2021-12-08 21:15:40

I have array like:

我有数组:

array(
  0 => 'a',
  1 => 'b',
  2 => 'c'
);

I need to convert it to:

我需要将其转换为:

array(
  'a',
  'b',
  'c'
);

What's the fastest way to swap keys with values?

用值交换键的最快方式是什么?

6 个解决方案

#1


22  

PHP has the array_flip function which exchanges all keys with their corresponding values, but you do not need it in your case because the arrays are the same.

PHP具有array_flip函数,该函数将所有键与其对应的值进行交换,但在您的例子中不需要它,因为数组是相同的。

array(
  'a',
  'b',
  'c'
);

This array has the keys 0, 1, and 2.

这个数组的键为0,1和2。

#2


4  

Use array_flip(). That will do to swap keys with values. However, your array is OK the way it is. That is, you don't need to swap them, because then your array will become:

使用array_flip()。这样就可以用值交换键。但是,数组的方式是OK的。也就是说,你不需要交换它们,因为你的数组会变成:

array(
  'a' => 0,
  'b' => 1,
  'c' => 2
);

not

array(
  'a',
  'b',
  'c'
);

#3


3  

array(
  0 => 'a',
  1 => 'b',
  2 => 'c'
);

and

array(
  'a',
  'b',
  'c'
);

are the same array, the second form has 0,1,2 as implicit keys. If your array does not have numeric keys you can use array_values function to get an array which has only the values (with numeric implicit keys).

是相同的数组,第二种形式有0 1 2作为隐式键。如果你的数组没有数字键,你可以使用array_values函数来获得一个只有数值的数组(带有数值隐式键)。

Otherwise if you need to swap keys with values array_flip is the solution, but from your example is not clear what you're trying to do.

否则,如果您需要用值array_flip交换键,则解决方案是array_flip,但是从您的示例中不清楚您要做什么。

#4


2  

See: array_flip

看:array_flip

#5


2  

$flipped_arr = array_flip($arr); will do that for you.

$ flipped_arr = array_flip(arr);我会帮你的。

(source: http://php.net/manual/en/function.array-flip.php)

(来源:http://php.net/manual/en/function.array-flip.php)

#6


2  

You'll want to use array_flip() for that.

您需要为此使用array_flip()。

#1


22  

PHP has the array_flip function which exchanges all keys with their corresponding values, but you do not need it in your case because the arrays are the same.

PHP具有array_flip函数,该函数将所有键与其对应的值进行交换,但在您的例子中不需要它,因为数组是相同的。

array(
  'a',
  'b',
  'c'
);

This array has the keys 0, 1, and 2.

这个数组的键为0,1和2。

#2


4  

Use array_flip(). That will do to swap keys with values. However, your array is OK the way it is. That is, you don't need to swap them, because then your array will become:

使用array_flip()。这样就可以用值交换键。但是,数组的方式是OK的。也就是说,你不需要交换它们,因为你的数组会变成:

array(
  'a' => 0,
  'b' => 1,
  'c' => 2
);

not

array(
  'a',
  'b',
  'c'
);

#3


3  

array(
  0 => 'a',
  1 => 'b',
  2 => 'c'
);

and

array(
  'a',
  'b',
  'c'
);

are the same array, the second form has 0,1,2 as implicit keys. If your array does not have numeric keys you can use array_values function to get an array which has only the values (with numeric implicit keys).

是相同的数组,第二种形式有0 1 2作为隐式键。如果你的数组没有数字键,你可以使用array_values函数来获得一个只有数值的数组(带有数值隐式键)。

Otherwise if you need to swap keys with values array_flip is the solution, but from your example is not clear what you're trying to do.

否则,如果您需要用值array_flip交换键,则解决方案是array_flip,但是从您的示例中不清楚您要做什么。

#4


2  

See: array_flip

看:array_flip

#5


2  

$flipped_arr = array_flip($arr); will do that for you.

$ flipped_arr = array_flip(arr);我会帮你的。

(source: http://php.net/manual/en/function.array-flip.php)

(来源:http://php.net/manual/en/function.array-flip.php)

#6


2  

You'll want to use array_flip() for that.

您需要为此使用array_flip()。

相关文章