在PHP中用现有数组构造一个新数组

时间:2015-10-29 18:04:04

标签: php arrays

我有一个$accounts数组,其中包含此数据

array:2 [▼
  0 => array:25 [▼
    "email_address" => "bob@xyzcorp.com"
    "account_id" => 111
    "password" => "abc"
    "account_type" => "admin"
    "name_prefix" => "Mr"
    "first_name" => "Bob"
    "middle_names" => "X."
    "last_name" => "Jones"
    "name_suffix" => "Jr."
    "non_person_name" => false
    "DBA" => ""
    "display_name" => "BobJ"
    "address1" => "111 Park Ave"
    "address2" => "Floor 4"
    "address3" => "Suite 4011"
    "city" => "New York"
    "state" => "NY"
    "postal_code" => "10022"
    "nation_code" => "USA"
    "phone1" => "212-555-1212"
    "phone2" => ""
    "phone3" => ""
    "time_zone_offset_from_utc" => -5
    "customer_type" => 2
    "last_updated_utc_in_secs" => 200200300
  ]
  1 => array:25 [▼
    "email_address" => "tom@xyzcorp.com"
    "account_id" => 112
    "password" => "abd"
    "account_type" => "mbn"
    "name_prefix" => "Mr"
    "first_name" => "Tom"
    "middle_names" => "Z."
    "last_name" => "Smith"
    "name_suffix" => "Sr."
    "non_person_name" => false
    "DBA" => ""
    "display_name" => "TomS"
    "address1" => "112 Park Ave"
    "address2" => "Floor 3"
    "address3" => "Suite 3011"
    "city" => "New York"
    "state" => "NY"
    "postal_code" => "10022"
    "nation_code" => "USA"
    "phone1" => "212-555-2323"
    "phone2" => ""
    "phone3" => ""
    "time_zone_offset_from_utc" => -5
    "customer_type" => 2
    "last_updated_utc_in_secs" => 200200300
  ]
]

我想从这个数组中构造一个新数组。 但新阵列我只想拥有2个键:email_addresspassword

我试过了:

$new_array = array();
$i = 0;
foreach ($accounts as $account) {

  $new_array[$i++];

  foreach ($account as $user) {
    $new_array['email_address'] = $user['email_address'];
    $new_array['password'] = $user['password'];
  }
}

dd($new_array);

我希望有人能在这里给我一点推动。

2 个答案:

答案 0 :(得分:2)

您没有向$new_array添加新元素。每次通过foreach循环,您都会覆盖相同的两个键。 $new_array的元素应该是数组。

您也不需要内部循环,因为每个帐户只是一个用户。

foreach ($accounts as $account) {
    $new_array[] = array('email_address' => $account['email_address'],
                         'password' => $account['password']);
}

您不需要变量$i,因为$new_array[] =会将新元素推送到数组上。

答案 1 :(得分:1)

 $new_array[$i++];

  foreach ($account as $user) {
    $new_array['email_address'] = $user['email_address'];
    $new_array['password'] = $user['password'];
  }

您的问题就在这里,您几乎拥有它,但是当您在数组中使用数组时,使用两组foreach循环,您需要具有相同的如果要保留多个记录“passwordemail_address

,请使用结构深度

所以,用以下内容替换上面的引用:

$i++
 $new_array[$i];

  foreach ($account as $user) {
    $new_array[$i]['email_address'] = $user['email_address'];
    $new_array[$i]['password'] = $user['password'];
  }

这将输出

$new_array(
    [0] ===> ['password'] --> {data}
        ===> ['email_address'] --> {data}

    [1] ===> ['password'] --> {data}
        ===> ['email_address'] --> {data}
    ....
)

修改 这可以进一步减少和整理为:

   $i++
     $new_array[$i];

        $new_array[$i]['email_address'] = $account['email_address'];
        $new_array[$i]['password'] = $account['password'];

因为你知道你想要的值的键,所以你不需要分开外部数组来找到任何东西,你可以直接引用 outerValue ({{1} })然后account等)。