•  0
    PHP

    Sorting multidimensional array in PHP

      Asmin Jismun    133        0        Report content

    $data = array(
    		'Category' => array(
    	        0 => array(
    	            'Name' => 'Bcategory',
    	            'View' => array(
                            0 => array('Name' => 'Cview'),
                            1 => array('Name' => 'Bview'),
                            2 => array('Name' => 'Aview'),
                        )
    	        	),
    	        1 => array(
    	            'Name' => 'Ccategory',
    	            'View' => array(
                            0 => array('Name' => 'Cview'),
                            1 => array('Name' => 'Aview'),
                            2 => array('Name' => 'Bview'),
                        )
    	        	),
    	        2 => array(
    	            'Name' => 'Acategory',
    	            'View' => array(
                            0 => array('Name' => 'Aview'),
                            1 => array('Name' => 'Cview'),
                            2 => array('Name' => 'Bview'),
                        )
    	        	)
    	        )
    	);
            //Sorting array
            $tmp = Array();
    		foreach($data['Category'] as $key => $val){
    			$tmp[] = $val["Name"];
    			$tmp2 = Array();
    			foreach($val['View'] as $v){
    				$tmp2[] = $v['Name'];
    			}
    			array_multisort($tmp2, $data['Category'][$key]['View']); 
    		}   
    		array_multisort($tmp, $data['Category']);
            echo '
    '; print_r($data);
    Output:
    Array
    (
        [Category] => Array
            (
                [0] => Array
                    (
                        [Name] => Acategory
                        [View] => Array
                            (
                                [0] => Array
                                    (
                                        [Name] => Aview
                                    )
                                [1] => Array
                                    (
                                        [Name] => Bview
                                    )
                                [2] => Array
                                    (
                                        [Name] => Cview
                                    )
                            )
                    )
                [1] => Array
                    (
                        [Name] => Bcategory
                        [View] => Array
                            (
                                [0] => Array
                                    (
                                        [Name] => Aview
                                    )
    
                                [1] => Array
                                    (
                                        [Name] => Bview
                                    )
                                [2] => Array
                                    (
                                        [Name] => Cview
                                    )
                            )
                    )
                [2] => Array
                    (
                        [Name] => Ccategory
                        [View] => Array
                            (
                                [0] => Array
                                    (
                                        [Name] => Aview
                                    )
                                [1] => Array
                                    (
                                        [Name] => Bview
                                    )
                                [2] => Array
                                    (
                                        [Name] => Cview
                                    )
                            )
                    )
            )
    )

     


  •  


Leave a Comment

Please Login to insert comment.

 

Facebook Conversations