0

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.

1
  • You could also do the sorting and the deduplication in the database. If you want to get the first item, use echo reset($item); Commented Feb 26, 2020 at 18:30

1 Answer 1

2

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;
   }                    
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.