I want to build an array with key-value pairs of MySqli query. What I have so far overwrites array's entries:
$array = array();
while($row = mysqli_fetch_array($result))
{
$difference = abs($base - $row["sql_number"]);
$array = [$difference => $row["sql_number"]];
$bar = each($array);
print_r($bar);
}
echo count($array);
ksort($array);
echo count($array);
foreach ($array as $key => $value) {
echo "Key: ".$key;
echo "Value: ". $value;
}
print_r($bar); prints the key-value pairs correct but I am convinced it is because it just prints the latest entry from the mysqli_fetch_array($result) and the previous entries are overwriten.
I want to create such an array then sort it with ksort by the key value.
foreach ($arrayaswhich should read asforeach ($array as. If that is your real code, error reporting would have thrown an undefined arrayas variable notice. If you are usingforeach ($array as, please fix your question.$array[] = [$difference => $row["sql_number"]];. This will prevent samedifferencefrom being overridden too, but note that in the nextforeachthevaluewill be anarray. Can you post the desired structure of the final array?Array to string conversion. I gues that this technique builds array of arrays?