Foreach echo数组为1返回3个值[关闭]

I have the following strange problem, I cant solve it, my loop returns unique parts once, but the two parts I fill the array with before both loops, get used for every one of them. Hope you can help me.

$aBuild = array();
$part1 = "Test 1";
$part2 = "Test 2";  
$aBuild = array(1=> $part1, 2 => $part2);

Then I have a foreach loop getting data from database, in that same loop I do this:

$aBuild[$iNumber] = $sOtherParts;

Adding things to the array. At the end of this query foreach loop, i do this:

ksort($aBuild);

foreach($aBuild as $values)
{
    echo  $values;  
}  

This echo's each $sOtherParts once, but for each of those, it adds the $part1 and $part2, to it:

Like this:

Unique part Test 1 Test 2 Another Unique part Test 1 Test 2 Really Unique part Test 1 Test 2 Ect.. Unique part Test 1 Test 2

You should echo out your key as well. It's likely the following line may not be replacing data from your original array:

$aBuild[$iNumber] = $sOtherParts;

Try changing the foreach loop to print out your key:

ksort($aBuild);

foreach($aBuild as $key => $value)
{
    echo $key." => ".$value."<br />
";
}

Without seeing the actual code, it's hard to tell.

You need a newline at the end of the echo statement, as echo doesn't automatically provide one like echo in DOS or Bash etc.

Also, if your $iNumber var is just an incrementing variable, why not just use:

$aBuild[] = $sOtherParts;

Then you shouldn't need ksort.