0

I have a multidimensional array. The arrays will have different lengths and seldom will they have the same length. My problem here is how can I make it so that the arrays will all share the length of the array with the biggest size?

My Array:

Array
(
    [1] => Array
        (
            [Session 2] => Beer
            [Food] => Chicken
            [Drink] => Beer
        )

    [2] => Array
        (
            [Session 2] => Tea
            [Food] => Aaaa
            [Drink] => Ddd
            [Cake] => Weee
            [Brownies] => Rrrr
        )

)

Expected output:

Array
(
    [1] => Array
        (
            [Session 2] => Beer
            [Food] => Chicken
            [Drink] => Beer
            [Cake] => ''
            [Brownies] => ''
        )

    [2] => Array
        (
            [Session 2] => Tea
            [Food] => Aaaa
            [Drink] => Ddd
            [Cake] => Weee
            [Brownies] => Rrrr
        )

)

The array size is not limited to only two arrays. Is this even possible and if so how?

I only want to copy the array keys and not the values its main purpose here is for presenting the content of the array in a table.

1
  • I suspect an object would be a better fit - you can define the properties then. Commented Nov 13, 2018 at 9:29

3 Answers 3

6

Here's one option, where you build an array of all possible array keys, then loop over your original array and set empty strings to the keys that don't exist yet:

// find all possible keys
$keys = [];
foreach ($array as $entry) {
    $keys = array_merge($keys, array_keys($entry));
}

// pad missing keys with an empty string
foreach ($array as &$entry) {
    foreach ($keys as $key) {
        if (!isset($entry[$key])) {
            $entry[$key] = '';
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

You don't need the inner loop + the if(). Use array_replace(keys, entry)
@Andreas good point - $keys = ['Foo' => '']; then $entry = array_replace($entry, $keys)
thank you. will give this a try but what does this line mean? foreach ($array as &$entry) { the &$entry
I generally try to avoid modify by reference in loops because of the weird way PHP handles references. Even though it's a bit slower, array_walk() accomplishes the same thing without the possibility of weird side effects.
Is it not preferable to also use array_fill_keys to just set all the values to the desired ones? So if he wishes to have all the keys and empty values to just do it as $emptyValuesArray = array_fill_keys(array_keys($targetArray), ''); And avoid the for loops
|
1

If the main purpose is to show the data in a table, then you do not need to fill in the missing keys. You can use the isset() or empty() functions to determine whether an array has a given key. So, your table code could look like the following:

<?php
foreach ($rows as $row) {
    echo "<tr>";
    echo "<td>" . (isset($row["Session 2"]) ? $row["Session 2"] : "") . "</td>"; //Old school
    echo "<td>" . ($row["Food"] ?? "") . "</td>"; //PHP 7+
    //remaining rows
    echo "</tr>";
}

2 Comments

The null coalescing operator could also be used here
@KarstenKoop Thanks, I've updated my answer with some better examples
0

Let's say the array you're talking about is inside a variable $array,

Do this to find the maximum length;

$max = 0;

foreach($array as $index => $value){

    if($index == sizeof($array) - 1){
        break;
    }

    if($index && $max > sizeof($array[$index+1])){
        $max = $max;
    }
    if(!$index && sizeof($value) > sizeof($array[$index+1])){
        $max = sizeof($value);
    }else {
        $max = sizeof($array[$index+1]);
    }
}

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.