1

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.

9
  • 1
    you made a typo here foreach ($arrayas which should read as foreach ($array as. If that is your real code, error reporting would have thrown an undefined arrayas variable notice. If you are using foreach ($array as, please fix your question. Commented Nov 21, 2015 at 15:05
  • Since you don't want to override the value, try $array[] = [$difference => $row["sql_number"]];. This will prevent same difference from being overridden too, but note that in the next foreach the value will be an array. Can you post the desired structure of the final array? Commented Nov 21, 2015 at 15:11
  • @Fred-ii- fixed. Thanks! Commented Nov 21, 2015 at 15:12
  • you're welcome Lenny. Commented Nov 21, 2015 at 15:12
  • @FirstOne i have made changes according to your suggestion but now when I want to access the key value at my foreach loop it gives me an error: Array to string conversion. I gues that this technique builds array of arrays? Commented Nov 21, 2015 at 15:14

1 Answer 1

3

You were right about your assumptions. Since you have the print_r inside the loop, even though it's replacing the value each time, you are also printing it each time.

This line:

$array = [$difference => $row["sql_number"]];

Should be like this:

$array[$difference] => $row["sql_number"];

That way, you'll keep track of the previous values (keys) and add new ones.


This example shows the problem:

$a = array('ok' => 'fine');
$b['ok'] = 'fine';

print_r($a); // Array ( [ok] => fine ) 
echo '<br>';
print_r($b); // Array ( [ok] => fine )
echo '<br><br>';

$a = array('ok2' => 'fine2');
$b['ok2'] = 'fine2';

print_r($a); // Array ( [ok2] => fine2 ) 
echo '<br>';
print_r($b); // Array ( [ok] => fine [ok2] => fine2 ) 
echo '<br><br>';

I just feel like mentioning that you CAN'T FORGET that this will override previous differences. In this case, same $row["sql_number"]-s will produce same differences, so it might not be a problem. It's still worth mentioning. Example of this problem:

$b['ok'] = 'fine';
print_r($b); // Array ( [ok] => fine )     

echo '<br><br>';

$b['ok'] = 'fine2';
print_r($b); // Array ( [ok] => fine2 ) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.