How to filter array using same key value of(preview_url and countries code) and return higher amount of array and unmatch array data will be same

I have this arrayif preview_url and countries code are same then i want result which have higher amount of value and unmatch array data remain same. I want result i have add in comment please provide sollution for same i have stuck on this issue from couple of days

Thanks in advance
I have this array

Array
(
    [id] => 377556        
    [amount] => 1.46000
    [preview_url] => myurl
    [countries] => Array
        (
            [US] => Array
                (
                    [id] => 840
                    [code] => US
                    [name] => united states
                    
                )
        )
)
Array
(
    [id] => 377557        
    [amount] => 2.46000
    [preview_url] => myurl
    [countries] => Array
        (
            [US] => Array
                (
                    [id] => 840
                    [code] => US
                    [name] => united states                        
                )
            [UK] => Array
                (
                    [id] => 841
                    [code] => UK
                    [name] => united kingdom
                )    
        )
)
Array
(
    [id] => 377558       
    [amount] => 7.46000
    [preview_url] => myurl1
    [countries] => Array
        (
            [US] => Array
                (
                    [id] => 840
                    [code] => US
                    [name] => united states
                    [regions] => Array
                        (
                        )
                )
        )
)

I want this result please provide sollution for same

Array
(
    [id] => 377557        
    [amount] => 2.46000
    [preview_url] => myurl
    [countries] => Array
        (
            [US] => Array
                (
                    [id] => 840
                    [code] => US
                    [name] => united states                        
                )
            [UK] => Array
                (
                    [id] => 841
                    [code] => UK
                    [name] => united kingdom
                )    
        )
)
Array
(
    [id] => 377556        
    [amount] => 1.46000
    [preview_url] => myurl1
    [countries] => Array
        (
            [US] => Array
                (
                    [id] => 840
                    [code] => US
                    [name] => united states
                   
                )
        )
)

as you do not provide any code it looks like your are stuck because you tried exactly nothing on your own and just asking others to do your work.

I’m having trouble seeing what you’re after. Removing the empty regions array?

I often read code better than I can read English :books: Please post the PHP code you have that is the closest to what you want the code to do.

i have try but not geting proper result please check my code
foreach($array as $key=>$data){

   if(!isset($output[$data['url']]) && !isset($output[$data['con']])){       	
		//echo "<pre>";print_r($output);
        $output[$data['id']] = $data;
    }
    elseif( $output[$data['id']]['amount'] < $data['amount']){
    	echo "hello";
        $output[$data['id']] = $data;
    }
      	                
}
$array = array( 
  array(
    "id" => 1, 
    "amount" => 10,
    "url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia',
    "con" => 'us',
    ),
    array( 
    "id" => 2, 
    "amount" => 3,
    "url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia'  ,
    "con" => 'us',  
	),
	 array( 
    "id" => 3, 
    "amount" => 19,
    "url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia'  ,
    "con" => 'uk',  
	),
	array( 
    "id" => 4, 
    "amount" => 3, 
    "url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia1' ,
    "con" => 'uk',    
	),
	array( 
    "id" => 5 ,
    "amount" => 5,  
    "url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia111' ,
    "con" => 'uk',      
	),
	array( 
    "id" => 6, 
    "amount" => 10,  
    "url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia111' ,
    "con" => 'us',      
	),
	array( 
    "id" => 7, 
    "amount" => 65, 
    "url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia1' ,
    "con" => 'us',    
	) ,
	array( 
    "id" => 8, 
    "amount" => 45, 
    "url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia1' ,
    "con" => 'us',    
	) 
	            
);
$output= []
foreach($array as $key=>$data){   
   
       if(!isset($output[$data['url']]) && !isset($output[$data['con']])){       	
			//echo "<pre>";print_r($output);
	        $output[$data['id']] = $data;
	    }
	    elseif( $output[$data['id']]['amount'] < $data['amount']){
	    	echo "hello";
	        $output[$data['id']] = $data;
	    }

deleted a working answer due to your PM and reported you for violating language

(sorry for everybody who encountered the same problem)[quote=“nilayadattract, post:6, topic:259035”]If you don’t able to answer this question so keep calm and shut your mouth.[/quote]

Hello u provide a wrong sollution so don’t be a smart its not working for me

OK people, take a breath and calm down.
I suspect that @chorn will not be participating in this topic any further, so let it go.
@nilayadattract you may just have to wait for someone else to help you with this.

2 Likes

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')),    
    	), 

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 "<pre>"; print_r($output);

I haven’t had time to test this but it may give you want you need.

<?php

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

$output = array_reduce($array, function ($carry, $itemA) {
    return array_filter($carry, function ($itemB) use($itemA) {
        return ( 
            $itemB['url'] != $itemA['url']
            || $itemB['con'] != $itemA['con']
            || $itemB['amount'] >= $itemA['amount']
        );
    });
}, $array);
1 Like

Hello davidtsadler,

Thank you so much for helping me in this its working properly what i want.

thanks for the help.

Regards,
Nilay Parekh

Hello davidtsadler,

Is there one issue in that if amount value are same in array than both array was display even if preview url and country was same.

how can i solve this.

Regards,
Nilay Parekh

for example

$array = [
    [
        'id'     => 1, 
        'amount' => 10,
        'url'    => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia',
        'con'    => ['US' => ['id' => '1', 'code' => 'US']],
    ],
    [
        'id'     => 2, 
        'amount' => 10,
        'url'    => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia'  ,
        'con'    => ['US' => ['id' => '1', 'code' => 'US']],  
    ],
      
];

if this array return both result even if url and con are same i want only one from this how can i solve this please provide sollution for same
thanks in advance.

Hi @nilayadattract Thought it best to let you know that as of yesterday I’m on holiday. I won’t be able to look into any issues with the code until I’m back in 2 weeks. I’m sorry that I can’t help at the moment.

Kind Regards,

David

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.