1

i faced with problem and i hope that you can help

I have array like this (but with 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 to find all subarrays that equals by 'previewUrl' and then find among them one with max value of 'payout' and delete others with smaller value.

Thank you!

3 Answers 3

1

Loop through the original array ($arr) collecting the maximum payout values in a temporary array ($max_arr). When a higher payout is found replace the previous higher payout in the temporary array and delete it in the original array. When a lower or equal payout is 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 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
  }
}

?>
Sign up to request clarification or add additional context in comments.

Comments

1

You can try with the following:

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...."));

Comments

1

Not necessarily fast, but it's pretty easy to understand.

$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.