2

I have a multidimensional array with 3 keys ("length", "width" and "height). Each key is associated with an array of values:

$data = [
    "length" => ["12", "44"],
    "width" => ["22", "45"],
    "height" => ["22", "34"]
];

How can I transpose the columns of each key into rows of 3 columns as follows:

$rows = [
    ["length" => "12", "width" => "22", "height" => "22"],
    ["length" => "44", "width" => "45", "height" => "34"]
]; 
5
  • 1
    What are the columns of your table in the database ? Commented May 5, 2020 at 10:00
  • What have you already tried to get those two new arrays (we are still not talking about isnserting but only about formatting data)? Do you know how to check length of input arrays and compare/confirm if those have the same element count? Have you used for loop with that element count and try to make new arrays from existing ones? Commented May 5, 2020 at 10:02
  • @YezanRafed one of the column name is lenght and width and height. Commented May 5, 2020 at 10:05
  • Lenght of input arrays is not fixed maybe have change in some times.@YezanRafed Commented May 5, 2020 at 10:09
  • 1
    I have lenght and width and height column. and i want save this data in two row of my data base because these arrays have two value. first value of all arrays in one row and second value of all arrays in Another row. @YezanRafed . Commented May 5, 2020 at 10:24

3 Answers 3

1

The following function will do the job:

function transpose($data)
{
    $result = [];
    $keys = array_keys($data);
    for ($row = 0,  $rows = count(reset($data)); $row < $rows; $row++) {
        foreach ($keys as $key) {
            $result[$row][$key] = $data[$key][$row];
        }
    } 

    return $result;
}

Notice that the function is a general solution it doesn’t depend on the name of the keys nor on the number of entries of each key.

Sign up to request clarification or add additional context in comments.

Comments

0

Late to the party, but here's a one-liner that would work

$data = [
    "length" => ["12", "44"],
    "width" => ["22", "45"],
    "height" => ["22", "34"]
];

foreach ($data as $n => $v) foreach ($v as $vn => $vv) $new[$vn][$n]=$vv;
print_r($new);

https://www.tehplayground.com/OvSeQW8X6JBiDZ4i

Comments

-1

you can try this one to get the arrays like you said

   $height = array(12,44);
$width = array(20,50);
$length = array(30,50);
$new_array[0] = $height[0];
$new_array[1] = $width[0];
$new_array[2] = $length[0];
$new_array2 = $new_array[1];
print_r($new_array);
echo "<br>";
print_r($new_array2);

1 Comment

yes,you get first index in new arary but i want get second array too. I know should use loop but mabye i have 10 index in this array and i can get one element to store in loop

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.