php - How can I sort the substrings of an array of strings -


i have array of strings this:

array     0 => string 'cheese=french'     1 => string 'pizza=large&cheese=french'     2 => string 'cheese=french&pizza=small'     3 => string 'cheese=italian' 

i need sort each substring (divided &) in string in array alphabettically. example: pizza=large&cheese=french should other way around: cheese=french&pizza=large, 'c' comes before 'p'.

i thought explode original array this:

foreach ($urls $url) {     $exploded_urls[] = explode('&',$url); }  array     0 => array         0 => string 'cheese=french'     1 => array         0 => string 'pizza=large'         1 => string 'cheese=french'     2 => array         0 => string 'cheese=french'         1 => string 'pizza=small'     3 => array         0 => string 'cheese=italian' 

and use sort in foreach loop, like:

foreach($exploded_urls $url_to_sort) {     $sorted_urls[] = sort($url_to_sort); } 

but when this, returns:

array     0 => boolean true     1 => boolean true     2 => boolean true     3 => boolean true     4 => boolean true 

up to:

    14 => boolean true 

when this:

foreach($exploded_urls $url_to_sort) {     sort($url_to_sort); } 

i 1 of arrays back, sorted:

array     0 => string 'cheese=dutch'     1 => string 'pizza=small' 

what's correct way go this?

the sort function returns boolean show if successful or not. in line:

$sorted_urls[] = sort($url_to_sort); 

you assigning return value of sort (true or false) $sorted_urls array. don't need - sort modify array call on, instead of trying assign result new array, call sort , @ $url_to_sort sorted array.

from documentation on sort:

$fruits = array("lemon", "orange", "banana", "apple");     sort($fruits);     foreach ($fruits $key => $val) {         echo "fruits[" . $key . "] = " . $val . "\n";     } 

the above example output:

fruits[0] = apple

fruits[1] = banana

fruits[2] = lemon

fruits[3] = orange


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -