开发者

array conversion in php

开发者 https://www.devze.com 2022-12-11 09:21 出处:网络
how would you convert this array: Array ( [0] => Array ( 开发者_Go百科[Contact] => Array ( [number] => 0425 234 634

how would you convert this array:

Array
(
    [0] => Array
        (
      开发者_Go百科      [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
)

?


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.


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.


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
)


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'];
    }
?>


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);
}


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.


This should work also with

Set:extract( "{n}.Contact.number", $a );
Set::extract( "/number", $a );
0

精彩评论

暂无评论...
验证码 换一张
取 消