PHP array in alphabetic order in option drop down
Get the solution ↓↓↓the text below takes google sheet json output, turns it into php array and then filters just by football club. the outcome is a drop down list of football clubs, sorted in random order. i am having trouble sorting them by alphabetical order.
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
//create array to store
$clubs=array();
//loop for each into array
foreach ($data['feed']['entry'] as $item)
{
$clubs[]= $item['gsx$clubs']['$t'];
}
//take array and get unique clubs
$clubs =array_unique($clubs);
//start select html
echo '<select>';
//print out unique clubs in option dropdown
foreach ($clubs as $key => $club) {
echo '<option>' . $club . '</option>' ;
}
//finish select html
echo '</select>';
when i print out print_r($clubs); it comes out like this
Array ( [0] => AFC Bournemouth [1] => Chelsea [2] => Wolverhampton Wanderers [3] => Crystal Palace [4] => Burnley [5] => Brighton & Hove Albion [6] => Tottenham Hotspur [8] => West Ham United [11] => Everton [12] => Manchester City [14] => Aston Villa [16] => Arsenal [20] => Manchester United [21] => Watford [23] => Sheffield United [28] => Southampton [30] => Newcastle United [39] => Norwich City [51] => Liverpool [71] => Leicester City )
so far i have already tried sort($clubs) and it doesnt seem to work
Answer
Solution:
php code:
$asd = array(
"AFC Bournemouth",
"Chelsea",
"Wolverhampton Wanderers",
"Crystal Palace",
"Burnley",
"Brighton & Hove Albion",
"Tottenham Hotspur"
// ...
);
echo "<pre>";
print_r($asd);
echo "</pre>";
sort($asd);
echo "<pre>";
print_r($asd);
echo "</pre>";
output:
Array
(
[0] => AFC Bournemouth
[1] => Chelsea
[2] => Wolverhampton Wanderers
[3] => Crystal Palace
[4] => Burnley
[5] => Brighton & Hove Albion
[6] => Tottenham Hotspur
)
Array
(
[0] => AFC Bournemouth
[1] => Brighton & Hove Albion
[2] => Burnley
[3] => Chelsea
[4] => Crystal Palace
[5] => Tottenham Hotspur
[6] => Wolverhampton Wanderers
)
no problem, sorted alphabetically.
write what you want to receive in array?
Answer
Solution:
There are some sorting functions for PHP arrays already. I think sort() is what you want. Have to tried it? https://www.w3schools.com/php/php_arrays_sort.asp Usage:
<?php
$array = ["Newcastle United", "AFC Bournemouth", "Wolverhampton Wanderers", "Chelsea"];
sort($array);
print_r($array);
?>
Output
Array (
[0] => AFC Bournemouth
[1] => Chelsea
[2] => Newcastle United
[3] => Wolverhampton Wanderers
)
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function getclientoriginalname() on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.