This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
is there any way to get common values between two arrays in php?
I am trying to compare 2 arrays and keep the elements based on the second array.
I have
array 1
array('3' => 'test1', '4' => 'test2', '1' =>'test3')
array 2
array('2' =>'test2', '3' =>'test3')
I want to compare array 1 and array 2 and keep the test 2 and test 3 in array 1 in my case.
So the end result will be
array('4' => 'test2', '1' =>'test3')
I have tried array_diff but it doesn't come out the results I wanted. I also google for a while but coudln't find anything useful either.
Are there anyways to get what I need? Thanks a lot!
You were close, array_intersect() is the function you need.
I suspect the function you really want is array_diff_key():
$diff = array_diff_key($array1, $array2);
(demo on codepad.org)
Related
This question already has answers here:
Are PHP Associative Arrays ordered?
(4 answers)
Closed 8 years ago.
I'm learning PHP and I've got a question that's bothering me. PHP arrays seem to be hashmaps internally. If you give an array a key and value, it almost certainly has to put the key through some sort of hashing function before placing it in an actual array, right? Why then, if I give an array a series of keys and values and then dump these to screen, does PHP maintain the order in which I entered the values?
for instance:
$arr = array();
$arr[1] = 'one';
$arr[3] = 'three';
$arr[2] = 'two';
foreach($arr as $key => $val)
echo "$key => $val<br>"
would render "1 => one, 2 => two, 3 => three" in a typical hashmap, but instead I get "1 => one, 3 => three, 2 => two." Which to me means that there have to be both and order and a key being maintained in whatever datatype this actually is.
Thanks in advance for any explanation.
You are correct about the array being stored as a hash table or ordered map. Basically, everything in PHP is a hash table.
See here: Understanding PHP's internal array implementation
This question already has answers here:
How to get last key in an array?
(18 answers)
Closed 8 years ago.
As I read though how to get the last value of multidimensional array, end(array) has come up multiples times.
My problem is similar, I have an array like this:
array = (
[12] => Array (xxx => xxx),
[34] => Array (xxx => xxx),
[56] => Array (yyy => yyy)
);
I want to get the index number. If I use end(array) I will get the whole array indexed from [56]. How do I get [56] itself instead of the array?
P.S. I know I can use loop to get the last index number, I just don't want to loop though the whole array to just get the last index number...
$keys = array_keys($yourArray);
$lastKey = $keys[count($keys)-1];
So, get the keys and pick the last one, does this suit you?
I wouldn't recommend this on very large arrays though, if you are doing an iterative operation. I believe the array_keys actually loops the array internally (confirm me on this please).
Alternatively, as #Ghost mentioned in a comment, you can point the array to end with end() and use key() on it to get the key (this is more performant):
end($yourArray);
$lastKey = key($yourArray);
This question already has answers here:
Characters allowed in php array keys?
(11 answers)
Closed 8 years ago.
I am studying PHP and came across a question like this:
What is the output of the following array?
Code:
$a = array(0.001 => 'b', .1 => 'c');
print_r($a);
The answer is 0 => 'c' - now I know array keys can't be numbers but wouldn't that throw an error? Why is the first element overwritten?
From the documentation on arrays:
Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
and, as Alex points out below:
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.
This question already has answers here:
comparing arrays in php, without caring for the order
(6 answers)
Closed 9 years ago.
I'm trying solving a problem where i need to check if the arrays are same no matter how they are sorted i cannot use sorting because it add extra over head to time this function is taking in answering.
I am currently using array_diff_assoc
$arr1 = array(1,2,3);
$arr2 = array(3,2,1);
$result = array_diff_assoc($arr1,$arr2);
print_r($result);
Array
(
[0] => 1
[2] => 3
)
But the above arrays are same!! The human way.
Any idea for comparing two arrays.
Well interpreter is not human right ? ;)
Even if you do a simple var_dump($arr1==$arr2) on your existing array, it will return false.
This below code returns true !
$arr1 = array(1,2,3);
$arr2 = array(2=>3,1=>2,0=>1);//position is same as yours., i've just set a key
var_dump($arr1==$arr2); //true
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP Arrays: A good way to check if an array is associative or sequential?
Hello :)
I was wondering what is the shortest (best) way to check if an array is
a list:
array('a', 'b', 'c')
or it's an associative array:
array('a' => 'b', 'c' => 'd')
fyi: I need this to make a custom json_encode function
function is_assoc($array){
return array_values($array)!==$array;
}
Note that it will also return TRUE if array is indexed but contains holes or doesn't start with 0, or keys aren't ordered. I usually prefer using this function because it gives best possible performance.
As an alternative for these cases I prefer this (just keep in mind that it's almost 4 times slower than above):
function is_assoc($array){
return !ctype_digit( implode('', array_keys($array)) );
}
Using ksort() as Rinuwise commented is a bit slower.