Is there a way to only keep the first N (for example 10) elements of an array? I know there is array_pop, but is there a better, more elegant way?
You can use array_slice or array_splice:
$b = array_slice($a, 0, 10);
$c = array_splice($a, 0, 10);
Note that array_slice copies the items of $a and returns them while array_splice does modify $a itself and only returns the items that have been removed from $a.
Related
Why does the array_reduce() method work differently when adding and multiplying? When I add the array values below, the code produces the expected result: 15. But when I multiply, it returns: 0. Same code... The only difference is that the + sign is switched for the * sign.
function sum($arr){
print_r(array_reduce($arr, function($a, $b){return $a + $b;}));
}
function multiply($arr){
print_r(array_reduce($arr, function($a, $b){return $a * $b;}));
}
sum(array(1, 2, 3, 4, 5)); // 15
multiply(array(1, 2, 3, 4, 5)); // 0
According to documentation, you might wanna try
function multiply($arr){
print_r(array_reduce($arr, function($a, $b){return $a * $b;},1));
}
Here is a quote from this discussion:
The first parameter to the callback is an accumulator where the result-in-progress is effectively assembled. If you supply an $initial value the accumulator starts out with that value, otherwise it starts out null.
For example, we have array $A with 11 elements (no matter if is odd number, then it can be 5 elements in $B and 6 elements in $C, or 8 elements in $C and 3 elements in $C). The result must satisfy rules from the title
$A = array(1, 2, 5, 7, 12, 11, 9, 13, 10, 3, 2)
$B = array(....)
$C = array(....)
that satisfy (sum of $B elements) - (sum of $C elements) = minimum number (0 if possible)
Sort the numbers in $A, then start moving them into $B and $C from the front or end of $A as needed, keeping them sorted as well. When done, you can do binary search and shift items back and forth as needed to balance. I'll add a code example if you'd like
I have 2 arrays like those:
$a = array(152,32,113,47,53);
$b = array("a","w","lk","qw","ol");
I will sort $a by using asort() and I want to auto-sort the second array $b likewise $a.
How to do I?
You can use array_multisort.
http://us2.php.net/manual/en/function.array-multisort.php
I have 2 variables each containing a number (integer). I would like to sort them to have the lowest number first and the second largest. For example:
$sortedVar = getSmaller(45, 62); // Will return 45
$sortedVar = getSmaller(87, 23); // Will return 23
Do you see what I want to do? Can you help me please?
Thanks :)
http://php.net/manual/en/function.min.php
min — Find lowest value..
If the first and only parameter is an array, min() returns the lowest value in that array. If at least two parameters are provided, min() returns the smallest of these values.
Note:
Values of different types will be compared using the standard comparison rules. For instance, a non-numeric string will be compared to an integer as though it were 0, but multiple non-numeric string values will be compared alphanumerically. The actual value returned will be of the original type with no conversion applied.
Caution
Be careful when passing arguments with mixed types values because min() can produce unpredictable results...
Use min() which supports any number of arguments as well as arrays.
$smallest = min(1,2); //returns 1
$smallest = min(4,3,2); //returns 2
$smallest = min(array(5,4)) //returns 4
function getSmaller($a, $b) {
return $a < $b ? $a : $b;
}
In plain english, if $a is smaller than $b, then return $a, else return $b.
Or as others pointed out, there's also a function for that, called min().
$sortedVar = $a < $b ? $a : $b;
Code:
$arr = array( 10, 20, 30 );
$arr1 = array(
1=>30,
2=>20,
0=>10
);
var_dump( $arr == $arr1 );
$a = array( 1, 2, 3);
$b = array(
1=>2,
2=>3,
0=>1
);
var_dump($a == $b);
This outputs:
bool(false)
bool(true)
Two arrays will be considered equal if their corresponding values are the same.
In your first example you are comparing two arrays:
[10, 20, 30]
[10, 30, 20]
Obviously these are not the same, so it returns false. The second example though:
[1, 2, 3]
[1, 2, 3]
...are the same. Am I missing something here?
If you want to test if two arrays have the same members, see this question:
Algorithm to tell if two arrays have identical members
If you just want to see they have the same totals, you can use array_sum
If you do not specify keys for array, php automatically selects numbers, starting with 0.
Therefore, the following pairs of lines mean the same:
$arr = array(10,20,30);
$arr = array(0=>10,1=>20,2=>30);
$a = array(1,2,3);
$a = array(0=>1,1=>2,2=>3);
It's to be expected, that
array(10,20,30) != array(10,30,20)
and
array(1,2,3) == array(1,2,3)
Given some of your comments, I think it would be useful if you read up on array type in php. Mainly the fact that there is no key-less arrays.
And don't forget comparison operators as well.
Thats a very broad question, but in the case of an array, it compares on an index-by-index basis.
In your first block of code, $arr is not equal to $arr1 because 30 != 10 and 10!=30.
In the second block of code, you are specifying that at index 0, the value is 1. At index 1, the value is 2, and at index 2, the value is 3. Thus, you have the same array.
th equality operator is === in php and within an array => is correct. also $arr !=$arr1 coz 20 !=30, 30!=20 as per you have assigned.