I have a multidimensional array in PHP where I need to remove one array based on the value of an item in one of the arrays:
Example Array
array(
"0"=>array("0"=>"joe", "1"=>"2018-07-18 09:00:00"),
"1"=>array("0"=>"tom", "1"=>"2018-07-17 09:00:00"),
"2"=>array("0"=>"joe", "1"=>"2018-07-14 09:00:00")
)
I know that I want to remove the array that contains joe in key 0, but I only want to remove the array that contains joe with the most current date in key1. The following output is what I'm trying to accomplish:
array(
"0"=>array("0"=>"tom", "1"=>"2018-07-17 09:00:00"),
"1"=>array("0"=>"joe", "1"=>"2018-07-14 09:00:00")
)
Is there a simple way to do this in PHP aside from looping through each array?
Here is a non looping method that uses array_intersect and array_column to find the "Joe's" and then deletes the maximum array_key since I first sort the array on dates.
usort($arr, function($a, $b) {
return $a[1] <=> $b[1];
}); // This returns the array sorted by date
// Array_column grabs all the names in the array to a single array.
// Array_intersect matches it to the name "Joe" and returns the names and keys of "Joe"
$joes = array_intersect(array_column($arr, 0), ["joe"]);
// Array_keys grabs the keys from the array as values
// Max finds the maximum value (key)
$current = max(array_keys($joes));
unset($arr[$current]);
var_dump($arr);
https://3v4l.org/mah6K
Edit forgot to add the array_values() if you want to reset the keys in the array.
Just add $arr = array_values($arr); after the unset.
I would go about it like this:
<?php
$foo = array(
"0"=>array("0"=>"joe", "1"=>"2018-07-18 09:00:00"),
"1"=>array("0"=>"tom", "1"=>"2018-07-17 09:00:00"),
"2"=>array("0"=>"joe", "1"=>"2018-07-14 09:00:00")
);
$tmp = [];
foreach($foo as $k => $v) {
if ($v[0] === 'joe') {
$tmp[$v[1]] = $k;
}
}
if (!empty($tmp)) {
sort($tmp); //think that is sane with date format?
unset($foo[reset($tmp)]);
}
var_dump($foo);
Not sure if you don't want to loop on principal or what... I tend to go for readability. Find all occurrences of joe. Sort on date. Remove the most recent by key.
Related
I have a multi-D array like so:
array ( 'JD'=>2457002.50, 67.618536),
array ( 'JD'=>2457003.50, 67.619705),
array ( 'JD'=>2457004.50, 67.620938)....
I have a value say:
$MyJD = 2457003.9553;
I would like to find the value in the array, and if not, match the closest number to the array in question and return the the next index (which i'm assuming is [1])
I was thinking to do an array_search, but it's not going to find the exact number, I want the closest number to $MyValue?
This won't return the index but will return the proper array:
array_multisort(array_map(function($v) use($MyJD) {
return abs($v['JD'] - $MyJD);
}, $array), $array);
$result = reset($array);
Calculate the difference between each JD value and $MyJD
Sort on the difference (sorting the original) and get the lowest (first) one
Alternately, you could combine using the difference as the key and then sort on the keys:
$array = array_combine(array_map(function($v) use($MyJD) {
return abs($v['JD'] - $MyJD);
}, $array), $array);
ksort($array);
$result = reset($array);
Maybe someone will post a good array_reduce answer.
Do bucle put new array with key difference between
In this way you will have array ordered by difference
In each iteration
$myarray[myvalue - yourvalueinbucle] if -1 this key then ×-1 and how value your enter iterator of bucle, then your value for this key = your line multiarrayvalue in for each iterator
I have this array and i want to compare each key items with each other and get similars with array_intersect . I wrote this code but it just compare first 2 key items and i want to compare all of key items.
$res_arr = array_shift($m);
foreach($m as $filter){
$arr = array_intersect($res_arr, $filter);
}
If you want the intersection of all the arrays inside the initial array, you need to perform the intersection of the elements with the intersection of the previous elements:
$arr = [
0=>[...],
1=>[...],
2=>[...],
];
$intersection = array_pop($arr);
foreach($arr as $el)
$intersection = array_intersect($arr, $intersection);
print_r($intersection);
Please not that in your example you have 1+ empty array, and so the intersection will be an empty array
I have a multidimensional array:
$arr = array(
array('lions', 'tigers', 'bears'), // count = 3
array('dogs', 'cats'), // count = 2
array('horses', 'pigs', 'cattle', 'sheep', 'chickens') // count = 5
);
I want to return the array with the lowest count (I don't need to know the count, just need the array that HAS the lowest count). In this case, array('dogs', 'cats')
Right now I have:
$lowest = null;
foreach($nodePath as $arr)
{
$lowest = count($arr) < count($lowest) || $lowest == null ? $arr : $lowest;
}
This works but I'm wondering if I missed a more contained solution, perhaps using array_map, array_walk or a similar function.
Use array_map() with count as a callback to get the number of elements in each array, min() to get the smallest value. Then, to get the key of the smallest array - use array_flip() to flip the arrays keys and values, and access the $minth index. Now you can use the key to get the array you need:
$counts = array_map('count', $arr);
$min = min($counts);
$key = array_flip($counts)[$min];
$smallest_arr = $arr[$key];
Demo
Map your array to another array with counts of each child array. Get the key of the minimum value in this new array. Smallest array has the key of the minimum value:
$count = array_map('count', $arr);
$min = array_keys($count , min($count))[0];
var_dump($arr[$min]); // Array ( [0] => dogs [1] => cats )
Eval.in
How can I delete duplicates in array?
For example if I had the following array:
$array = array('1','1','2','3');
I want it to become
$array = array('2','3');
so I want it to delete the whole value if two of it are found
Depending on PHP version, this should work in all versions of PHP >= 4.0.6 as it doesn't require anonymous functions that require PHP >= 5.3:
function moreThanOne($val) {
return $val < 2;
}
$a1 = array('1','1','2','3');
print_r(array_keys(array_filter(array_count_values($a1), 'moreThanOne')));
DEMO (Change the PHP version in the drop-down to select the version of PHP you are using)
This works because:
array_count_values will go through the array and create an index for each value and increment it each time it encounters it again.
array_filter will take the created array and pass it through the moreThanOne function defined earlier, if it returns false, the key/value pair will be removed.
array_keys will discard the value portion of the array creating an array with the values being the keys that were defined. This final step gives you a result that removes all values that existed more than once within the original array.
You can filter them out using array_count_values():
$array = array('1','1','2','3');
$res = array_keys(array_filter(array_count_values($array), function($freq) {
return $freq == 1;
}));
The function returns an array comprising the original values and their respective frequencies; you then pick only the single frequencies. The end result is obtained by retrieving the keys.
Demo
Try this code,
<?php
$array = array('1','1','2','3');
foreach($array as $data){
$key= array_keys($array,$data);
if(count($key)>1){
foreach($key as $key2 => $data2){
unset($array[$key2]);
}
}
}
$array=array_values($array);
print_r($array);
?>
Output
Array ( [0] => 2 [1] => 3 )
PHP offers so many array functions, you just have to combine them:
$arr = array_keys(array_filter(array_count_values($arr), function($val) {
return $val === 1;
}));
Reference: array_keys, array_filter, array_count_values
DEMO
Remove duplicate values from an array.
array_unique($array)
$array = array(4, "4", "3", 4, 3, "3");
$result = array_unique($array);
print_r($result);
/*
Array
(
[0] => 4
[2] => 3
)
*/
If I have an associative array like this
$array = array();
$array['e01'] = '03/16/2012';
$array['e02'] = '03/14/2012';
$array['e05'] = '03/01/2014';
I'd like to sort the array by date then loop through the results to get the value of the index, i.e. e01, e02, e05. I need that index to retrieve information from an object.
Try uasort() to sort your array:
function mysort($a, $b) {
return (strtotime($a) < strtotime($b)) ? -1 : 1;
}
uasort($array, 'mysort');
For getting the keys you can just use array_keys()
When sorting an associative array, the keys and values change order but the key to value association will not change. So if key e01 has a value of 03/16/2012, even after a uasort (which is probably what you are looking for, see Crashspeeder's answer) e01 will still have the value 03/16/2012. If you want to change the order of the values while keeping the order of the keys:
use array_keys to get the current order of the keys
use usort (no need for uasort as we aren't preserving the keys) to sort the values by date
use array_combine to make a new array with the keys and values.
From the PHP documentation on asort(). This will sort it by value and let you access the key and value of each index:
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
Use the array_keys function.