-2

I have an array something like below.We need to create three array from this array.We need to seperate all for facebook inside another array and something like other twitter,email.

Is this possible?

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
        )

)

We need like this for all three(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,
}
5
  • Yes, it is possible. What have you tried? foreach seems like a good start. Commented Aug 7, 2013 at 10:08
  • OK, what is your question? Commented Aug 7, 2013 at 10:08
  • Um, ok. Have you tried anything? Commented Aug 7, 2013 at 10:09
  • How it is possible?.I am confused where we start. Commented Aug 7, 2013 at 10:09
  • Use a foreach loop. Commented Aug 7, 2013 at 10:09

3 Answers 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"];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hell you spoiled it, we wanted him to at least try :(
1

Try array_column

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

OR

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

Comments

0

You can create three arrays easily :-)
let say main array name is $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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.