0

I have a multidimensional array like so:

$neighborhood => array(
  'the_smiths' => array(
    'dad'      => 'Donald',
    'mom'      => 'Mary',
    'daughter' => 'Donna',
    'son'      => 'Samuel'
  )
  'the_acostas' => array(
    'dad'      => 'Diego',
    'mom'      => 'Marcela',
    'daughter' => 'Dominga',
    'son'      => 'Sergio'
  )
);

I would like to create another array (let's call it $array_of_moms) of all the moms in the neighborhood. Pulling them all in separately is doable, but not practical (like so):

$array_of_moms = array(
  $neighborhood['the_smiths']['mom'],
  $neighborhood['the_acostas']['mom'],
)

How do I create something like this:

$array_of_moms = $neighborhood['mom'];
1
  • Hmmm, where are you pulling this stuff from? Because you can always shove that into a database, and that work make things a lot easier to handle in the long run Commented Sep 13, 2011 at 23:54

3 Answers 3

1
$moms = array();
foreach($neighborhood as $family)
{
    $moms[] = $family['mom'];
}

This'll iterate through each family in the array and add the mom to the new $moms array.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, I was writing the exact same answer - down to the variable names. Here, have a +1
0

Using foreach, you can iterate through an array with variable indicies.

$array_of_moms = array();

foreach ($neighborhood AS $family) {
    $array_of_moms[] = $family['mom']; // append mom of each family to array
}

1 Comment

You need to declare $array_of_moms as an array otherwise you'll get a NOTICE
0

If you can manipulate your array, you could:

<?php

$neighborhood = array(
  'families' => array(
    'the_smiths' => array(
      'dad'      => 'Donald',
      'mom'      => 'Mary',
      'daughter' => 'Donna',
      'son'      => 'Samuel'
      ),
    'the_acostas' => array(
      'dad'      => 'Diego',
      'mom'      => 'Marcela',
      'daughter' => 'Dominga',
      'son'      => 'Sergio'
    )
  )
);

foreach ($neighborhood['families'] as $family => $folks) {
    $neighborhood['moms'][] = $folks['mom'];
}

print_r($neighborhood);

?>

Which outputs:

Array
(
    [families] => Array
        (
            [the_smiths] => Array
                (
                    [dad] => Donald
                    [mom] => Mary
                    [daughter] => Donna
                    [son] => Samuel
                )

            [the_acostas] => Array
                (
                    [dad] => Diego
                    [mom] => Marcela
                    [daughter] => Dominga
                    [son] => Sergio
                )

        )

    [moms] => Array
        (
            [0] => Mary
            [1] => Marcela
        )

)

http://codepad.org/xbnj5UmV

2 Comments

I'm not sure what you mean. Did you try the demo?
I posted the comment before you changed your answer; I see what you are doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.