0

I know this question was asked before, and I checked most of them. I couldn't get rid of this error. I keep getting Notice: Array to string conversion in C:\wamp64\www\test.php on line 23. Line 23 is this line: $aRanges[] = $aGroup[0] . '-' . $aGroup[count($aGroup)-1];

I'd appreciate your help. thank you.

$sql = "SELECT Rab, Rbc, Rcd, Rde FROM datatb";
$result = $conn->query($sql);

//Fetching all the results to array
while(($mData[] = mysqli_fetch_assoc($result)) || array_pop($mData)); 


function GetRanges( $mData ) {
  $mData = array_unique( $mData );
  sort( $mData );
  $aGroups = array();
  for( $i = 0; $i < count( $mData ); $i++ ) {
    if( $i > 0 && ( $mData[$i-1] == $mData[$i] - 1 ))
      array_push( $aGroups[count($aGroups)-1], $mData[$i] );
    else
      array_push( $aGroups, array( $mData[$i] )); 
  }
  $aRanges = array();
  foreach( $aGroups as $aGroup ) {
    if( count( $aGroup ) == 1 )
      $aRanges[] = $aGroup[0];
    else
      $aRanges[] = $aGroup[0] . '-' . $aGroup[count($aGroup)-1];
  }
  return $aRanges;
}

print_r( GetRanges( $mData ));     
user223549
  • 117
  • 8
  • You assing a string `$aGroup[0] . '-' . $aGroup[count($aGroup)-1]` to an array `$aRanges[]` that does not work. What are you trying to achive? – Jens Feb 03 '19 at 10:29
  • `while(($mData[] = mysqli_fetch_assoc($result)) || array_pop($mData)); ` <--- why not just use `mysqli_fetch_all`? That is not a good line for anyone. – Jonnix Feb 03 '19 at 10:30

0 Answers0