Picking indexes with equal keys from a non-associative array

0

How do I get / group indexes with equal keys in an array, for example:

Array
(
    [field_label] => Array
        (
            [0] => Texto
            [1] => Checkbox
            [2] => URL
        )

    [field_type] => Array
        (
            [0] => text
            [1] => checkbox
            [2] => url
        )

    [field_value] => Array
        (
            [0] => 
            [1] => valor_1 | Valor 1
valor_2 | Valor 2
            [2] => 
        )

    [field_required] => Array
        (
            [0] => on
            [1] => on
            [2] => on
        )

    [field_order] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)

I want the array to look like this:

array (size=3)
  0 => 
    array (size=5)
      'field_label' => string 'Texto' (length=5)
      'field_type' => string 'text' (length=4)
      'field_value' => string '' (length=0)
      'field_required' => string 'on' (length=2)
      'field_order' => string '1' (length=1)
  1 => 
    array (size=5)
      'field_label' => string 'Checkbox' (length=8)
      'field_type' => string 'checkbox' (length=8)
      'field_value' => string 'valor_1 | Valor 1 valor_2 | Valor 2 | valor_3 | Valor 3' (length=55)
      'field_required' => string 'on' (length=2)
      'field_order' => string '2' (length=1)
  2 => 
    array (size=5)
      'field_label' => string 'URL' (length=3)
      'field_type' => string 'url' (length=3)
      'field_value' => string '' (length=0)
      'field_required' => string '' (length=0)
      'field_order' => string '3' (length=1)
    
asked by anonymous 12.08.2018 / 19:57

1 answer

2

You can do this:

EDITION

The problem was that your array is inside another array. So I had to change the code for this:

$newArray = [];

$maxKeys = count(max($array[0])); // conta o número máximo de chaves internas do array

for($x = 0; $x < $maxKeys; $x++){
    foreach ($array[0] as $key => $arr){
        if(isset($arr[$x])){
            $newArray[$x][$key] = $arr[$x];
        } else {
            $newArray[$x][$key] = "";
        }

    }
}

print_r($newArray);

See it working

    
12.08.2018 / 20:23