Picking indexes with equal keys from a non-associative array

Asked

Viewed 91 times

0

How do I pick/group indexes with equal keys of an array, 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)
  • 2

    You could put the result in code to help right? =)

  • I’ll put....

  • And put the expected end result

  • I put more information... See if improved.

  • The question is good.

  • I believe my answer is right. But take the test.

Show 1 more comment

1 answer

2


You can do it like 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 working

  • Its output was as I need, but there in the first block of code I passed the indexes can go beyond [0,1, 2]. Testing your script when I pass my array, it doesn’t work. Where to stay [field_*] => value ends up returning 0 => ''

  • I’m running some tests here with your code, but I believe the problem is with the.

  • @Rodrigofontes Calm and... I’ll fix.

  • @Rodrigofontes see now how it turned out

  • @Rodrigofontes Alters the $array by the name of its variable

  • 1

    Wow, your solution worked :D Quite different from what I was trying to do.

  • @Rodrigofontes glad it worked out! =)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.