I'm using sort to sort an array alphabetically that's done like this:
$Consumer[] = "Norman";
$Consumer[] = "Food";
$Consumer[] = "Clothes";
$Consumer[] = "Chips";
But when I use this code to output the array, it won't work.
$cat = sort($Consumer);
foreach ($cat as $value)
{
echo '<option value="'.$value.'">'.$value.'</option>';
}
It works if I remove the sort. What am I doing wrong here and how do I set this right?
As indicated in the docs sort acts by reference and returns a boolean
bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
so $cat
is a boolean (true or false).
The following is a working example of your code:
$Consumer[] = "Norman";
$Consumer[] = "Food";
$Consumer[] = "Clothes";
$Consumer[] = "Chips";
sort($Consumer);
foreach ($Consumer as $value)
{
echo '<option value="'.$value.'">'.$value.'</option>';
}
sort
function returns boolean value so you are overwriting your data. It modifies your $Consumer
variable by reference.
Try with:
sort($Consumer);
foreach ($Consumer as $value)
{
echo '<option value="'.$value.'">'.$value.'</option>';
}