0

I am stuck with a problem . I have an json array which contains categories,I need the array should be sorted alphabetically based on the category name. Here is my code.

  • Controller

    $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));
    
  • View page

                                $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
                )

        )

)

Please help me with this... Waiting for a response....

3
  • can you show your array $model ? Commented Jul 21, 2016 at 7:13
  • once go through usort Example 2 Commented Jul 21, 2016 at 7:23
  • i referred stackoverflow.com/questions/10484607/… Commented Jul 21, 2016 at 7:29

1 Answer 1

0

I got the answer i just made a small change

Controller

$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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.