echo count(array("1" => "A", 1 => "B", "C", 2 => "D"));
This outputs 2. I've played around with it and noticed that PHP identifies both string numbers and integers the same when used as keys in an array. Seems like integers are prioritized. Also when I var_dump the array only elements containing the value "B" and "D" are displayed. I understand why "A" is not displayed but why is "C" not in the var_dump?
Your array is basically being built as follows:
Key "1" is an integer-like string, treat as integer 1
Assign "A" to key 1
Assign "B" to key 1 (overwrite "A")
No explicit key, take greatest key so far and add 1 = 2
Assign "C" to key 2
Assign "D" to key 2 (overwrite "C")
Thus your resulting array is array(1=>"B",2=>"D");
It appears as though you cannot mix associative arrays and non-associative arrays together. If you add an index to C like you did for everything else, it works as expected. As for strings, if they are a valid integer then it will be casted to an int arrays.
$arr = array("0" => "A", 1 => "B", 2 => "C", 3 => "D");
// However, if you do:
$array = array(
"0" => "A",
1 => "B",
"2" => array(1, 2, 3)
);
It shows up like you would expect in a var_dump array(3) { [0]=> string(1) "A" [1]=> string(1) "B" [2]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } }
Related
I have two array of objects:
Array1:
[[0] => stdClass, [1] => stdClass, [2] => stdClass]
Array2
[[0] => stdClass, [1] => stdClass
I want to get the difference between two arrays (Array1-Array2).
Does it exist a better way instead of iterating the two arrays and checking the properties of the objects?
Thanks a lot
You can use classical array_diff, but need to process strings, not objects in arrays that are compared.
So you can map these arrays to JSON strings and them revert this transformation
<?php
$a = [(object)array('a' => 'b'), (object)array('c' => 'd'), (object)array('e' => 'f')];
$b = [(object)array('a' => 'b'), (object)array('g' => 'h')];
var_dump(array_map('json_decode', array_diff(array_map('json_encode', $a), array_map('json_encode', $b))));
This code will print:
array(2) {
[1]=>
object(stdClass)#6 (1) {
["c"]=>
string(1) "d"
}
[2]=>
object(stdClass)#7 (1) {
["e"]=>
string(1) "f"
}
}
So these are elements that exist in $a and absent in $b.
A similar problem is considered under the link:
array_diff() with multidimensional arrays
I am simply trying to turn this:
Array
(
[0] => 20200330
[1] => 20200329
[2] => 20200328
)
Into this and I am having an extremely hard time
Array
(
20200330,
0200329,
20200328,
)
All arrays in PHP have a unique key for each value within that array.
By default they are 0, 1, 2, 3, etc unless you explicitly set them (e.g. $a = ['key' => 1234];).
It is possible to "remove" keys (set to default without impacting the order) through the use of the array_values() function:
$a = ['a' => 123, 'b' => 321];
$a = array_values($a);
print_r($a); // [0 => 123, 1 => 321]
But it is not possible to entirely remove the keys from an array.
Arrays are by default associated with numbers starting from 0
<?php
$arr=array("String1","String2","Something else");
var_dump($arr);
?>
Output will be:
array(3) {
[0]=>
string(7) "String1"
[1]=>
string(7) "String2"
[2]=>
string(14) "Something else"
}
So if you want to access element of array you type $arr[index] and index is number by default
Here's the idea: a user enters his ZIP code.
Based on the inserted ZIP code, I get an array of ZIP codes (distance ordered).
Next I want to order an existing array of ZIP codes based on the distance ordered array.
So basically I have two arrays:
Array which should be ordered
array(2) {
[0]=>
string(4) "2018"
[1]=>
string(4) "2500"
}
Distance ordered array
array(247) {
[0]=>
string(4) "2000"
[1]=>
string(4) "2500"
[2]=>
string(4) "2050"
[2]=>
string(4) "2018"
In this example, my array (number 1) should be ordered like so: [0] => 2500, [1] => 2018
How can I manage this?
You could use array_intersect() to get only the values of the second array that are also in the first array. And as the function preserves the keys - and so the order -, you only have to renumber them.
$a1=array(2018,2500);
$a2=array(2000,2500,2050,2018);
$a3=array_intersect( $a2 , $a1 );
echo print_r($a3,true);
Result:
Array (
[1] => 2500
[3] => 2018 )
Hi I have a PHP array
$arr = array(10,2,3,0=>4,5,6);
echo "<pre>";
var_dump($arr);
my Expected result was
<pre>array(6) {
[0]=>
int(4)
[1]=>
int(10)
[2]=>
int(2)
[3]=>
int(3)
[4]=>
int(5)
[5]=>
int(6)
}
But my result is
<pre>array(5) {
[0]=>
int(4)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(5)
[4]=>
int(6)
}
From the documentation it says
http://php.net/manual/en/language.types.array.php
Example #5 Keys not on all elements
<?php
$array = array(
"a",
"b",
6 => "c",
"d",
);
var_dump($array);
?>
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[6]=>
string(1) "c"
[7]=>
string(1) "d"
}
**here I can understand that no other element have the key 6 . and in my case i think because the first element also have the key 0 . it get removed .
But because we have added key 0 to another element externally , i feel that 10 should have key 1 . please explain this . thank you very much .
The example you cite does not match your actual use case. In it you are explicitly setting an array key which is not yet defined. In your example you are defining an array key which has already been defined. Thus in your situation you are overriding the original value with the new value you have set later in your array declaration.
I think that the answer to your problem is in example 2 on that page.
<?php
$array = array(
1 => "a",
"1" => "b",
1.5 => "c",
true => "d",
);
var_dump($array);
?>
The above example will output:
array(1) {
[1]=>
string(1) "d"
}
i.e. later entries in an array creation will over-write earlier entries. Because in the array you created the first element "10" is assigned the key 0, the later entry 0=>4 overwrites it. Therefore what you are seeing is the expected result from array creation in PHP.
The problem here is , You are defining the value for a key which is already assigned to a value. Let me explain it a bit.
When the PHP interpreter runs through the array, It assigns
[0] -> 10
[1] -> 2
[2] -> 3
then it sees [0] -> 4 ,
So it replace the [0] -> 10 with [0] -> 4
So on..
[3] -> 5
[4] -> 6
Hope you will understand.
Why PHP treating "1" as integer in array_merge()
ex.
$arr1 = array( "1"=>1, 2 , 3 );
$arr2 = array( "1"=>1, 2 );
print_r(array_merge( $arr1 , $arr2 ));
var_dump("1");
var_dump(1);
Outputs:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 1
[4] => 2
)
string(1) "1" int(1)
As per array_merge() function :-
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
This is not array_merge() related, but rather on how array keys are handled by PHP. See http://php.net/manual/en/language.types.array.php for implicit cast of keys.
You simply shouldn't use numeric values as string keys because they are treated as numerical keys.
The most important thing in this case is:
Strings containing valid integers will be cast to the integer type.
E.g. the key "8" will actually be stored under 8. On the other hand
"08" will not be cast, as it isn't a valid decimal integer.
This above is related to PHP arrays as in PHP Arrays manual
You can see it using this sample code:
$arr1 = array( "1"=>1, "01" => 4, 2 , 3 );
var_dump($arr1);
It will return:
array(4) { 1=> int(1) ["01"]=> int(4) [2]=> int(2) [3]=> int(3) }
So your key "1" became numerical key but key "01" is still string key