views:

200

answers:

7

how would you convert this array:

Array
(
    [0] => Array
        (
            [Contact] => Array
                (
                    [number] => 0425 234 634
                )

        )

    [1] => Array
        (
            [Contact] => Array
                (
                    [number] => 2939 492 235
                )

        )

)

into this array

Array
(
    [0] => 0425 234 634
    [1] => 2939 492 235
)

?

+4  A: 

A very dirty method, don't think there is a function to do this for you though.

foreach($array1 as $key => $value) {
    $array2[$key]=$value['contact']['number'];
}

EDIT:

Actually, array_values might be of some help, would be worth testing it against your multi-dimensional array.

ILMV
not too, dirty, since the given structure is so specific.
xtofl
Very true, cheers :-)
ILMV
values in the mentioned array are in fact arrays themselves thus turning array_values useless in the current context. try it!
Peter Lindqvist
It may well be, didn't get chance to test it with a multi-dimensional array :-)
ILMV
thank you, i tried array_values() there are some interesting ways to flatten arrays in the manual http://php.net/manual/en/function.array-values.php ill try your solution too, if it works you get a tick ;)
ondrobaco
you can use array_shift to flatten your array. Check out my answer below
Andrei Serdeliuc
worked :). just one adjustment needed: contact => Contact
ondrobaco
:O whoops, hope that helped.
ILMV
Use CakePHP's Set::extract function (similar to Set::classicExtract() below) - do this in 1 line of code not 3, and it's far more readable.
neilcrookes
+1  A: 

Here you go, fully functional and without any assumptions about your original array :)

<?php
$array = array(
    0 => array(
        'Contact' => array(
            'number' => 123123123
        )
    ),
    1 => array(
        'Contact' => array(
            'number' => 123123123
        )
    ),
    2 => array(
        'Contact' => array(
            'number' => 123123123
        )
    ),
);


function flattenArray(array $arr, &$newArr) {
    while($array = array_shift($arr)) {
        if(is_array($array)) {
            flattenArray($array, $newArr);
        } else {
            $newArr[] = $array;
        }
    }
}

$newArr = array();

foreach($array as $key => $value) {
    flattenArray($value, $newArr);
}
Andrei Serdeliuc
+1  A: 
class DeepCollect {
  protected $arr = array();

  public function collect($item, $key) {
    $this->arr[] = $item;
  }

  public static function collect_all($array) {
    $collect = new self;
    array_walk_recursive($array, array($collect, "collect"));
    return $collect->arr;
  }
}

print_r(DeepCollect::collect_all($input_array));

Will work for any nested-array irrespective of key combinations.

Lukman
Nice and clean method.. I vote your's over my own. :)
Peter Lindqvist
+2  A: 

I've only seen solutions using a seperate array to store the results, this would work just as fine:

<?php
    foreach($numbers as $key => $number) {
        $numbers[$key] = $number['Contact']['number'];
    }
?>
Daniel
+4  A: 

Since you are using CakePHP, there is an included utility library that does just what you need.

$a = array(
    0 => array(
        'Contact' => array(
                'number' => "0425 234 634"
        )
    ),
    1 => array(
        'Contact' => array(
                'number' => "2939 492 235"
        )
    )
);

$b = Set::classicExtract($a, '{n}.Contact.number');

print_r($b);

And the result is:

Array
(
    [0] => 0425 234 634
    [1] => 2939 492 235
)
duckyflip
Why not the modern `Set::extract('/Contact/number', $a)`?
deceze
+1  A: 

This should work also with

Set:extract( "{n}.Contact.number", $a );
Set::extract( "/number", $a );
Abba Bryant
great, thanks for the info, i'll check that out.
ondrobaco
+7  A: 

See Set::extract() It uses xpath notation to extract node values from paths in arrays.

$numbers = Set::extract('/Contact/number', $numbers);

Achieve all this in 1 line in far more understandable code than other examples suggested here.

neilcrookes
+1 for mentioning current usage of Set::extract (xpath as opposed to dot notation)
deizel