0

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'

My desired result:

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

3 Answers 3

1

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 . "\n";
    }

The above example will output:

fruits[0] = apple

fruits[1] = banana

fruits[2] = lemon

fruits[3] = orange

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

If you know you are dealing with query strings, don't merely use explode(); use the parsing tool designed for the task -- parse_str().

After parsing a string, sort the associative array by key, then return the data to its original form using http_build_query().

Code: (Demo)

$queryStrings = [
    'cheese=french',
    'pizza=large&cheese=french',
    'cheese=french&pizza=small',
    'cheese=italian',
];

var_export(
    array_map(
        function($qs) {
            parse_str($qs, $result);
            ksort($result);
            return http_build_query($result);
        },
        $queryStrings
    )
);

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.