如何对字符串数组的子字符串进行排序

I have an array of strings like this:

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

I need to sort each substring (divided by &) in the string in the array alphabettically. So for example: pizza=large&cheese=french should be the other way around: cheese=french&pizza=large, as 'c' comes before 'p'.

I thought i could explode the original array like this:

foreach ($urls as $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 then use sort in a foreach loop, like:

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

But when I do this, it just returns:

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

up to:

    14 => boolean true

When i do it like this:

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

I get one of the arrays back, sorted:

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

What's the correct way to go about this?

sort returns a boolean value (which is practically useless) and does the sort in-place. The code can be as simple as

// note iteration by reference
foreach($exploded_urls as &$url_to_sort){
    sort($url_to_sort);
}

After this loop each element of $exploded_urls will be sorted.

The sort function returns a boolean to show if it was successful or not. In your line:

$sorted_urls[] = sort($url_to_sort);

You are assigning the return value of sort (true or false) to the $sorted_urls array. You don't need to do this - sort will modify the array you call it on, so instead of trying to assign the result into a new array, just call sort and then look at the $url_to_sort for your sorted array.

From the documentation on sort:

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

The above example will output:

fruits[0] = apple

fruits[1] = banana

fruits[2] = lemon

fruits[3] = orange