1

I've a series of arrays with values that goes from 1 to 5. Almost every array has missing values, some even dont have any values. My objective is to fill the missing values with 0. All those arrays are stored into a multidimensional array.

My array looks like:

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

    [2] => Array
        (
            [0] => 1
            [1] => 5
        )

    [3] => Array
        (
            [0] =>  (this array has no values)
        )

    [4] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
        )

    etc...

)

How it should be:

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

    [2] => Array
        (
            [0] => 1
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 5
        )

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

    [4] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
        )

    etc...

)

Any help would be appriciated!

1
  • foreach() loop and array_pad() is all you need Commented Nov 22, 2012 at 22:00

4 Answers 4

2

For each of your subarrays loop through the numbers 1 to 5, and if that value exists set its key to be one less than its value:

$newarray = array();

foreach($arr as $key => $subarr) {
    for ($i = 1; $i <= 5; $i++) {
        if (in_array($i, $subarr)) $newarray[$key][$i - 1] = $i;
        else $newarray[$key][$i - 1] = 0;
    }
}

Where $newarray is your output and $arr is your input array.

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

Comments

1

You may want to note that PHP does not truly do multidimensional arrays. It only allows you to relate 2 flat arrays together which is not true multidimensionality.

This does not work and will produce results described above.

$menu[1] = "My Training"; //not $menu[1][0]

$menu[1][1] = "To Do List";
$menu[1][2] = "Catalog";
$menu[1][3] = "Self-Report";
$menu[1][4] = "Completions";

$menu[2] = "Manager";

$menu[2][1] = "Direct Reports";
$menu[2][2] = "Incompletes";
$menu[2][3] = "Completions";

$menu[3] = "Instructor";

$menu[3][1] = "My Classes";
$menu[3][2] = "Printables";
$menu[3][3] = "Qualifications";

This does work.

$menu[1] = "My Training"; //not $menu[1][0]

$submenu[1][1] = "To Do List";
$submenu[1][2] = "Catalog";
$submenu[1][3] = "Self-Report";
$submenu[1][4] = "Completions";

$menu[2] = "Manager";

$submenu[2][1] = "Direct Reports";
$submenu[2][2] = "Incompletes";
$submenu[2][3] = "Completions";

$menu[3] = "Instructor";

$submenu[3][1] = "My Classes";
$submenu[3][2] = "Printables";
$submenu[3][3] = "Qualifications";

$submenu is only related to $menu through the first key number as there are no first dimension values to $submenu.

Comments

1

Something like this (array_pad() won't do the trick). $myArray is your source array. Completed array is returned in $result:

$result = array();
foreach( $myArray as $subKey=>$subArray ) {
  for( $i=0; $i<5; $i++ ) {
    if( isset( $subArray[$i] )) {
       $result[$subKey][$i] = $subArray[$i];
    } else {
      $result[$subKey][$i] = 0;
    }
  }
}

Note, we do copy of the array. You cannot fill array in-place.

Comments

-1

It's been many years since I wrote any PHP but something like this might do the trick I guess?

for($i = 0; $i < 5; $i++)
{
  if(empty($myArray[$i])
  {
    $myArray[$i] = 0;
  }
}

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.