错误使用uasort与多维数组:未定义指数
问题描述:
我有这个阵列错误使用uasort与多维数组:未定义指数
$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";
和我尝试它使用uasort()
这样排序:
uasort($array, function ($a, $b) { return strcmp($a['product'], $b['product']); });
但即时得到一个错误:未定义索引产品
答
试试这个: -
uasort($array, function ($a, $b) {
$i=0;
return strcmp($a[$i]['product'], $b[$i]['product']);
});
对数组排序时,只能根据最外层维排序。因此,$ a和$ b没有索引产品,它们具有索引0和1.并且在那里将产品 –