How does one iterate in reverse over php associative array? https://stackoverflow.com/a/10777617/1032531 gives solutions for a non-associated array.
My attempt:
$a=['5'=>'five','3'=>'three','7'=>'seven'];
var_dump($a);
foreach($a as $k=>$v){echo("$k $v\n");}
$a=array_reverse($a);
var_dump($a);
foreach($a as $k=>$v){echo("$k $v\n");}
produces the following results:
array(3) {
[5]=>
string(4) "five"
[3]=>
string(5) "three"
[7]=>
string(5) "seven"
}
5 five
3 three
7 seven
array(3) {
[0]=>
string(5) "seven"
[1]=>
string(5) "three"
[2]=>
string(4) "five"
}
0 seven
1 three
2 five
I wish to preserve the keys, and return:
array(3) {
[5]=>
string(4) "five"
[3]=>
string(5) "three"
[7]=>
string(5) "seven"
}
5 five
3 three
7 seven
array(3) {
[7]=>
string(5) "seven"
[3]=>
string(5) "three"
[5]=>
string(4) "five"
}
7 seven
3 three
5 five
Just use $a=array_reverse($a,true); instead of $a=array_reverse($a); for keep key.
array_reverse() have a second optional parameter for preserve keys. default value is false.
Read doc here
You were very close - you had all the key words already - and just need to remember that the PHP manual is your friend :)
The manual page for array_reverse lists an optional argument $preserve_keys, which defaults to false.
So you just need to change $a=array_reverse($a); to $a=array_reverse($a, true);, and you should get the result you were after.
Related
I've got multiple arrays, and want to remove the duplicates. So I only got the unique items.
array(4) {
[0]=>
string(14) "Bergen op Zoom"
[1]=>
string(9) "jan steen"
[2]=>
string(7) "culture"
[3]=>
string(11) "Netherlands"
}
array(8) {
[0]=>
string(14) "fasion"
[1]=>
string(9) "conceptial"
[2]=>
string(7) "industrial"
[3]=>
string(11) "Netherlands"
}
I want to print all the strings out of the array except for the last Netherlands because it's already printed.
I've tried it with array_unique() but it only does that if there are duplicates in the array itself.
no clue how to get this thing working..
Use array_diff. It will stay in arr1 only items that not in arr2
array_diff($arr1, $arr2);
Try this
array_unique( array_merge($array1, $array2) );
I have two arrays something like this
1) array(3) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(3) "med" }
2) array(4) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(4) "other" [3]=> string(3) "med" }
now i want to merge and delete the double entries of the two arrays, important thing is here to keep the order of the first array in the final array (max,min,med -> from first array, and then all others from second array)
the two arrays have different lengths array(3) and array(4)
$myfinalarray = (array_unique(array_merge($arr_first, $arr_last)));
the problem is the order is lost
result:
array(4) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(4) "other" [3]=> string(3) "med" }
what i need is this
array(4) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(3) "med" [3]=> string(4) "other" }
You should erase the double entries from 2nd array first:
$array_last = array_diff($arr_last, $arr_first);
and then
$myfinalarray = (array_unique(array_merge($arr_first, $arr_last)));
Order is not lost, but if you want to sort array in alphabetical order use sort function
$myfinalarray=sort($myfinalarray);
Check the below code,
<?php
$arr1=array(0 => "max",1 => "min",2 => "med");
$arr3=array(0 => "max",1 => "min",2 => "other",3 =>"med");
$myfinalarray = (array_unique(array_merge($arr1, $arr3)));
print_r($myfinalarray);
?>
the output willbe like,
Array
(
[0] => max
[1] => min
[2] => med
[5] => other
)
check here
I have the following Array data:
array(1) {
[0]=> array(8) {
[0]=> string(2) "55"
[1]=> string(1) "2"
[2]=> string(1) "1"
[3]=> string(1) "3"
[4]=> string(1) "4"
[5]=> string(1) "5"
[6]=> string(1) "6"
[7]=> string(1) "7"
}
}
I'd like to be able to bring back the values, individually for [1] and [2] for examples, who's values are 2 and 1 respectively.
$matches[0][1] - cannot believe I actually asked this question.
In a multidimensional array, you need to reference the nested array, and then the nested array element.
For example, if your array was set in $array, you would reference it as:
$array[0][1];
Which is basically saying 'Value of 2nd element of 1st nested array'.
I have a $times array, which contains:
array(8) {
[0]=>
string(5) "10:00"
[1]=>
string(5) "13:00"
[2]=>
string(5) "10:00"
[3]=>
string(5) "11:00"
[4]=>
string(5) "12:00"
[5]=>
string(5) "13:00"
[6]=>
string(5) "14:00"
[7]=>
string(5) "15:00"
}
How can I a) sort it so it starts with lowest value b) only have one entry of each time? (no duplicates, currently theres two 10:00s and 13:00s etc)
Why not just use PHP's inbuilt functions:
$input = array_unique($input);
sort($input);
print_r($input);
Well, you could always use the php-shipped standard functions for arrays:
Array Unique
and sort()
$array = array_unique(sort($a));
$GLOBALS["items"] = array('one', 'two', 'three', 'four', 'five' ,'six', 'seven');
$alter = &$GLOBALS["items"]; // Comment this line
foreach($GLOBALS["items"] as $item) {
echo get_item_id();
}
function get_item_id(){
var_dump(key($GLOBALS["items"]));
}
Check output of this code, with commented and uncommented second line.
My result(PHP 5.3.0).
With second line
int(1) int(2) int(3) int(4) int(5) int(6) NULL
Without second line:
int(1) int(1) int(1) int(1) int(1) int(1) int(1)
Why so strange result?
Here is a possible explanation:
We know that foreach always loops over a copy of the array if it is not referenced:
Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer.
That means that the internal pointer of the original array is not changed and key() will always return the same value (as we can see when we comment out the line). And indeed if we do a var_dump($GLOBALS), we get:
["items"]=>
array(7) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
[2]=>
string(5) "three"
[3]=>
string(4) "four"
[4]=>
string(4) "five"
[5]=>
string(3) "six"
[6]=>
string(5) "seven"
}
(no reference)
But as soon as we generate a reference to the array (with $alter), $GLOBALS['items'] becomes a reference too, because both entries have to point to the same array:
["items"]=>
&array(7) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
[2]=>
string(5) "three"
[3]=>
string(4) "four"
[4]=>
string(4) "five"
[5]=>
string(3) "six"
[6]=>
string(5) "seven"
}
["alter"]=>
&array(7) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
[2]=>
string(5) "three"
[3]=>
string(4) "four"
[4]=>
string(4) "five"
[5]=>
string(3) "six"
[6]=>
string(5) "seven"
}
Hence, the foreach loop does iterate over the original array and changes the internal pointer, which affects key().
To sum up: It is a problem with references, not with $GLOBALS.