PHP Foreach - Select Specific Items instead of Keys of an Array

John :

I've been working on this MySql and PHP code:

//SQL para cálculo de trabajos acumulados
    $torsAC_sql = mysqli_query($db,"select * from reports WHERE TORS_PROYECTO = '$IDTORS' AND IDUSER = '$user_check' AND MONTH(DATEREG) = MONTH(CURDATE()) AND YEAR(DATEREG) = YEAR(CURDATE()) ORDER BY DATEREG DESC LIMIT 1");
    $row9 = mysqli_num_rows($torsAC_sql);
    $rowC = mysqli_fetch_array($torsAC_sql,MYSQLI_ASSOC);
    if($row9 > 0){
       $arrayAcumulados[] = array($rowC["TORS_PROYECTO"] => $rowC["DATEREG"]); 
    }else{
       echo "Error";
    }
    echo print_r($arrayAcumulados);

As a result, I get the following array of arrays:

`Array ( 
[0] => Array ( [576] => 2020-02-25 ) 
[1] => Array ( [417] => 2020-02-19 ) 
[2] => Array ( [417] => 2020-02-19 ) 
[3] => Array ( [417] => 2020-02-19 ) 
[4] => Array ( [549] => 2020-02-07 ) 
[5] => Array ( [31] => 2020-02-07 )
)`

I removed the duplicates by using the array_unique php function, getting the following result:

`$a = array_unique($arrayAcumulados, SORT_REGULAR);
Array ( 
[0] => Array ( [576] => 2020-02-25 ) 
[1] => Array ( [417] => 2020-02-19 ) 
[4] => Array ( [549] => 2020-02-07 ) 
[5] => Array ( [31] => 2020-02-07 )
)`

I would like to get the date values instead of the keys on the array without duplicates, using foreach.

foreach($a as $i => $item) {
     //Insert code to get Date Value                               
}

If I try to echo $a[$item], the result is an empty array.

Virender Kumar :

If you don't need keys anymore then change this line

$arrayAcumulados[] = array($rowC["TORS_PROYECTO"] => $rowC["DATEREG"]); 

to

$arrayAcumulados[] = $rowC["DATEREG"];

otherwise, you need to use a nested for each loop.

foreach($a as $i => $item) {
    foreach($item as $key=> $date){
     echo  $date;
   }                    
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=13941&siteId=1