简体   繁体   English

PHP 数组在选项下拉列表中按字母顺序排列

[英]PHP array in alphabetic order in option drop down

the text below takes google sheet json output, turns it into php array and then filters just by football club.下面的文本采用谷歌表 json output,将其转换为 php 数组,然后仅按足球俱乐部过滤。 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);当我打印出 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到目前为止,我已经尝试过 sort($clubs) 但它似乎不起作用

php code: php 代码:

$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: 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?写你想在数组中接收什么?

There are some sorting functions for PHP arrays already. PHP arrays 已经有一些排序功能。 I think sort() is what you want.我认为 sort() 是你想要的。 Have to tried it?一定要试试吗? https://www.w3schools.com/php/php_arrays_sort.asp Usage: https://www.w3schools.com/php/php_arrays_sort.asp用法:

<?php
$array = ["Newcastle United", "AFC Bournemouth", "Wolverhampton Wanderers", "Chelsea"];
sort($array);

print_r($array);
?>

Output Output

Array ( 
        [0] => AFC Bournemouth 
        [1] => Chelsea 
        [2] => Newcastle United 
        [3] => Wolverhampton Wanderers 
      )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM