带有键值的数组的PHP排序数组
我有一个数组,如下所示。我想用srp值按升序对其进行排序。如何在PHP中实现这一点。我是一名初学者,我认为可以使用usort()来完成,但不确定如何完成带有键值的数组的PHP排序数组,php,arrays,sorting,Php,Arrays,Sorting,我有一个数组,如下所示。我想用srp值按升序对其进行排序。如何在PHP中实现这一点。我是一名初学者,我认为可以使用usort()来完成,但不确定如何完成 Array ( [0] => stdClass Object ( [_id] => 5911af8209ed4456d069b1d3 [title] => Zen M4S(Silver) [mrp] => 0
Array
(
[0] => stdClass Object
(
[_id] => 5911af8209ed4456d069b1d3
[title] => Zen M4S(Silver)
[mrp] => 0
[srp] => 1900
)
[1] => stdClass Object
(
[_id] => 5911af8209ed4456d069b1d2
[title] => Zen M4S(Silver)
[mrp] => 0
[srp] => 1000
)
[2] => stdClass Object
(
[_id] => 5911af8209ed4456d069b1d4
[title] => JIVI X525(Red)
[mrp] => 0
[srp] => 1250
)
)
所需输出:
Array
(
[0] => stdClass Object
(
[_id] => 5911af8209ed4456d069b1d2
[title] => Zen M4S(Silver)
[mrp] => 0
[srp] => 1000
)
[1] => stdClass Object
(
[_id] => 5911af8209ed4456d069b1d4
[title] => JIVI X525(Red)
[mrp] => 0
[srp] => 1250
)
[2] => stdClass Object
(
[_id] => 5911af8209ed4456d069b1d3
[title] => Zen M4S(Silver)
[mrp] => 0
[srp] => 1900
)
)
class TopDealsDAOMongoImpl implements TopDealsDAO{
//put your code here
public function getTopDeals() {
$topDeals = json_decode(json_encode(MasterAffiliateProductMappingMongo::select("product_id","affiliate_id","title","our_product_id","mrp","srp","product_url","category_name","ID")->where('top_deal','true')->get()));
uasort($topDeals, array($this, "sort_arr"));
}
public function sort_arr($a,$b)
{
if ($a->srp == $b->srp) return 0;
return ($a->srp < $b->srp)?-1:1;
}
}
我的班级是这样的:
类topdalsdaomongoimpl实现topdalsdao{
//把你的代码放在这里
公共函数getTopDeals(){
$topDeals=json解码(json编码(MasterAffiliateProductMappingMongo::选择(“产品id”、“附属公司id”、“标题”、“我们的产品id”、“mrp”、“srp”、“产品url”、“类别名称”、“id”)->其中('top_deal'、'true')->获取());
函数my_sort($a,$b)
{
如果($a->srp==$b->srp)返回0;
返回($a->srp<$b->srp)?-1:1;
}
uasort($topDeals,'my_sort');
}
}
尝试一下:
// Sort array
uasort($array, 'cmp');
function cmp($a, $b) {
if ($a['srp'] == $b['srp']) {
return 0;
}
// Return in acsending order
return ($a['srp'] < $b['srp']) ? -1 : 1;
}
//排序数组
uasort($array,'cmp');
功能cmp($a$b){
如果($a['srp']=$b['srp']){
返回0;
}
//按发送顺序返回
回报($a['srp']<$b['srp'])?-1:1;
}
阅读更多信息:有关简单数组
function my_sort($a,$b)
{
if ($a['srp'] == $b['srp']) return 0;
return ($a['srp'] < $b['srp'])?-1:1;
}
uasort($array, 'my_sort'); // replace $array with your variable
函数我的排序($a,$b)
{
如果($a['srp']=$b['srp'])返回0;
回报($a['srp']<$b['srp'])?-1:1;
}
uasort($array,'my_sort');//用变量替换$array
对于StdObject,语法将为:
function my_sort($a,$b)
{
if ($a->srp == $b->srp) return 0;
return ($a->srp < $b->srp)?-1:1;
}
uasort($array, 'my_sort'); // replace $array with your variable
函数我的排序($a,$b)
{
如果($a->srp==$b->srp)返回0;
返回($a->srp<$b->srp)?-1:1;
}
uasort($array,'my_sort');//用变量替换$array
使用usort
并在其中传递用户定义函数
$tmp = new stdClass;
$tmp->_id="5911af8209ed4456d069b1d3";
$tmp->title="Zen M4S(Silver)";
$tmp->mrp=0;
$tmp->srp=1900;
$tmp1 = new stdClass;
$tmp1->_id="5911af8209ed4456d069b1d2";
$tmp1->title="Zen M4S(Silver)";
$tmp1->mrp=0;
$tmp1->srp=1000;
$tmp2 = new stdClass;
$tmp2->_id="5911af8209ed4456d069b1d4";
$tmp2->title="JIVI X525(Red)";
$tmp2->mrp=0;
$tmp2->srp=1250;
$new_array = array($tmp,$tmp1,$tmp2);
function sort_arr($a, $b)
{
return ($a->srp<$b->srp)?-1:1;
}
usort($new_array, "sort_arr");
var_dump($new_array);
试试下面的代码,
我希望这是有帮助的
PHP脚本
产出
echo”“;
打印($array\u json2);
回声“;
打印($array\u json4);
//Asc
排列
(
[0]=>stdClass对象
(
[\u id]=>5911af8209ed4456d069b1d2
[标题]=>禅M4S(银牌)2
[srp]=>1000
)
[1] =>stdClass对象
(
[\u id]=>5911af8209ed4456d069b1d4
[标题]=>禅宗M4S(银牌)1
[srp]=>1250
)
[2] =>stdClass对象
(
[\u id]=>5911af8209ed4456d069b1d3
[标题]=>禅宗M4S(银牌)1
[srp]=>1900
)
)
//描述
排列
(
[0]=>stdClass对象
(
[\u id]=>5911af8209ed4456d069b1d2
[标题]=>禅M4S(银牌)2
[srp]=>1000
)
[1] =>stdClass对象
(
[\u id]=>5911af8209ed4456d069b1d4
[标题]=>禅宗M4S(银牌)1
[srp]=>1250
)
[2] =>stdClass对象
(
[\u id]=>5911af8209ed4456d069b1d3
[标题]=>禅宗M4S(银牌)1
[srp]=>1900
)
)
如果答案重复,则可能会产生错误:在向SO发布问题之前,无法将stdClass类型的对象用作数组。尝试自我解决和研究。当你发布一个重复的问题时,你会造成网站膨胀,浪费志愿者的时间。这是重复的。可能重复的[ErrorException]uasort()要求参数2为有效回调,函数“my_sort”未找到或函数名无效您是否已添加my_sort函数并将$array
的名称更改为数组名?然后您应该查看此链接:同时检查如何调用函数:[ErrorException]uasort()期望参数2是有效的回调、未找到函数“my_sort”或函数名无效您能在此处发布返回该错误的代码吗?我的例子中没有我的排序。我也试过你的:uasort($topDeals,'cmp');var_dump($topDeals);函数cmp($a,$b){if($a['srp']==$b['srp']){return 0;}//返回acsending order return($a['srp']<$b['srp'])?-1:1;}好的,您想要为foreach()提供的答案[ErrorException]参数无效,它必须是不同的东西。在这个q/a中没有foreach
loopusort($topDeals,function($a,$b){return$b->srp-$a->srp;});var_dump($topDeals);这起作用了
class TopDealsDAOMongoImpl implements TopDealsDAO{
//put your code here
public function getTopDeals() {
$topDeals = json_decode(json_encode(MasterAffiliateProductMappingMongo::select("product_id","affiliate_id","title","our_product_id","mrp","srp","product_url","category_name","ID")->where('top_deal','true')->get()));
uasort($topDeals, array($this, "sort_arr"));
}
public function sort_arr($a,$b)
{
if ($a->srp == $b->srp) return 0;
return ($a->srp < $b->srp)?-1:1;
}
}
array (size=3)
0 =>
object(stdClass)[2]
public '_id' => string '5911af8209ed4456d069b1d2' (length=24)
public 'title' => string 'Zen M4S(Silver)' (length=15)
public 'mrp' => int 0
public 'srp' => int 1000
1 =>
object(stdClass)[3]
public '_id' => string '5911af8209ed4456d069b1d4' (length=24)
public 'title' => string 'JIVI X525(Red)' (length=14)
public 'mrp' => int 0
public 'srp' => int 1250
2 =>
object(stdClass)[1]
public '_id' => string '5911af8209ed4456d069b1d3' (length=24)
public 'title' => string 'Zen M4S(Silver)' (length=15)
public 'mrp' => int 0
public 'srp' => int 1900
<?php
// Static array.
$array=array();
$array[0]=array(
'_id'=>'5911af8209ed4456d069b1d3',
'title'=>'Zen M4S(Silver) 1',
'srp'=>1900
);
$array[1]=array(
'_id'=>'5911af8209ed4456d069b1d2',
'title'=>'Zen M4S(Silver) 2',
'srp'=>1000
);
$array[2]=array(
'_id'=>'5911af8209ed4456d069b1d4',
'title'=>'Zen M4S(Silver) 1',
'srp'=>1250
);
// For acending sorting.
function asc_sort_json($array1,$array2){
$on = 'srp';
if ($array1[$on] == $array2[$on]) {
return 0;
}
return ($array1[$on] < $array2[$on]) ? -1 : 1;
}
// For decending sorting.
function desc_sort_json($array1,$array2){
$on = 'srp';
if ($array1[$on] == $array2[$on]) {
return 0;
}
return ($array1[$on] < $array2[$on]) ? 1 : -1;
}
$array_json = json_encode($array); // encode array in json
$array_json1 = json_decode($array_json); // decode json from array because get json object
$array_json2 = json_decode(json_encode($array_json1),true); // parse json into array
usort($array_json2, "asc_sort_json"); // Call function for acending order.
$array_json2 = json_decode(json_encode($array_json2)); // Array to json.
$array_json1 = json_encode($array); // encode array in json
$array_json2 = json_decode($array_json1); // decode json from array because get json object
$array_json3 = json_decode(json_encode($array_json2),true); // parse json into array
usort($array_json3, "desc_sort_json"); // Call function for decending order.
$array_json4 = json_decode(json_encode($array_json2)); // Array to json.
?>
echo "<pre>";
print_r($array_json2);
echo "<pre>";
print_r($array_json4);
// Asc
Array
(
[0] => stdClass Object
(
[_id] => 5911af8209ed4456d069b1d2
[title] => Zen M4S(Silver) 2
[srp] => 1000
)
[1] => stdClass Object
(
[_id] => 5911af8209ed4456d069b1d4
[title] => Zen M4S(Silver) 1
[srp] => 1250
)
[2] => stdClass Object
(
[_id] => 5911af8209ed4456d069b1d3
[title] => Zen M4S(Silver) 1
[srp] => 1900
)
)
// Desc
Array
(
[0] => stdClass Object
(
[_id] => 5911af8209ed4456d069b1d2
[title] => Zen M4S(Silver) 2
[srp] => 1000
)
[1] => stdClass Object
(
[_id] => 5911af8209ed4456d069b1d4
[title] => Zen M4S(Silver) 1
[srp] => 1250
)
[2] => stdClass Object
(
[_id] => 5911af8209ed4456d069b1d3
[title] => Zen M4S(Silver) 1
[srp] => 1900
)
)