I like to compare two arrays without using in_array as both of these arrays are extremely large (50,000 plus). I like to generate a new array of all the ones that are missing from the first array.
What would be the fastest most efficient solution I would use?
First Array
Multidimensional array generated from SQL Query
Array (
[0] => Array (
[id] => 17228219
[name] => ...
)
[1] => Array (
[id] => 17228220
[name] => ...
)
[2] => Array (
[id] => 17228221
[name] => ...
)
[3] => Array (
[id] => 17228222
[name] => ...
)
[4] => Array (
[id] => 17228223
[name] => ...
)
[5] => Array (
[id] => 17228224
[name] => ...
)
)
Second Array
Generated from Simple XML
Array (
[0] => SimpleXMLElement Object (
[0] => 17228219
)
[1] => SimpleXMLElement Object (
[0] => 17228221
)
[2] => SimpleXMLElement Object (
[0] => 17228222
)
[3] => SimpleXMLElement Object (
[0] => 17228224
)
)
New Array
Create an array with missing IDs
Array (
[0] => Array (
[id] => 17228220
[name] => ...
)
[1] => Array (
[id] => 17228223
[name] => ...
)
)
you can make it a little faster by implementing an AVL Tree for example, then it will do it in
O(N*Log(N)), you can find many implementations of trees in php
that will be a little faster then the double 'for' (N^2),
also, you can sort the arrays and move every iteration one step on both arrays, this way you can find the difference, but this is also O(N*Log(N)), its hard to believe it can be faster than this.
p.s.
if its already sorted (like in the code you posted), then you can do it in O(N) with the second way
From the algorithm-point of view the fastest would be item-wise (mergesort like) compare and complement detection by one pass with two sorted arrays... with time complexity O(N logN) + O(MlogM) + O(M + N) ~
O(N log N)...
AVL Tree is an overkill...
Using the 'id' as the array key for both sets would make for a much faster PHP based algorithm as V-X suggests.
However, the most efficient solution is to leave your reference set in the database and add the XML records to it, detecting collisions / non-collisions either on insert or on a subsequent SELECT with join, particularly if the reference set is larger than the comparison set.
You don't say what you intend doing with the un-matched data - which has some bearing on the approach.
Related
I'm pulling data from an XML file, converting it to json and then an array to be able to handle it.
The issue is because of the structure of the XML, it's like inception. Arrays inside of arrays inside of more arrays.
Array
(
[0] => ...
[3] => Array
(
[records] => Array
(
[record] => Array
(
[id] => 506
[sequenceNumber] => 1
[values] => Array
(
[value] => Array
(
[0] => Array
(
[picklistOptionId] => -1
[refId] => 230
[value] => **VALUE**
)
...
I want to be able to call the "**VALUE**" by the refId of 230. What would be the best way?
$arr[3]['records']['record']['values']['value'][0]['value']
There are downsides to having arrays of this many dimensions. e.g., they are less maintainable and (in some cases) slower.
I don't know what is going on (maybe it because it is the 12th consecutive hour working).
I always used this notation but now it just doesn't work (I already checked several time that the data is passed to this template I'm talking about).
Data is not iterated and the result is empty.
Mustache code:
{{#ad}}
setInput("{{key}}", "{{value}}");
{{/ad}}
I tried also:
{{#ad}}
setInput("{{key}}", "{{value}}");
{{/ad}}
The data passed is the following:
Array
(
[ad] => Array
(
[0] => Array
(
[key] => id
[value] => 1
)
[1] => Array
(
[key] => created_on
[value] => 1371464401
)
[2] => Array
(
[key] => updated_on
[value] =>
)
[3] => Array
(
[key] => dealer_id
[value] => 1
)
)
)
Solved: be careful to not pass hashes instead of "plain" arrays!! Even if it seemed to be a common array because of the indexing (0 => "a", 1 => "b") it was actually an hash! So just return the malicious data within an array_values($data) to fix it!
I have an array as such
Array
(
[1] => Array
(
[0] => PrettyName1
[1] => PrettyName2
[2] => PrettyName3
)
[2] => Array
(
[0] => UglyURL1
[1] => UglyURL2
[2] => UglyURL3
)
)
I want to be able to put these into an array for a code-igniter template, well when I push and merge arrays I end up messing the whole thing up. Can someone show me the proper way to merge these arrays? I need something like
Array
(
PrettyName1 => UglyURL1
PrettyName2 => UglyURL2
PrettyName3 => UglyURL3
)
Maybe something like this array_combine($ar[1], $ar[2]) ?
I want to sort two dimensional array according to the occurrence of the search string in php. i tried to fetch data according to the relevance of search string . i come across match and against but it will work on the MYISAM but my table is in innodb. so plan to sort the array using any sort function but i cant find out anything.
my search pressure tes string array
Array
(
[0] => pressure
[1] => tes
)
My output array which matches the above string with title is
Array
(
[0] => Array
(
[title] => tests.doc
[link] => http://localhost/test.doc
[snippet] =>
)
[1] => Array
(
[title] => Pressure Testing Your Company
[link] => http://localhost/Pressure_Testing_Your_Companys.pdf
[snippet] => Questions used by the CFO against dimensions critical to success
)
[2] => Array
(
[title] => pressure.doc
[link] => http://localhost/pressure.doc
[snippet] => Templates for services
)
)
In the above array the most relevant array[1] then array[2] and then array[0] should be in this order. i want to sort this array accordingly. my output should be like below:
Array
(
[0] => Array
(
[title] => Pressure Testing Your Company
[link] => http://localhost/Pressure_Testing_Your_Companys.pdf
[snippet] => Questions used by the CFO against dimensions critical to success
)
[1] => Array
(
[title] => pressure.doc
[link] => http://localhost/pressure.doc
[snippet] => Templates for services
)
[2] => Array
(
[title] => tests.doc
[link] => http://localhost/test.doc
[snippet] =>
)
)
please help me!!!!
Look at the example #3, I think that's what you want.
http://php.net/manual/en/function.array-multisort.php
Just tested this, it seems to be working :
$titles = array();
foreach ( $array as $key => $value )
{
$titles[ $key ] = $value['title'];
}
array_multisort( $titles, SORT_ASC , $array );
I would highly recommend to sort your results in your MySQL query, that would be much better performance wise.
I have an array (array01) that contains a bunch of sub arrays consisting of two pieces of data each, like:
Array ( [0] => Array ( [0] => 10CC [1] => Dreadlock Holiday )
[1] => Array ( [0] => 10CC [1] => I\'m Not In Love )
[2] => Array ( [0] => 10CC [1] => Dreadlock Holiday ) )
etc...
I have another array (array02) like:
Array ( [66] => Array ( [0] => 10CC [1] => Dreadlock Holiday )
[585] => Array ( [0] => 10CC [1] => I\'m Not In Love )
etc...
I'm successfully using foreach and then in_array to see what array01 elements are in array02. However, what I am struggling to figure out is how to get the id of what element in array2 the hit was on.
So for example, array01's 0 and 2 elements (both 10CC, Dreadlock Holiday), are matched in array02, but how do I get the ID of the element (in this case, 66)?
Thanks for your help.
Have you tried the array_search function?
look for documentation for the array_intersect() in PHP manual. Note that the original keys are preserved so it returns an array with the elements present on the array02 array with the original keys.
Hope it's what you're looking for
Cheers