1

I have the array below:

$test = array(
    'key_a' => array('a', 'b'),
    'key_b' => array('c', 'd')
);

and I want to convert in the format below:

$test = array(
    array(
        'key_a' => 'a',
        'key_b' => 'c'
    ),
    array(
        'key_a' => 'b',
        'key_b' => 'd'
    )
);

Is it possible? How can I do this?

1
  • You can use $array = array_values($array);.... Commented Nov 16, 2015 at 14:29

4 Answers 4

1

I think you can use something like the following:

$test = array(
    'key_a' => array('a', 'b'),
    'key_b' => array('c', 'd')
);

$max_length = 0;
$data = array();

foreach($test as $key => $value) { //Find the max_length of all subarrays
    $this_length = count($value);
    if ($this_length > $max_length) {
        $max_length = $this_length;
    }
}

for($i = 0; $i < $max_length; $i++) { //For all subarray's values (horizontal)
    foreach($test as $key => $value) { //Foreach array (vertical)
        if (isset($value[$i])) { //If the value is set 
            $data[$i][$key] = $value[$i];
        }
    }
}

var_dump($data);

Result:

array (size=2)
  0 => 
    array (size=2)
      'key_a' => string 'a' (length=1)
      'key_b' => string 'c' (length=1)
  1 => 
    array (size=2)
      'key_a' => string 'b' (length=1)
      'key_b' => string 'd' (length=1)

Also works for uneven arrays:

$test = array(
    'key_a' => array('a', 'b', 'M'),
    'key_b' => array('c', 'd',)
);

Result:

array (size=3)
  0 => 
    array (size=2)
      'key_a' => string 'a' (length=1)
      'key_b' => string 'c' (length=1)
  1 => 
    array (size=2)
      'key_a' => string 'b' (length=1)
      'key_b' => string 'd' (length=1)
  2 => 
    array (size=1)
      'key_a' => string 'M' (length=1)
Sign up to request clarification or add additional context in comments.

Comments

1

One of the solutions would be:

$test = array(
    'key_a' => array('a', 'b'),
    'key_b' => array('c', 'd')
);

$result = array();    
foreach ($test as $arrays) {
    $result = array_merge_recursive(
        $result,
        array_combine(
            array_keys($test),
            array_chunk($arrays, 1)
        )
    );
}

foreach ($result as $key => &$value) {
    $value = array_combine(array_keys($test), $value);
}

echo '<pre>';
print_r($result);
echo '</pre>';

The result would be:

Array
(
    [key_a] => Array
        (
            [key_a] => a
            [key_b] => c
        )

    [key_b] => Array
        (
            [key_a] => b
            [key_b] => d
        )

)

3 Comments

Very good use of functions but I think it prints different structure from what OP wants.
@KostasMitsarakis you're right.. I've changed a little bit and it should work as expected now..
There are 2 approaches that works.. the OP is the one who choose which of them to use..
0

Go through your array and rebuild the new one in the same time.

function arrayTranspose($myArray) {
    $result = array();
    foreach ($myArray as $mainKey => $subArray) {
        foreach ($subArray as $subKey => $value) {
            if (!isset($result[$subKey])) 
                $result[$subKey] = array();
            $result[$subKey][$mainKey] = $value;
        }
    }
    return $result;
}

Comments

-1

Yes, it is possible. You can use array_values - This function returns all the values from the array and indexes the array numerically.

Try this:

<?php

$test = array(
    'key_a' => array('a', 'b'),
    'key_b' => array('c', 'd')
);

$test = array_values($test);

echo '<pre>';
print_r( $test );
echo '</pre>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.