我遇到了问题。我有一个包含类别的json数组,我需要根据类别名称按字母顺序排序数组。 这是我的代码。
控制器
$post = file_get_contents("............");
$model = CJSON::decode($post, true);
function cmp($a, $b)
{
//echo "<pre> a: ";print_r($a);die;
return strcmp($a["name"], $b["name"]);
}
usort($model, "cmp");
$this->render('index',array('model'=>$model));
查看页面
$options = array();
foreach ($model as $user) :
foreach($user as $use):
$options[$use['id']] = $use['name'];
endforeach;
endforeach;
echo CHtml::dropDownList('mySelect', 'name', $options,array('prompt'=>'------Select--------'));
?>
</div>
</div>
$ model
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => SMALL PATCHES
)
[1] => Array
(
[id] => 2
[name] => BIG PATCHES
)
[2] => Array
(
[id] => 3
[name] => CUSTOM PATCHES
)
[3] => Array
(
[id] => 4
[name] => EVENT PATCHES
)
[4] => Array
(
[id] => 5
[name] => BLANK PATCHES
)
[5] => Array
(
[id] => 6
[name] => USS PATCHES
)
[6] => Array
(
[id] => 7
[name] => FLAGS
)
)
)
请帮助我......等待回复......
答案 0 :(得分:0)
我得到了答案,我做了一个小改动
控制器
$post = file_get_contents("...........");
$model = CJSON::decode($post, true);
function cmp($a, $b)
{
return strcmp($a["name"], $b["name"]);
}
uasort($model['categories'], "cmp");
$this->render('index',array('model'=>$model));