Kaustav Chakravorty
Kaustav Chakravorty

Reputation: 260

Check in an array where it doesn't contain the value in a foreach loop

I am viewing a list of tasks for different projects and the its status.

I have an array which lists out the the different projects i have in the database project_list

 Array
    (
        [0] => Array
            (
                [project_name] => test
            )

        [1] => Array
            (
                [project_name] => Demo
            )

        [2] => Array
            (
                [project_name] => Demo Test
            )

    )

I have the task_list array

Array
(
    [0] => Array
        (
            [task_id] => 1
            [task_name] => File Upload
            [assigned_to] => John DOe
            [project_name] => [{"project_name":"test","status":"Inprogress"},{"project_name":"Demo","status":"Completed"}]
        )

    [1] => Array
        (
            [task_id] => 2
            [task_name] => Image Upload
            [assigned_to] => Jax
            [project_name] => [{"project_name":"Demo","status":"Completed"}]
        )

By looking at the task list you can see a person is assigned to different projects and it shows each project status you can see in project_name field now i want to view the data in a html table list all the projects and then from the task list data from each data it should check the project_name from the project list and put the respective status in its column i have done in the following way but i am not getting the desired result

<thead>
<tr>
<th><i class="fa fa-tasks"></i> Task Name </th>
<th></i> Assigned to </th>
<?php 
foreach ($project_list as $keys => $val) {
echo '<th>'.$val['project_name'].'</th>';
}
?>
</tr>
</thead>
</tbody>

<?php 
foreach ($task_list as $key => $value) {?>
<tr>
<td> <?php echo $value['task_name']; ?> </td>
<td> <?php echo $value['assigned_to']; ?> </td>
<?php 
$obj = json_decode($value['project_name'],true);
foreach ($project_list as $keys => $val) {
    foreach ($obj as $row) {
        if($val['project_name']==$row['project_name']){
        echo '<td>'.$val['project_name'].'--'.$row['status'].'</td>';

       // In this phase in facing the problem
        }elseif (!in_array($val['project_name'],$row)) {
        echo "<td></td>";
        }
  }
}
?>
</tr>
<?php }?>
</tbody>

I want to check if from the task list if a project is not list in it, then it should show blank in the table this is the point where i am struggling Thanks in advance

Upvotes: 1

Views: 66

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

You need to change your foreach() like below:-

foreach ($project_list as $keys => $val) {

    if (!in_array($val['project_name'],array_column($obj,'project_name'))) {
        echo "<td></td>";
    }else{
         echo '<td>'.$val['project_name'].'--'.$obj[array_search($val['project_name'],array_column($obj,'project_name'))]['status'].'</td>';

    }
  }

Upvotes: 1

Related Questions