How do I remove NaN values from an array in php?
$array = [1, 3, 5, 3, float(NaN), 4, float(NaN)];
$desired_result = [1, 3, 5, 3, 4];
To remove any other element, I found this solution, but I can't get it to work for NaN.
https://stackoverflow.com/a/7225113/9606753
Context: I am reading data from an rrd Database. I want to calculate the mean across several data entries, however at least one of them is float(NaN).
Use array_filter to remove NaN with is_nan function.
$array = [1, 3, 5, 3, float(NaN), 4, float(NaN)];
$filtered_array = array_filter($array, function ($element) {
return !is_nan($element);
});
Note: This will also remove numbers that are stored as strings, '21' for example would be removed.
Related
This question already has answers here:
Merge two 2d arrays by shared column value
(6 answers)
PHP - Merging two arrays into one array (also Remove Duplicates)
(9 answers)
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
If I have two super-simple arrays, as such:
$a = [1, 2, 3];
$b = [2, 3, 4, 5];
How do I combine them so that the result is a single array without duplicate values? i.e.,
[1, 2, 3, 4, 5];
I would have thought this could be handled with a simple function like array_merge() or array_replace(), but those operate on keys in an associative array. I need them to go by value. I don't care about the indices in the array and I can always sort it by value if I need to using one of the many sorting methods provided by PHP.
The same problem exists with Laravel's Collections: ->merge(), ->concat(), ->replace(), ->union(), etc. don't work on values at all.
It seems incredibly inefficient to me that the only way to accomplish this is to write several lines, including a foreach() loop with if() conditions inside that use in_array() to decide whether or not to add each value from the second array to the first.
Is there a better way?
Edit 09/08/2022: Although this question was initially flagged as a duplicate of another one, the accepted answer to that question never actually answered the OP's question and, by extension, my own question of how to sort by value. In addition, the accepted answer of "use array_merge_recursive()" does not, by itself, answer my question. Finally, the question in that post involves an associative array, whereas my question disregards keys entirely. Thus, the reason why I posted this question and do not consider it a duplicate.
You can use array_unique() & array_merge() to merge and remove duplicates :
$a = [1, 2, 3];
$b = [2, 3, 4, 5];
$array = array_unique(array_merge($a, $b));
// output [1,2,3,4,5]
You can use Arr::collapse to combine multiple array and use array_unique to remove duplicate and use array_values for reindexing
e.g.
make sure to include Arr helpers
use Illuminate\Support\Arr;
then
$array = Arr::collapse([ [1, 2, 3], [2, 3, 4, 5]]);
return array_values(array_unique($array ));
Type-1: (Array Merge)
$array1 = [1, 2, 3];
$array2 = [2, 3, 4];
$finalArray = array_merge($array1, $array2);
// output [1, 2, 3, 2, 3, 4]
Type-2: (Unique array after merge)
$array1 = [1, 2, 3];
$array2 = [2, 3, 4];
$finalArray = array_unique(array_merge($array1, $array2));
// output [1, 2, 3, 4]
array_unshift is using for inserting one or many values at the beginning of an array. Now I have an array-
$data = [1, 3, 4];
Another array is needed to insert at the beginning of the $data array.
$values = [5, 6];
Here I want to insert the values 5, 6 at the beginning of the $data array, and the resulting $data would be-
$data = [5, 6, 1, 3, 4];
Note: I know there is a way like array_unshift($data, ...$values); but this is working from php7.3.x AFAIK. I need to do it below php7.3.
Is there any way to do this without looping the $values array in the reverse order?
Function array_merge exists since PHP 4:
<?php
$data = [1, 3, 4];
$values = [5, 6];
$data = array_merge($values, $data);
print_r($data);
Live PHP sandbox
You should use array_merge instead of array_unshift.
$data = [1, 3, 4];
$values = [5, 6];
$result = array_merge($values, $data); // the sequence of the array inside array_merge will decide which array should be merged at the beginning.
Can someone help me for my College Exam. I tried to search but Im totally newbie in php and Im still studying
Here's what i want, Can you give me some Idea or function so that I can arrange an array of positive integers to form the largest numerical string?
For Example:
$arrnew = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
assert(getLargestNumStr($arrnew) == 98765432110, 'Basic test 9-8-7-6-5-4-3-2-1-10');
Hope you can help me.
This is a nice problem, this could be solved by this observation:
let's consider two elements in the array x and y, so assume that for the two numbers created by appending these two elements: xy > yx => in the final result, x will always be in front of y, otherwise, we could easily create a larger number by swapping the position of x and y in the result.
=> We could simply create a custom sort based on this observation, when compare two number x and y.
Give a try with below code if it solve your problem...
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$text='';
foreach($array as $arr){
$text.=$arr;
}
$string = str_split($text, "1");
$new_text = implode(",", $string);
$output=explode(",",$new_text);
rsort($output);
$final = '';
foreach($output as $out){
$final.=$out;
}
echo $final;
Try sorting the array in descending order with something like arsort() and concatinate the Array-elements to a string. This should give you the highest possible number.
Try this
$arrnew = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
arsort($arrnew);
$str = str_replace(',','',join(',',$arrnew));
$arr1 = str_split($str);
arsort($arr1);
print_r($arr1);
echo implode('',$arr1);
Demo
I have 2 arrays to compare and find if there is at least a single value in common.
This works just fine:
$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(2, 3, 4, 5, 6);
if (array_intersect($arr1, $arr2)) {
// good, at least one match found
}
However, the question is performance. It doesn't make sense to continue looping thru the arrays after the first match was found. Is there a native PHP function or a useful snippet to achieve this?
Will a combination of foreach() and in_array() do the trick?
How about this?
foreach ($arr1 as $key => $val) {
if (in_array($val, $arr2)){
// do something, maybe return so you wouldn't need break
break;
}
}
Just compare the first value?
$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(2, 3, 4, 5, 6);
if (array_intersect($arr1, $arr2)[0]) {
// good, at least one match found
}
I have an array let say $array = array(2, 1, 8, 3, 6, 0, 10, 10)and I want to get second largest value of that array.
Which sorting/searching technique will be best & how could I use it?
I'd just remove the duplicates from your array (using array_unique) and then use rsort (which uses Quicksort) with the SORT_NUMERIC flag to sort numerically from highest to lowest:
$array = array(2, 1, 8, 3, 6, 0, 10, 10);
$unique_array = array_unique($array);
rsort($unique_array, SORT_NUMERIC);
$second_highest = $unique_array[1]; // will be 8
The PHP manual has a comparison of sorting techniques.
Something like this?
$array = array(2, 1, 8, 3, 6, 0, 10, 10);
rsort($array);
echo $array[1]; // 10
this reverse sorts the array and then outputs the second element.
Edit: if you want the second highest unique value, just add an array_unique call:
$array = array(2, 1, 8, 3, 6, 0, 10, 10);
$array = array_unique($array);
rsort($array);
echo $array[1]; // 8
you could use rsort, but it will do a lot of additional operations which are not needed (quick sort has O(logn) complexity).
It might be a lot faster if you pass the array only in one pass like this (O(n)):
$largest=$array[0];
$secondLargest=null; //none present, by default
foreach($array as $value)
if($value>$largest)
{
$secondLargest=$largest;
$largest=$value;
}
it will be significantly faster on large arrays, but it is only good when you want the first, second, maybe third largest element. In all other cases it would be more reasonable to sort the array and take the N'th largest element dynamically.