2

i have this array

$array["4E-952778"][0]['fileName'] = "File 1";
$array["4E-952778"][0]['product'] = "Muse On Demand";
$array["4E-952778"][1]['fileName'] = "File 2";
$array["4E-952778"][1]['product'] = "Muse On Demand";   

$array["15210"][0]['fileName'] = "File 3";
$array["15210"][0]['product'] = "4Manager"; 
$array["15210"][1]['fileName'] = "File 4";
$array["15210"][1]['product'] = "4Manager";

and im trying to sort it using uasort() this way:

uasort($array, function ($a, $b) { return strcmp($a['product'], $b['product']); });

but im getting an error: undefined index product

1
  • When you sort the array, you are only sorting based on the outermost dimension. Therefore the $a and $b don't have index product, they have indexes 0 and 1. And inside those would be product Commented Apr 7, 2015 at 9:40

1 Answer 1

1

Try this:-

uasort($array, function ($a, $b) {
     $i=0;
     return strcmp($a[$i]['product'], $b[$i]['product']);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, rather than edit, delete is happen. but i reposted it. Please have a look.

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.