Viewed   63 times

So I was reading up on sorting arrays in PHP and it makes sense, but they only gave easy examples, so I'm not sure how I would sort something the code below. I want to sort the arrays inside the "School Supplies" and "Kitchen Needs" by the title of the item. How would I do such a thing?

Thanks a lot! Dan

Array
(
    [School Supplies] => Array
        (
            [0] => Array
                (
                    [item_id] => 1
                    [title] => Backpack
                    [parent_id] => 0
                    [amazon_url] => 
                )

            [1] => Array
                (
                    [item_id] => 2
                    [title] => Calculator
                    [parent_id] => 0
                    [amazon_url] => 
                )

            [2] => Array
                (
                    [item_id] => 3
                    [title] => Erasers
                    [parent_id] => 0
                    [amazon_url] => 
                )
        )

    [Kitchen Needs] => Array
        (
            [0] => Array
                (
                    [item_id] => 13
                    [title] => Bottle Opener
                    [parent_id] => 0
                    [amazon_url] => 
                )

             [1] => Array
                (
                    [item_id] => 14
                    [title] => Disposable Plates
                    [parent_id] => 0
                    [amazon_url] => 
                )

            [2] => Array
                (
                    [item_id] => 15
                    [title] => Disposable Silverware
                    [parent_id] => 0
                    [amazon_url] => 
                )
        )
)

 Answers

1
function cmp($x, $y) {
    return strcmp($x['title'], $y['title']);
}

usort($array['School Supplies'], 'cmp');
usort($array['Kitchen Needs'], 'cmp');
Wednesday, August 24, 2022
1
function cmp($a, $b)
{

$sizes = array(
"XXS" => 0,
"XS" => 1,
"S" => 2,
"M" => 3,
"L" => 4,
"XL" => 5,
"XXL" => 6
);

$asize = $sizes[$a];
$bsize = $sizes[$b];

if ($asize == $bsize) {
    return 0;
}

return ($asize > $bsize) ? 1 : -1;
}

usort($your_array, "cmp");
Wednesday, August 24, 2022
 
enrique
 
3

Use multi-byte string functions. There is a function called strcoll which seems to suit your needs.

More info:

  • On how to sort an array of UTF-8 strings
  • How to sort an array of UTF-8 strings?

EDIT: added Peter's working code, below

setlocale(LC_COLLATE, 'sk_SK.utf8');

usort($fb_friends['data'], 'custom_sort');

function custom_sort($a, $b) {
    return strcoll ($a['last_name'], $b['last_name']);
}

foreach ($fb_friends['data'] as $friend) {
    echo '<br>';
    echo $friend['name'];
}
Wednesday, August 3, 2022
 
3

I found a good explanation at Pass-By-Name Parameter Passing. Essentially, the body of a function is interpreted at call time after textually substituting the actual parameters into the function body. In this sense the evaluation method is similar to that of C preprocessor macros.

By substituting the actual parameters into the function body, the function body can both read and write the given parameters. In this sense the evaluation method is similar to pass-by-reference. The difference is that since with pass-by-name the parameter is evaluated inside the function, a parameter such as a[i] depends on the current value of i inside the function, rather than referring to the value at a[i] before the function was called.

The page I linked above has some more examples of where pass-by-name is both useful, and dangerous. The techniques made possible by the pass-by-name are largely superseded today by other, safer techniques such as pass-by-reference and lambda functions.

Tuesday, December 27, 2022
 
dafi4
 
4

That's because JVM does not support a "by-name" parameter, so Scala has to implement it in another way. => X actually compiles to a Function0[X], which erases to Function0[Object], which makes it impossible for Scala to distinguish two methods that differ only by the expected type of a by-name parameter.

Thursday, November 3, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :