Compare two arrays without sort them [duplicate] - php

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

Related

Turning PHP key=>value into sub array: [key,value] [duplicate]

This question already has answers here:
reformat an php array [duplicate]
(2 answers)
Closed 10 months ago.
I'm having following data:
Array ( [one] => this [two] => this2 )
Which I want to convert to json type which looks like:
data:{[one,this],[two,this2]}
How can I get through this in effecient manner?
Edit:
I've tried a lot of things This is actual data which I need to make datatable compatible:
{"draw":0,"recordsTotal":null,"recordsFiltered":null,"data":‌​[[{"first":[""],"sec‌​ond":[""],"third":["‌​"],"fourth":[""],"fi‌​fth":[""],"sixth":["‌​value"]}]]}
as the data here is in key=>value form json is not compatible for datatables (PHP)
You can use array_map and array_keys:
$result = array_map(null, array_keys($array), $array);
$json = json_encode($result);
Here is working demo.

How do PHP arrays actually work? [duplicate]

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

Understanding array output [duplicate]

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.

Php Unset All Keys [duplicate]

This question already has answers here:
For cleared or unset php arrays, are elements garbage collected?
(3 answers)
Closed 8 years ago.
My question might seems basic but still, can't figure how to works this out.
Consider an array of my favorite fruits
$array = array("Banana","Rasberry","Blackberry")
I'm looking to clear this array so that all keys and values would be erased. My array would be empty just like if I had wrote
$array = array();
Then, I could array_push some new data in.
I thought that I could array_walk($array, unset($array[$key]) but it's not working properly.
Your question includes the best solution for your situation:
$array = array();
This is the fastest way to make the $array variable point to an empty array.

PHP array compare issue [duplicate]

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)

Categories