【问题标题】:Dynamically retrieve array from another array and push it to another associative array as key value pair从另一个数组中动态检索数组并将其作为键值对推送到另一个关联数组
【发布时间】:2014-08-13 06:45:38
【问题描述】:

我正在从 api 调用中获取结果。

我将它存储在数组$phone_nums 中。数组的结构是这样的:

结果:

    array (size=2)
      0 => 
        array (size=4)
          'is_error' => int 0
          'version' => int 3
          'count' => int 3
          'values' => 
            array (size=3)
              0 => 
                array (size=4)
                  "id"            ="1"
                  'contact_id'    = "207"
                  'phone'         = "8888888888"
                  'phone_id_type' = "2"
              1 => 
                array (size=4)

                  "id"            ="2"
                  'contact_id'    = "207"
                  'phone'         = "8475895894"
                  'phone_id_type' = "2"
              2 => 
                array (size=4)
                  "id"            ="2"
                  'contact_id'    = "207"
                  'phone'         = "48948594894"
                  'phone_id_type' = "2"

      1 => 
        array (size=5)
          'is_error' => int 0
          'version' => int 3
          'count' => int 1
          'id' => int 160
          'values' => 
            array (size=1)
              0 => 
                array (size=4)
                  "id"            ="1"
                  'contact_id'    = "207"
                  'phone'         = "48948594894"
                  'phone_id_type' = "2"

现在我必须从这个数组中获取电话号码 ph.no 类型,并添加到一个新的关联数组 $ph_maps 中,键为 contact_id 并将相应的 ph 号码映射到它,如下所示。

$ph_maps = ("207"=>array(48782387489,4874843899,90483499908), 208=>array(789732187,38983298,938938)

这是我的代码。它有一些问题。

for ($i=0; $i < count($phone_nums); $i++) { 
    for ($j=0; $j < $phone_nums[$i]['count']; $j++) {
        $ph_maps = array();
        $ph_maps[$phone_nums["values"][$i]["contact_id"]] = array($phone_nums[$i]['values'][$j]['phone']);
    }      

【问题讨论】:

  • 当说“我的代码有问题”时,您应该说明问题所在。 (当前输出,错误消息,...)它使我们更容易为您提供帮助。
  • 当然.. 从现在开始我会具体说明

标签: php arrays associative-array array-push


【解决方案1】:

1 - 您每次在循环中使用$ph_maps = array(); 重置一个新数组,删除之前的条目。您当前的结果可能是一个包含单个contact_id / phone 条目的数组。把它放在循环之外。

2 - 在您的第二个循环中,您不会为每个contact_id 添加一个新条目到您的数组中,而是设置一个新的唯一条目。您必须添加 [] 以强制创建新条目。

3 - 您正在为每个电话号码添加一个新数组,而在您想要的输出中,您似乎只是想要该值,因此您应该删除 = array(...)

4 - 您的 contact_id 密钥不正确:使用 $phone_nums[$i]["values"][$j]["contact_id"] 而不是 $phone_nums["values"][$i]["contact_id"]

$ph_maps = array();
for ($i=0; $i < count($phone_nums); $i++) { 
    for ($j=0; $j < $phone_nums[$i]['count']; $j++) { 
        $ph_maps[$phone_nums[$i]["values"][$j]["contact_id"]][] = $phone_nums[$i]['values'][$j]['phone'];
    } 
}

【讨论】:

  • 感谢您的详细解答。你已经指出了错误。但是使用你的解决方案我没有得到 $contact _id 是电话号码的键。结果就像这个数组 (size=1) '' => array (size=5) 0 => string '894503213' (length=9) 1 => string '849504959' (length=9) 2 => string '9384738948 '(长度=10)3 =>字符串'9949394920'(长度=10)4 =>字符串'94494 12094'(长度=11)
  • 您的答案是正确的,但我想要的不是 1、2、3 等键,而是contact_id。非常感谢你
  • @ShrinidhiKulkarni 更新了我的答案,现在应该可以了。
  • @克莱门特马莱特...太棒了。再次感谢你。
猜你喜欢
  • 2017-07-06
  • 1970-01-01
  • 2014-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-14
  • 2020-08-23
相关资源
最近更新 更多