delete subarrays from multiple array by equal values of key PHP -


i faced problem , hope can help

i have array (but hundreds of subarrays):

array (     [0] => array         (             [id] => 211             [name] => name              [description] => desc              [link] => http://link/211             [previewurl] => https://link/id885364?mt=8             [payout] => 0.30             [image] => http://link/ios.png             [categories] => array                 (                     [0] => ios                     [1] => games                 )          )      [1] => array         (             [id] => 2             [name] => name              [description] => desc              [link] => http://link/211             [previewurl] => https://link/id885364?mt=8             [payout] => 2             [image] => http://link/ios.png             [categories] => array                 (                     [0] => ios                     [1] => games                 )          )         ) 

i need find subarrays equals 'previewurl' , find among them 1 max value of 'payout' , delete others smaller value.

thank you!

loop through original array ($arr) collecting maximum payout values in temporary array ($max_arr). when higher payout found replace previous higher payout in temporary array , delete in original array. when lower or equal payout found delete it.

<?php  $arr = array(array('id' => 211, 'name' => 'name', 'description' => 'desc', 'link' => 'http://link/211', 'previewurl' => 'https://link/id885364?mt=8', 'payout' => '0.30', 'image' => 'http://link/ios.png', 'categories' => array('0' => 'ios', '1' => 'games')), array('id' => 2, 'name' => 'name', 'description' => 'desc', 'link' => 'http://link/211', 'previewurl' => 'https://link/id885364?mt=8', 'payout' => '2', 'image' => 'http://link/ios.png', 'categories' => array('0' => 'ios', '1' => 'games')), array('id' => 11, 'name' => 'name', 'description' => 'desc', 'link' => 'http://link/211', 'previewurl' => 'https://link/id885364?mt=7', 'payout' => '3', 'image' => 'http://link/ios.png', 'categories' => array('0' => 'ios', '1' => 'games')), array('id' => 1, 'name' => 'name', 'description' => 'desc', 'link' => 'http://link/211', 'previewurl' => 'https://link/id885364?mt=7', 'payout' => '1', 'image' => 'http://link/ios.png', 'categories' => array('0' => 'ios', '1' => 'games')));   $max_arr = array(); // temporary array  foreach ( $arr $key => $value ) {   if ( !isset($max_arr[$value['previewurl']]) ) {     $max_arr[$value['previewurl']] = array_merge(array('key' => $key), $value);   }   else {     // higher payout     if ( $max_arr[$value['previewurl']]['payout'] < $value['payout'] ) {       unset($arr[$max_arr[$value['previewurl']]['key']]);       $max_arr[$value['previewurl']] = array_merge(array('key' => $key), $value);     }     else { unset($arr[$key]); } // lower or equal payout   } }  ?> 

Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -