-1

I have an array like this:

Array
(
    [0] => Array
        (
            [0] => element 1
            [1] => Array
                (
                    [0] => white house history
                    [1] => white house historical association
                    [2] => white house hotel istanbul
                    [3] => white house high school
                )

        )

    [1] => Array
        (
            [0] => element 2
            [1] => Array
                (
                    [0] => white kilt hose
                    [1] => white knight hose
                    [2] => white house kennels
                    [3] => white house kinsale
                )

        )

    [2] => Array
        (
            [0] => element 3
            [1] => Array
                (
                    [0] => white house news
                    [1] => white house nannies
                    [2] => white house number
                    [3] => white house nominations

                )

        )

I want to create a new array that contains only elements from inside each array:

[0] => white house history
[1] => white house historical association
[2] => white house hotel istanbul
[3] => white house high school
[4] => white kilt hose
[5] => white knight hose
[6] => white house kennels
[7] => white house kin sale
[8] => white house news
[9] => white house nannies
[10] => white house number
[11] => white house nominations

Does anybody know how to do this?

RichardBernards
  • 3,146
  • 1
  • 22
  • 30
miki22
  • 65
  • 1
  • 6

2 Answers2

0
<?php
$names = array();
foreach ($arr as $k => $v) {
    if (! empty($v[1])) {
        foreach ($v[1] as $elem) {
            $names[] = $elem;
        }
    }
}
?>

Explanation:

You need all the elements which reside in your second part of every array.

Loop over the main array.

Go to the second part of the child array, these are the names you want.

Just append them to a blank (required) array and we get the output.

Please see demo

Pupil
  • 23,834
  • 6
  • 44
  • 66
0

Normally I'd encourage you to give it a try first, but in the interest of teaching, here's some commented code:

$newArray = []; // use array() for PHP before 5.4, I think
foreach($oldArray as $subArray) { // process top-level elements
    bool $firstElement = true;  // flag to indicate if we've gone past 1 element yet
    foreach($subArray as $element) { // process second-level elements
        if(!firstElement && is_array($element)) { // look for arrays that come after first element
            $newArray = array_merge($newArray, $element);
        }
        $firstElement = false;  // unset the flag
    }
}

edit: after requirements were clarified, implemented nested loops and $firstElement flag

RobP
  • 9,144
  • 3
  • 20
  • 33
  • Other solutions assume all the values are exactly 2 levels deep; I had assumed you wanted the values no matter how many levels deep they are nested. – RobP Dec 09 '14 at 10:51
  • Doesn't that simply gather *all* elements? But OP wants to exclude the values at level 2 it seems. – Ja͢ck Dec 09 '14 at 11:01
  • Jack, the way I read it, gathering all the elements (leaf nodes) is exactly what OP wanted. – RobP Dec 09 '14 at 11:02
  • I don't see the "element 1", "element 2", etc. in their desired output, although they definitely are leaves. – Ja͢ck Dec 09 '14 at 11:04
  • Good point, but we all seem to be guessing what general rule OP actually was trying to implement. I'll correct the code as soon as OP explains what the logic should be. (Leaves at level 2 and below? Leaves where there are no "peer" nodes that are arrays?) – RobP Dec 09 '14 at 11:06
  • Oh, the pattern is to always skip the first element of each element ... apparently =S – Ja͢ck Dec 09 '14 at 11:13
  • sounds plausible :) new code posted. – RobP Dec 09 '14 at 11:19