1

Есть такой массив:

как его преобразовать в одномерный таким образом?

3 ответа 3

2
$array = array(
    array(
        '[email protected]',
        '[email protected]'
    ),
    array(
        array(
            '[email protected]'
        )
    ),
    "[email protected]",
    "[email protected]"
);

$emails = []; 
array_walk_recursive($array, 
    function($item, $key) use (&$emails){
        $emails[] = $item;
    }
);

var_dump($emails);

Результат:

array(5) {
  [0]=>
  string(23) "[email protected]"
  [1]=>
  string(24) "[email protected]"
  [2]=>
  string(23) "[email protected]"
  [3]=>
  string(21) "[email protected]"
  [4]=>
  string(22) "[email protected]"
}
1
$arr = ['[email protected]',
    Array(Array('[email protected]', '[email protected]',)
    ), Array(Array('[email protected]'),
    ), '[email protected]', '[email protected]', '[email protected]',
    '[email protected]',];

function change(array $arg)
{
    $items = [];
    foreach ($arg as $item) {
        if(is_array($item)){
            $items = array_merge($items, change($item));
        }else{
            $items[] = $item;
        }
    }
    return $items;
}

var_dump(change($arr));
0
$arr = [1,2,[3,4, [5,6,7], 8], 9];
$flat_arr = iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($arr)), false);
var_dump($flat_arr);

Результат:

array(9) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(8) [8]=> int(9) }

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.