Jump to content

Filling Undefined values in an array with a null value


Go to solution Solved by kicken,

Recommended Posts

Currently I have a table that presents a count of records next to the title of the particular product. To do this I'm using the following code:

View Class:

<?php
    $ls_arr = array(1=>'Open',8=>'Hot',2=>'Closed',3=>'Transacted',4=>'Dead',9=>'Follow Up',11=>'Working'); 
     foreach($groupedleads as $grplead){
        $statuses[] = $status = $ls_arr[$grplead["lead_status"]];
        if($grplead["title"] == NULL || $grplead["title"] == '')
            $grplead["title"] = "Unknown";
        if(isset($grplead["title"]))
            $titles[] = $title = $grplead["title"];
        $leaddata[$status][$title] = $grplead["leadnum"];
    }

foreach($groupedleads_status as $grplead){
  $statuses[] = $status = $ls_arr[$grplead["lead_status"]];
    if($grplead["title"] == NULL || $grplead["title"] == '')
        $grplead["title"] = "Unknown";
    if(isset($grplead["title"]))
        $titles[] = $title = $grplead["title"];
    $leaddatastatus[$status][$title][$sub_status] = $grplead["leadnum"];
}
?>
<?php
if(is_array($titles))
  foreach($titles as $title){ 
?>
    <tr>
        <?php 
          echo "<td>".$title."</td>";
              foreach ($statuses as $status) {
                    $num = $leaddata[$status][$title];
                    $contacts = $leaddatastatus[$status][$title][$sub_status];  

                  $status_num = array_flip($ls_arr)[$status];
                  if($contacts != ''){
                  echo "<td><a target='_blank' href='" . site_url('reports/lists?source=' . $title . '&status=' . $status_num . '&sub_status=') . "'>".$num."</a>
                  <font size= '1'><a target='_blank' href='".site_url('reports/lists?source=' . $title . '&status=' . $status_num . '&sub_status=6') . "'>". "Not Contacted - ".$contacts."</a></font></td>";
                  }else{
                  echo "<td><a target='_blank' href='" . site_url('reports/lists?source=' . $title . '&status=' . $status_num . '&sub_status=' . $sub_status) . "'>".$num."</a></td>";
                  }
          ?>
    </tr>
<?php } ?>

Now I did a var_dump on $leadstatus and it gave the following output:

iSKRe.png.456e860490c2b6f5473e5f2235f8d7e3.png

Now there are values that are not defined in the second array which are defined in the first. So at those places it gives me a warning message saying Undefined array key Sign Boards. So I want a way so that it displays all the titles regardless of the title being there or not and display the count for that as null.

Did you you mean something like this?

$data = [ 'Hot' => [ 'A' => 5,
                     'B' => 20,
                     'C' => 15,
                     'D' => 10,
                     'E' => 8
                   ],
          'Dead' => [ 'A' => 15,
                      'B' => 30,
                      'F' => 55,
                      'C' => 40,
                      'G' => 60,
                    ]
        ];
echo '<pre> BEFORE ' . print_r($data, 1) . '</pre>';          // original array
//
//  GET ALL KEYS
//
$keys = [];        
foreach ($data as $k1 => $v1) {
    $keys = array_merge($keys, array_keys($v1));
}
$keys = array_unique($keys);
$blank_values = array_fill_keys($keys, null);
//
//  INSERT MISSING KEYS INTO THE SUBARRAYS
//
foreach ($data as $k => &$subarray) {
    $subarray = array_merge($blank_values, $subarray);
}

echo '<pre> AFTER ' . print_r($data, 1) . '</pre>';            // array after processing

which gives...

BEFORE Array
(
    [Hot] => Array
        (
            [A] => 5
            [B] => 20
            [C] => 15
            [D] => 10
            [E] => 8
        )

    [Dead] => Array
        (
            [A] => 15
            [B] => 30
            [F] => 55
            [C] => 40
            [G] => 60
        )

)


 AFTER Array
(
    [Hot] => Array
        (
            [A] => 5
            [B] => 20
            [C] => 15
            [D] => 10
            [E] => 8
            [F] => 
            [G] => 
        )

    [Dead] => Array
        (
            [A] => 15
            [B] => 30
            [C] => 40
            [D] => 
            [E] => 
            [F] => 55
            [G] => 60
        )

)

 

  • Like 1
  • Solution

You can simply use the null coalescing operator when reading the values from your array to remove the warning.

$num = $leaddata[$status][$title] ?? null;
$contacts = $leaddatastatus[$status][$title][$sub_status] ?? null;

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.