【问题标题】:how to make the value of an array as the keys of the values of another array in PHP? [duplicate]如何使一个数组的值作为PHP中另一个数组的值的键? [复制]
【发布时间】:2012-12-14 01:47:26
【问题描述】:

可能重复:
Merging PHP array, one as Keys the other as Values?

我有一个键数组,我有一个值数组..如何将数组 A 的值作为数组 B 值的“键”?

array A

   [0] => 224
    [1] => 77
    [2] => 78
    [3] => 79
    [4] => 80
    [5] => 81
    [6] => 82
    [7] => 76
)
1

array B

Array
(
    [0] => Men Shoes
    [1] => Fashion Accessories
    [2] => Men Apparels
    [3] => Shoes & Belts
    [4] => Watches & Clocks
    [5] => Women Apparels
    [6] => Others
    [7] => Bags

我想做的就是让它变成这样

array(
 [224] => Men Shoes
 [77] => Fashion Accessories
 [78] => Men Apparells
 [79] => Shoes & Belts
 [80] => Watches & clocks
 [81] => Women Apparels
 [82] => Others
 [76] => Bags 
)

【问题讨论】:

    标签: php


    【解决方案1】:

    您可以使用array_combine()

    通过使用键数组中的值作为键和 值数组中的值作为对应的值。

    【讨论】:

      【解决方案2】:

      您可以使用array_combine()

      为您提供示例代码。

      <?PHP
      $array_a = array
      (
          0 => 224,
          1 => 77,
          2 => 78,
          3 => 79,
          4 => 80,
          5 => 81,
          6 => 82,
          7 => 76
      );
      
      $array_b = array
      (
          0 => "Men Shoes",
          1 => "Fashion Accessories",
          2 => "Men Apparels",
          3 => "Shoes & Belts",
          4 => "Watches & Clocks",
          5 => "Women Apparels",
          6 => "Others",
          7 => "Bags"
      );
      
      $new_array = array_combine($array_a,$array_b);
      
      print_r($new_array);
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 2019-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多