从其他数组创建三个数组

时间:2013-08-07 10:06:22

标签: php

我有一个像下面这样的数组。我们需要从这个数组中创建三个数组。我们需要将另一个数组中的所有内容分别用于其他数组和其他内容,如电子邮件。

这可能吗?

Array
(
    [01] => Array
        (
            [facebook] => 375
            [twitter] => 3276
            [email] => 3276
        )

    [02] => Array
        (
            [facebook] => 385
            [twitter] => 3326
            [email] => 3326
        )

    [03] => Array
        (
            [facebook] => 391
            [twitter] => 3327
            [email] => 3327
        )

    [04] => Array
        (
            [facebook] => 446
            [twitter] => 3327
            [email] => 3327
        )

    [05] => Array
        (
            [facebook] => 486
            [twitter] => 3334
            [email] => 3334
        )

    [06] => Array
        (
            [facebook] => 2
            [twitter] => 6
            [email] => 6
        )

    [07] => Array
        (
            [facebook] => 1
            [twitter] => 7
            [email] => 7
        )

    [08] => Array
        (
            [facebook] => 3
            [twitter] => 11
            [email] => 11
        )

    [09] => Array
        (
            [facebook] => 0
            [twitter] => 0
            [email] => 0
        )

    [10] => Array
        (
            [facebook] => 0
            [twitter] => 0
            [email] => 0
        )

    [11] => Array
        (
            [facebook] => 0
            [twitter] => 0
            [email] => 0
        )

    [12] => Array
        (
            [facebook] => 0
            [twitter] => 0
            [email] => 0
        )

)

我们需要三个这样的(facebook,twitter,email)

Array(
    [01]=>385,
    [02]=>375,
    [03]=>391,
    [04]=>446,
    [05]=>486,
    [06]=>2,
    [07]=>1,
    [08]=>0,
    [09]=>0,
    [10]=>0,
    [11]=>0,
    [12]=>0,
}

3 个答案:

答案 0 :(得分:2)

$your_array = //here you have your original array

$facebook = array();
$twitter = array();
$email = array();

foreach($your_array as $sub_array) {
 $facebook[] = $sub_array["facebook"];
 $twitter[] = $sub_array["twitter"];
 $email[] = $sub_array["email"];
}

答案 1 :(得分:1)

尝试array_column

$fb=array_column($array, 'facebook');

OR

$fb=array_map( function ($i) { return $i['facebook'];},$array);

答案 2 :(得分:0)

您可以轻松创建三个阵列:-)
   假设主阵列名称是$ mainarray     


    $facebookarr = array();
    $twitterarr = array();
    $emailarr = array();
    foreach($mainarray as $socialarray)
    {
      $facebookarr[]  =  $socialarray['facebook'];
      $twitterarr[] =   $socialarray['twitter']
      $emailarr[] =     $socialarray['email']
    }
    echo '<pre>';
    print_r($facebookarr);
    print_r($twitterarr);
    print_r($emailarr);