KSI Posted November 30, 2021 Share Posted November 30, 2021 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: 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. Quote Link to comment https://forums.phpfreaks.com/topic/314257-filling-undefined-values-in-an-array-with-a-null-value/ Share on other sites More sharing options...
Barand Posted November 30, 2021 Share Posted November 30, 2021 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 ) ) 1 Quote Link to comment https://forums.phpfreaks.com/topic/314257-filling-undefined-values-in-an-array-with-a-null-value/#findComment-1592320 Share on other sites More sharing options...
Solution kicken Posted November 30, 2021 Solution Share Posted November 30, 2021 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; Quote Link to comment https://forums.phpfreaks.com/topic/314257-filling-undefined-values-in-an-array-with-a-null-value/#findComment-1592327 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.