如何比较两个键和值并从数组中找到最大键值 [英] How to compare two key and value and find max key value from array

查看:65
本文介绍了如何比较两个键和值并从数组中找到最大键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要如何比较两个键和值以及从数组中找到最大键值的解决方案。



解释: - 如果**我在这下面有数组** url **和** con code **两者都比**更高**数量**数组应该显示并且应该显示不匹配数据请为其提供溶剂。



$ array = array(

array(

id=> 1,

amount=> 10,

url=>'https://play.google.com/store/apps/details?id=com.my.heroesofutopia',

con= > array('US'=> array('id'=>'1','code'=>'US')),

),

数组(

id=> 2,

金额=> 3,

url=> 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia',

con=>数组('US'=>数组( 'id'=>'1','code'=>'US')),

),

数组(

id=> 3,

金额=> 19,

url=> 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia',

con=> array('US'=> array('id'=>'1','code'=>'US'),'Uk'=> array('id'=>'2','代码'=>'英国')),

),

数组(

id=> 4,
金额=> 50,

url=>'https://play.google.com/store/apps/details?id=com.my。 heroesofutopia1111',

con=> array('US'=> array('id'=>'1','code'=>'US'),'英国'=>数组('id'=>'2','代码'=>'英国')),

),

);



请查看我想要的结果

在这个例子中,1和2 id有相同的url和国家代码所以从这两个我们必须选择更高的数量one.so我们选择这个并且id 3和4不符合这个标准所以它在这个数组中将保持相同,因为ID 3具有相同的url但具有不同的国家代码并且ID 4具有不同的url。



i想要这个结果。请为此提供溶剂,

提前感谢... !!



数组(id=> 1,

金额=> 10,

url=>'https://play.google .com / store / apps / details?id = com.my.heroesofutopia',

con=> array('US'=> array('id'=>'1) ','code'=>'US')),

),

数组(

id=> 3,

金额=> 19,

url=>'https://play.google.com/store/apps/details?id=com .M y.heroesofutopia',

con=> array('US'=> array('id'=>'1','code'=>'US'),'Uk'=> array('id'=>'2','代码'=>'英国')),

),

数组(

id=> 4,
金额=> 50,

url=>'https://play.google.com/store/apps/details?id=com.my。 heroesofutopia1111',

con=> array('US'=> array('id'=>'1','code'=>'US'),'英国'=>数组('id'=>'2','代码'=>'英国')),

),



我尝试了什么:



我的代码就是







$ output = [];

foreach($ array as $ data){



if(!isset($ output [$ data ['url']])){



$ output [$ data [ 'url']] = $ data;



}

elseif($ output [$ data ['url']] ['amount']< $ data ['amount']){



$输出[$ data ['url']] = $ data;

}



}

echo

I need solution for How to compare two key and value and find max key value from array.

explain:- I have below array in this if **url** and **con code** both are same than higher **amount** array should display and unmatch data should be display please provide sollution for same .

$array = array(
array(
"id" => 1,
"amount" => 10,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia',
"con" => array('US' =>array('id'=> '1','code'=>'US')),
),
array(
"id" => 2,
"amount" => 3,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia' ,
"con" => array('US' =>array('id'=> '1','code'=>'US')),
),
array(
"id" => 3,
"amount" => 19,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia' ,
"con" => array('US' =>array('id'=> '1','code'=>'US'),'Uk' =>array('id'=> '2','code'=>'UK')),
),
array(
"id" => 4,
"amount" => 50,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia1111' ,
"con" => array('US' =>array('id'=> '1','code'=>'US'),'Uk' =>array('id'=> '2','code'=>'UK')),
),
);

Please check result i want
in this example 1 and 2 id have same url and country code so from this two we have to choose higher amount one.so we choose this and id 3 and 4 are not match this criteria so its will remain same in this array because ID 3 has same url but has different country code and ID 4 has different url.

i want this result.please provide sollution for same,
thanks in advance...!!

array( "id" => 1,
"amount" => 10,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia',
"con" => array('US' =>array('id'=> '1','code'=>'US')),
),
array(
"id" => 3,
"amount" => 19,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia' ,
"con" => array('US' =>array('id'=> '1','code'=>'US'),'Uk' =>array('id'=> '2','code'=>'UK')),
),
array(
"id" => 4,
"amount" => 50,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia1111' ,
"con" => array('US' =>array('id'=> '1','code'=>'US'),'Uk' =>array('id'=> '2','code'=>'UK')),
),

What I have tried:

My code is for that is



$output = [];
foreach($array as $data){

if(!isset($output[$data['url']]) ){

$output[$data['url']] = $data;

}
elseif( $output[$data['url']]['amount'] < $data['amount']){

$output[$data['url']] = $data;
}

}
echo "

"; print_r($output);

推荐答案

array = array(

array(
$ b) $ bid=> 1,

金额=> 10,

url=>'https://play.google.com /store/apps/details?id=com.my.heroesofutopia',

con=> array('US'=> array('id'=>'1', 'code'=>'US')),

),

数组(

id=> 2,

金额=> 3,

url=>'https://play.google.com/store/apps/details?id=com.my .heroesofutopia',

con=> array('US'=> array('id'=>'1','code'=>'US')),

),

数组(

id=> 3,

金额=> 19,

url=>'https://play.google.com/store/apps/details?id=com.my.heroesofutopia',

con=>数组(' US'=> array('id'=>'1','code'=>'US'),'Uk'=> array('id'=> '2','code'=>'UK')),

),

数组(

id=> 4,

金额=> 50,

url=>'https://play.google.com/store/apps/details?id = com.my.heroesofutopia1111',

con=> array('US'=> array('id'=>'1','code'=>'美国'),'Uk'=>数组('id'=>'2','code'=>'英国')),

),

);



请查看我想要的结果

在这个例子中,1和2 id有相同的url和国家代码所以从这两个我们必须选择更高的数量one.so我们选择这个并且id 3和4不符合这个标准所以它在这个数组中将保持相同,因为ID 3具有相同的url但具有不同的国家代码并且ID 4具有不同的url。



i想要这个结果。请为此提供溶剂,

提前感谢... !!



数组(id=> 1,

金额=> 10,

url=>'https://play.google .com / store / apps / details?id = com.my.heroesofutopia',

con=> array('US'=> array('id'=>'1) ','code'=>'US')),

),

数组(

id=> 3,

金额=> 19,

url=>'https://play.google.com/store/apps/details?id=com .M y.heroesofutopia',

con=> array('US'=> array('id'=>'1','code'=>'US'),'Uk'=> array('id'=>'2','代码'=>'英国')),

),

数组(

id=> 4,
金额=> 50,

url=>'https://play.google.com/store/apps/details?id=com.my。 heroesofutopia1111',

con=> array('US'=> array('id'=>'1','code'=>'US'),'英国'=>数组('id'=>'2','代码'=>'英国')),

),



我尝试了什么:



我的代码就是






array = array(
array(
"id" => 1,
"amount" => 10,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia',
"con" => array('US' =>array('id'=> '1','code'=>'US')),
),
array(
"id" => 2,
"amount" => 3,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia' ,
"con" => array('US' =>array('id'=> '1','code'=>'US')),
),
array(
"id" => 3,
"amount" => 19,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia' ,
"con" => array('US' =>array('id'=> '1','code'=>'US'),'Uk' =>array('id'=> '2','code'=>'UK')),
),
array(
"id" => 4,
"amount" => 50,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia1111' ,
"con" => array('US' =>array('id'=> '1','code'=>'US'),'Uk' =>array('id'=> '2','code'=>'UK')),
),
);

Please check result i want
in this example 1 and 2 id have same url and country code so from this two we have to choose higher amount one.so we choose this and id 3 and 4 are not match this criteria so its will remain same in this array because ID 3 has same url but has different country code and ID 4 has different url.

i want this result.please provide sollution for same,
thanks in advance...!!

array( "id" => 1,
"amount" => 10,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia',
"con" => array('US' =>array('id'=> '1','code'=>'US')),
),
array(
"id" => 3,
"amount" => 19,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia' ,
"con" => array('US' =>array('id'=> '1','code'=>'US'),'Uk' =>array('id'=> '2','code'=>'UK')),
),
array(
"id" => 4,
"amount" => 50,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia1111' ,
"con" => array('US' =>array('id'=> '1','code'=>'US'),'Uk' =>array('id'=> '2','code'=>'UK')),
),

What I have tried:

My code is for that is




output = [];

foreach(
output = [];
foreach(


数组为


这篇关于如何比较两个键和值并从数组中找到最大键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆