-2

我有一个类似下面的数组。我们需要从这个数组中创建三个数组。我们需要在另一个数组中为 facebook 和其他类似 twitter、电子邮件的东西分开。

这可能吗?

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
        )

)

我们三个都需要这样(脸书、推特、电子邮件)

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,
}
4

3 回答 3

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"];
}
于 2013-08-07T10:09:25.837 回答
1

试试array_column

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

或者

$fb=array_map( function ($i) { return $i['facebook'];},$array);
于 2013-08-07T10:31:15.167 回答
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);

于 2013-08-07T10:16:06.963 回答