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!
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
}
}
?>
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...."));
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);
Related
I have 2 multi arrays, profiles and encoded arrays like this
$profiles = array(
array(
'user_id' => 'fcc3d884-fbef-438a-9c86-0ad52c9b1223',
'first_name' => 'Narñia',
'middle_name' => 'Ñ',
'last_name' => 'Cruz',
'ext' => ''
),
array(
'user_id' => '0d31557d-1e9f-4db3-ac0d-72e1709fe89c',
'first_name' => 'Randy',
'middle_name' => 'O',
'last_name' => 'Rocker',
'ext' => ''
),
array(
'user_id' => '0f93f169-cf56-49df-a76b-7596446104c6',
'first_name' => 'Qwerty',
'middle_name' => 'K',
'last_name' => 'Asdfg',
'ext' => ''
),
array(
'user_id' => '23b1f4a2-034c-43b4-96b7-3191d78cead1',
'first_name' => 'Johny',
'middle_name' => 'L',
'last_name' => 'Walker',
'ext' => ''
)
);
$encoded = array(
array(
'encoder_id' => '0d31557d-1e9f-4db3-ac0d-72e1709fe89c',
'fullname' => 'Randy O. Rocker',
'encoded' => 10,
),
array(
'encoder_id' => '23b1f4a2-034c-43b4-96b7-3191d78cead1',
'fullname' => 'John L. Walker',
'encoded' => 20,
)
);
Now i want to get some data from $profiles then combine to $encoded array when user_id and encoder_id is match, i have this code but it seems wrong it only gets "John L. Waler" data. here's my code.
$data = [];
foreach ($profiles as $key => $val) {
$user_id = $val['user_id'];
foreach($encoded as $k => $v){
$ext_name = ($val['ext'] == '') ? '' : $val['ext'];
$fullname = $val['first_name'].' '.substr($val['middle_name'], 0, 1).'. '.$val['last_name'].' '.$ext_name;
$data[$key] = array(
'id' => ($v['encoder_id'] == $user_id) ? $v['encoder_id'] : $user_id,
'fullname' => ($v['encoder_id'] == $user_id) ? $v['fullname'] : $fullname,
'encoded' => ($v['encoder_id'] == $user_id) ? $v['encoded'] : 0
);
}
}
echo '<pre>';
print_r($data);
echo '</pre>';
here's the result
Array
(
[0] => Array
(
[id] => fcc3d884-fbef-438a-9c86-0ad52c9b1223
[fullname] => Narñia �. Cruz
[encoded] => 0
)
[1] => Array
(
[id] => 0d31557d-1e9f-4db3-ac0d-72e1709fe89c
[fullname] => Randy O. Rocker
[encoded] => 0 //this should be 10
)
[2] => Array
(
[id] => 0f93f169-cf56-49df-a76b-7596446104c6
[fullname] => Qwerty K. Asdfg
[encoded] => 0
)
[3] => Array
(
[id] => 23b1f4a2-034c-43b4-96b7-3191d78cead1
[fullname] => John L. Walker
[encoded] => 20
)
)
Also after combining them to a new array. i want to sort the new array by "encoded" attribute descending.
Thank you for understanding
You can make your life easier by re-indexing the $encoded array by the encoder_id values using array_column; then you don't have to search the array each time to look for a user_id value, you can just use isset. Once you've extracted your data, you can use usort to sort by the encoded values:
$encoded_ids = array_column($encoded, null, 'encoder_id');
$data = array();
foreach ($profiles as $profile) {
$user_id = $profile['user_id'];
if (isset($encoded_ids[$user_id])) {
$data[] = array('id' => $user_id,
'fullname' => $encoded_ids[$user_id]['fullname'],
'encoded' => $encoded_ids[$user_id]['encoded']
);
}
else {
$data[] = array('id' => $user_id,
'fullname' => "${profile['first_name']} ${profile['middle_name']} ${profile['last_name']}",
'encoded' => 0
);
}
}
usort($data, function ($a, $b) { return $b['encoded'] - $a['encoded'];});
print_r($data);
Output:
Array
(
[0] => Array
(
[id] => 23b1f4a2-034c-43b4-96b7-3191d78cead1
[fullname] => John L. Walker
[encoded] => 20
)
[1] => Array
(
[id] => 0d31557d-1e9f-4db3-ac0d-72e1709fe89c
[fullname] => Randy O. Rocker
[encoded] => 10
)
[2] => Array
(
[id] => fcc3d884-fbef-438a-9c86-0ad52c9b1223
[fullname] => Narñia Ñ Cruz
[encoded] => 0
)
[3] => Array
(
[id] => 0f93f169-cf56-49df-a76b-7596446104c6
[fullname] => Qwerty K Asdfg
[encoded] => 0
)
)
Demo on 3v4l.org
An alternative to Nick's version, using array_reduce:
$encodedByEncoderId = array_column($encoded, null, 'encoder_id');
$combined = array_reduce($profiles, function (array $combined, array $profile) use ($encodedByEncoderId): array {
$combined[] = [
'id' => $profile['user_id'],
'fullname' => $encodedByEncoderId[$profile['user_id']]['fullname']
?? "{$profile['first_name']} {$profile['middle_name']}. {$profile['last_name']}",
'encoded' => $encodedByEncoderId[$profile['user_id']]['encoded']
?? 0
];
return $combined;
}, []);
Demo: https://3v4l.org/kKBru
Try this!
$data = [];
foreach ($profiles as $key => $val) {
$user_id = $val['user_id'];
$is_matched = 0;
$encoded_data = [];
foreach($encoded as $k => $v){
if ($user_id == $v['encoder_id']) {
$is_matched = 1;
$encoded_data = $v;
}
}
$ext_name = ($val['ext'] == '') ? '' : $val['ext'];
$fullname = $val['first_name'].' '.substr($val['middle_name'], 0, 1).'. '.$val['last_name'].' '.$ext_name;
$data[$key] = array(
'id' => ($is_matched == 1) ? $encoded_data['encoder_id'] : $user_id,
'fullname' => ($is_matched == 1) ? $encoded_data['fullname'] : $fullname,
'encoded' => ($is_matched == 1) ? $encoded_data['encoded'] : 0
);
}
I have two array:
Array 1:
Array ( [0] => Array ( [id] => et1 [supplier_id] => 4 [supplier_product_code] => 00054X [is_active] => 1 )
[1] => Array ( [id] => et2 [supplier_id] => 4 [supplier_product_code] => 000558 [is_active] => 1 )
[2] => Array ( [id] => et3 [supplier_id] => 5 [supplier_product_code] => 00054X [is_active] => 1 ));
Array 2:
Array ( [0] => Array ([id] => et1 [same_sku] => et3);
I need to delete all the same_skus in array1 from array2.
So from my result array I need array1 to be:
Array ( [0] => Array ( [id] => et1 [supplier_id] => 4 [supplier_product_code] => 00054X [is_active] => 1 )
[1] => Array ( [id] => et2 [supplier_id] => 4 [supplier_product_code] => 000558 [is_active] => 1 ));
Code that I have right now does not work.
public function search_array($array, $val)
{
foreach ($array as $key => $row)
{
if ($row['id'] === $val)
{
return $key;
}
}
}
foreach($array2->result() as $row)
{
$id = $row->id;
$same_sku = $row->same_sku;
$key = $this->search_array($array1, $id);
if(!empty($key))
{
$same_sku_key = $this->search_array($array1, $same_sku);
if(!empty($same_sku_key))
unset($array1[$same_sku_key]);
}
}
In the following code I have recreated the two arrays from your example. I then created a function that removes from a haystack array (array1) all of the sub arrays that have an "id" that matches the value of "same_sku" within a needle array (array2). The final line echos the result array.
EDIT
I have modified the original answer to pass the array values by reference and unset the unwanted sub arrays, instead of passing by value, looping, and returning another array. This should resolve the memory issue, as well as the other issue mentioned in your comment.
$array1 = array(
array(
'id' => 'et1',
'supplier_id' => '4',
'supplier_product_code' => '00054X',
'is_active' => '1'
),
array(
'id' => 'et2',
'supplier_id' => '4',
'supplier_product_code' => '000558',
'is_active' => '1'
),
array(
'id' => 'et3',
'supplier_id' => '5',
'supplier_product_code' => '00054X',
'is_active' => '1'
),
array(
'id' => 'et4',
'supplier_id' => '5',
'supplier_product_code' => '00054X',
'is_active' => '1'
)
);
$array2 = array(
array(
'id' => 'et1',
'same_sku' => 'et3'
),
array(
'id' => 'et2',
'same_sku' => 'et4'
)
);
function remove_same_sku(&$haystack, &$needles){
foreach($needles as $needle){
foreach($haystack as $key => $val){
if($val['id'] === $needle['same_sku']){
unset($haystack[$key]);
}
}
}
}
remove_same_sku($array1, $array2);
echo print_r($array1);
I have an multidimensional array like this:
$orders = array(
array(
'id' => '123',
'name' => 'John',
'lastname'=>'Carter',
'rate' => '1.0'
),
array(
'id' => '546',
'name' => 'Ben',
'lastname'=>'Wall',
'rate' => '0.25'
),
array(
'id' => '666',
'name' => 'John Bow',
'lastname'=>'Porter',
'rate' => '0.25'
),
array(
'id' => '156',
'name' => 'John',
'lastname'=>'Carter',
'rate' => '0.5'
)
);
and want make a function that will be delete duplicate rows that rate < 1.0. The result array should be:
$orders = array(
array(
'id' => '123',
'name' => 'John',
'lastname'=>'Carter',
'rate' => '1.0'
),
array(
'id' => '546',
'name' => 'Ben',
'lastname'=>'Wall',
'rate' => '0.25'
),
array(
'id' => '666',
'name' => 'John',
'lastname'=>'Porter',
'rate' => '0.25'
)
);
Not very beautiful and speed, but workable solution:
$a = array(...); // Your array
$hashstack = array_map(function($value) { return md5($value['name'] . $value['lastname']); }, $a);
$result = array_filter($a, function($value) use ($hashstack)
{
if ((float)$value['rate'] < 1.0) {
$count = 0;
foreach ($hashstack as $hash) {
if (md5($value['name'] . $value['lastname']) == $hash) {
$count++;
}
}
if ($count > 1) {
unset($value);
return;
}
}
return $value;
});
Result is the same of yours.
Edit: Second and good version here: http://ideone.com/zJ8vj
What do You mean by 'duplicated' - same 'rate' ?
Bellow example remove duplicated orders with same 'rate' and 'rate' < 1.0.
Is this what You need ?
$tmpOrders = array();
foreach($orders as $order) {
// Check only rate < 1.0
if((float)$order['rate'] < 1.0) {
// Check duplicate
$exists = false;
foreach($tmpOrders as $tmp) {
if($tmp['rate'] == $order['rate']) {
$exists = true;
break;
}
}
// Add if rate not exists
if(!$exists) {
$tmpOrders[] = $order;
}
}
// Add all with rate >= 1.0
else {
$tmpOrders[] = $order;
}
}
$orders = $tmpOrders;
echo "<pre>";
print_r($orders);
echo "<pre>";
Result:
Array
(
[0] => Array
(
[id] => 123
[name] => John
[lastname] => Carter
[rate] => 1.0
)
[1] => Array
(
[id] => 546
[name] => Ben
[lastname] => Wall
[rate] => 0.25
)
[2] => Array
(
[id] => 156
[name] => John
[lastname] => Carter
[rate] => 0.5
)
)
I have a multi-dimensional array in the following format:
[0] = (
'id' => '1',
'type' => 'fish',
'owner' => 'bob',
)
[1] = (
'id' => '2',
'type' => 'cat',
'owner' => 'mary',
)
[2] = (
'id' => '3',
'type' => 'dog',
'owner' => 'larry',
)
[3] = (
'id' => '2',
'type' => 'cat',
'owner' => 'fred',
)
I would like to search for a value, and they return an array that contains all keys from matching arrays and looks like this on a search for type=cat:
[0] = (
'id' => '2',
'type' => 'cat',
'owner' => 'mary',
)
[1] = (
'id' => '2',
'type' => 'cat',
'owner' => 'fred',
)
I know I'm trying to treat the array as a database, but in this case it's dynamic data that doesn't need to be stored once the program ends.
Any advice?
Loop through the array:
function loopAndFind($array, $index, $search){
$returnArray = array();
foreach($array as $k=>$v){
if($v[$index] == $search){
$returnArray[] = $v;
}
}
return $returnArray;
}
//use it:
$newArray = loopAndFind($oldArray, 'type', 'cat');
you must iterate array like:
foreach ($array as $i => $values) {
print "$i {\n";
foreach ($values as $key => $value) {
print " $key => $value\n";
}
print "}\n";
}
and then check for 'type' key value....
then record which is matched must copy it to new array...
I'm having a problem lately that's driving me crazy. I have a multi-dimensional array like this:
$a = array(
'db' => array(
'0' => array(
'id' => '1',
'name' => 'test',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
),
'1' => array(
'id' => '2',
'name' => 'test2',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
)
'2' => array(
'id' => '3',
'name' => 'test',
'cat' => array(
'a' => '50',
'b' => '40',
'c' => '90'
),
'canvas' => '1'
)
)
);
And i want to search on it using a function like this: search('canvas = 1');
That would return all the arrays, child of db, that have a key canvas with the value of 1. Or, for example:
search('a = 15');
Would return all arrays that have a key, child of cat, named a and with a value of 15.
$a = array(
'db' => array(
'0' => array(
'id' => '1',
'name' => 'test',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
),
'1' => array(
'id' => '2',
'name' => 'test2',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
),
)
);
//checks if array $array contains element with $searchKey key, and $searchVal value
function arrayContains($array, $searchVal, $searchKey) {
if (!is_array($array))
return false;
foreach ($array as $key => $value) {
if ($key === $searchKey && $searchVal === $value)
return true;
if (is_array($value) && arrayContains($value, $searchVal, $searchKey))
return true;
}
return false;
}
function search($a, $search) {
list($searchKey, $searchVal) = explode('=', $search);
$result = array();
foreach($a as $val) {
if (arrayContains($val, $searchVal, $searchKey))
$result[] = $val;
}
return $result;
}
print_r(search($a['db'], "a=15"));
print_r(search($a['db'], "canvas=1"));
Which produces this output(outputs sub-arrays of $a['db'] which contain searched key=>value pair):
Array
(
[0] => Array
(
[id] => 1
[name] => test
[cat] => Array
(
[a] => 15
[b] => 20
[c] => 30
)
[canvas] => 2
)
[1] => Array
(
[id] => 2
[name] => test2
[cat] => Array
(
[a] => 15
[b] => 20
[c] => 30
)
[canvas] => 2
)
)
Array
(
[0] => Array
(
[id] => 3
[name] => test
[cat] => Array
(
[a] => 50
[b] => 40
[c] => 90
)
[canvas] => 1
)
)
Just check the below link if this can help you -
http://php.net/manual/en/function.array-search.php
It contains detailed documentation of php function array_search() and various user codes for searching in multi-dimensional array along with user reviews.
function search($array, $canvas)
{
$result = array();
foreach ($array as $k1 => $v1) {
foreach ($v1 as $k2 => $v2) {
if ($v2['canvas'] == $canvas) {
$result[] = $array[$k1][$k2];
}
}
}
return $result;
}
// $a = your array
print_r(search($a, 1));