通过密钥PHP的相等值从多个数组中删除子数组

时间:2014-05-29 20:49:48

标签: php arrays

我遇到问题,希望你能帮忙

我有这样的数组(但有数百个子数组):

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
                )

        )
        )

我需要找到所有等于' previewUrl'的子阵列。然后在其中找到一个最大值为' payout'并删除价值较小的其他人。

谢谢!

3 个答案:

答案 0 :(得分:1)

您可以尝试以下操作:

class MyArrayParser{
    var $preview_url;
    var $max_payout;

    function filter_preview_url($subarray)
    {
        return $subarray["previewUrl"] == $this->preview_url;
    }

    function filter_payout($subarray)
    {
        return $subarray["payout"] == $this->max_payout;
    }

    function search_max_payout_by_previewUrl($big_array,$preview_url)
    {
        $this->preview_url = $preview_url;

        $filtered_array = array_filter($big_array,array($this,"filter_preview_url"));  //Only subarrays with previewUrl == $preview_url


        function payout_extract($subarray)
        {
            return $subarray["payout"];
        }

        $payouts_list = array_map("payout_extract",$filtered_array);

        if(count($payouts_list)==0) //PreviewUrl not found
            return array();

        $this->max_payout = max($payouts_list);

        $only_max_payout_list = array_filter($filtered_array,array($this,"filter_payout"));

        return $only_max_payout_list;
    }

}

$obj = new MyArrayParser();
$filtered_array = $obj->search_max_payout_by_previewUrl($my_big_array,"....previewUrl...."));

答案 1 :(得分:1)

循环遍历原始数组($ arr),收集临时数组中的最大支付值($ max_arr)。当找到更高的支付时,替换临时阵列中先前的更高支付并将其删除在原始阵列中。当找到较低或相等的支付时,将其删除。

<?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 as $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
  }
}

?>

答案 2 :(得分:1)

不一定快,但它很容易理解。

$a = array(
    array ('id' => 211,'previewUrl' => 'https://link/id885364?mt=8','payout' => 0.30),
    array ('id' => 2,'previewUrl' => 'https://link/id885364?mt=8','payout' => 2));

$searchUrl = 'https://link/id885364?mt=8';
$to_delete = array();
$max_payout = -1;
$max_key = "";

// Loop through array, looking at previewUrls that match.
foreach ($a as $key => $subarray) {
  if ($subarray['previewUrl'] == $searchUrl) {
    // Save all matches to an array for deletion.
    array_push($to_delete, $key);
    // Find the element with the highest payout.
    if ($subarray['payout'] > $max_payout || $max_payout == -1) {
      $max_payout = $subarray['payout'];
      $max_key = $key;
    }
  }
}
// Remove the element with the highest payout.
if (($key = array_search($max_key, $to_delete)) !== false) {
  unset($to_delete[$key]);
}

//print $max_payout;
//print $max_key;
//print_r($to_delete);

// Finally, delete all the elements flagged for deletion.
foreach ($to_delete as $key) {
  unset($a[$key]);
}
print_r($a);