在多维数组中按键获取数组中的值

时间:2017-01-03 18:11:14

标签: php arrays

我正在尝试获取每个字段的值,例如。没有translation的子数组中的foreach。可能吗?

Array
(
    [0] => Array
        (
            [group_id] => 11
            [group_translation] => Extras
            [id] => 21
            [operation] => +
            [price] => 5
            [price_by] => once
            [price_type] => fixed
            [translation] => Pick up
            [price_total] => 5
        )

    [1] => Array
        (
            [group_id] => 11
            [group_translation] => Extras
            [id] => 22
            [operation] => +
            [price] => 10
            [price_by] => once
            [price_type] => fixed
            [translation] => Drinks
            [price_total] => 10
        )

)

谢谢!

1 个答案:

答案 0 :(得分:0)

当然有可能:

<?php
$data = [
    [
        'group_id' => 11,
        'group_translation' => 'Extras',
        'id' => 21,
        'operation' => '+',
        'price' => 5,
        'price_by' => 'once',
        'price_type' => 'fixed',
        'translation' => 'Pick up',
        'price_total]' => 5
    ],
    [
        'group_id' => 11,
        'group_translation' => 'Extras',
        'id' => 22,
        'operation' => '+',
        'price' => 10,
        'price_by' => 'once',
        'price_type' => 'fixed',
        'translation' => 'Drinks',
        'price_total' => 10
    ]
];

$extract = [];
array_walk($data, function($element) use (&$extract) {
    $extract[] = $element['translation'];
});
var_dump($extract);

输出显然是:

array(2) {
  [0] =>
  string(7) "Pick up"
  [1] =>
  string(6) "Drinks"
}