php - Rearrange/sort data in form of associate array with Alphabet index -
php - Rearrange/sort data in form of associate array with Alphabet index -
i have array of records (some category titles fetched database table) want sort them in next format can accomplish this
$categories = array( 'a' => array( 'abreva', 'activia', 'advantage', 'advil', 'air wick', 'ajax', 'aleve'), 'b' => array(),
... , on upto z );
i don't know how perform best of knowledge should sort of sorting function performed after fetching info database. have tried on own unable accomplish desired output. help/hint sufficient. in advance
here go...
<?php $categories=array(); $i=0; $a=array('fas','abas','ajuma','bizzo','bike','chacha','cade'); //array, assume info db foreach($a $key=>$value){ // loop array $a result db if( substr(ucfirst($value),0,1) == 'a'){ //array_push($categories["a"],$value); // array_push seems not work $categories["a"][]=$value; } if( substr(ucfirst($value),0,1) == 'b'){ $categories['b'][]=$value; } if( substr(ucfirst($value),0,1) == 'c'){ $categories['c'][]=$value; } // .. c,d ....z } print_r($categories); //print result ?>
you can re-create , see results
php arrays sorting
Comments
Post a Comment