Check for two key values from serialized array

We have been using the below code to create a progress report for users. It checks an array for each user under _usermeta. It worked up until now because the plugin that creates the data would only add the value for the lesson if it was completed. Now it adds the value regardless and has a second value for whether it has been completed or not. Any ideas on how to make this work?

/* php for the page itself */

 foreach($usersemail['users'] as $userdata){
         
                   $dtd=  get_user_meta($userdata['user_id'] ,'_sfwd-course_progress',true);
                   
                ?>
                <tr><td><?php echo get_user_meta($userdata['user_id'],'first_name',true).' '.get_user_meta($userdata['user_id'],'last_name',true);  ?></td> 
            <?php   
                  foreach($dtd as $dt){
                       if(array_key_exists('21414',$dt['lessons'])){
                       $lession_complete= false; 
                       echo "<td>completed </td>"; 
                       }else{
                        echo "<td> </td>"; 
                       } 
                      
                        if(array_key_exists('21416',$dt['lessons'])){
                        $lession_complete= false; 
                       echo "<td>completed </td>"; 
                       }else{
                        echo "<td> </td>"; 
                       } 
                        if(array_key_exists('21418',$dt['lessons'])){
                        $lession_complete= true; 
                       echo "<td>completed </td>"; 
                       }else{
                        echo "<td> </td>"; 
                       } 
/*example of table from _usermeta */

a:1:{i:21126;a:6:{s:7:"lessons";a:5:{i:21412;i:1;i:21414;i:0;i:21416;i:1;i:21418;i:1;i:21420;i:1;}s:6:"topics";a:5:{i:21412;a:0:{}i:21414;a:0:{}i:21416;a:0:{}i:21418;a:0:{}i:21420;a:0:{}}s:9:"completed";i:5;s:5:"total";i:5;s:7:"last_id";i:21420;s:6:"status";s:11:"in_progress";}}

Using lesson 21416 as an example: I need it to echo "complete" if both the lesson is found (21416) and if the value following it is "i:1". With the default being the empty td if the value is i:0.

Thanks in advance!

1 answer

  • answered 2021-11-29 05:28 Ivijan Stefan Stipić

    If you already have in lessons value 1 that you know is completed, you just need to change your loop as:

    foreach($dtd as $dt){
        foreach($dt['lessons'] as $lesson_id => $value) {
            if($value == 1){
                $lession_complete= false; 
                echo "<td>completed </td>"; 
            } else {
                echo "<td> </td>"; 
            }
        }
    }
    

    But if you need to search by key not ID, think about this:

    $needle_keys=['21414', '21416','21418'];
    
    foreach($dtd as $dt){
    
        $lessons_keys = array_keys($dt['lessons']);
        
        foreach($lessons_keys as $key){
            if(in_array($key, $needle_keys)){
                if($dt['lessons'][$key]){
                    $lession_complete= false; 
                    echo "<td>completed </td>"; 
                } else {
                    echo "<td> </td>"; 
                }
            }
        }
    }
    

    But in the general here you can do a both. Search by key and value also, just expand last if

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum