mat
mat

Reputation: 2617

Filling php array that has missing values

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!

Upvotes: 1

Views: 1864

Answers (4)

Marcin Orlowski
Marcin Orlowski

Reputation: 75645

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.

Upvotes: 1

John126
John126

Reputation: 11

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.

Upvotes: 1

freejosh
freejosh

Reputation: 11383

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.

Upvotes: 2

span
span

Reputation: 5624

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;
  }
}

Upvotes: -1

Related Questions