Is it more efficient to use the key intersect or the value intersect if both key and value have the same contents for example:
Array
(
[743] => 743
[744] => 744
[745] => 745
[746] => 746
[747] => 747
[748] => 748
)
Is there any difference in performance in using one or the other with the same values. Similar to the difference of using double or single quotes?
From another post: I have two unordered integer arrays, and i need to know how many integers these arrays have in common
Depending on your data (size) you
might want to use
array_intersect_key() instead of
array_intersect(). Apparently the
implementation of array_intersect
(testing php 5.3) does not use any
optimization/caching/whatsoever but
loops through the array and compares
the values one by one for each element
in array A. The hashtable lookup is
incredibly faster than that.
Related
Say, we make an array like this:
$arr = Array
(
2 => 'c',
1 => 'b',
0 => 'a'
);
When you pass it to array_pop():
array_pop($arr);
And the "last" element would be poped off, which has the index of zero!!
print_r($arr);
Result:
Array
(
[2] => c
[1] => b
)
So, what's the purpose of index?
Isn't it just a different way of saying "numeric keys of associative arrays"?
Is it only PHP dose so, or all the languages treat arrays like this?
Not all languages do this, but PHP does, because PHP is a little weird. It implements arrays more or less like dictionaries. PHP does offer some functions like ksort though, which let you sort the array by key.
And that's what this is: a key. An array has indexes as well, so what you got, is an array where item 2 has key 0. And that's where the confusion starts continues.
PHP: a fractal of bad design has a whole chapter about arrays. Interesting reading material. :)
The reason for this behavior is because arrays in PHP are actually unordered maps.
Because of this, don't think of accessing the arrays in terms of indexes, think of it in terms of keys. Keys can be numbers and they can be strings, but the result is the same; you're still using a map, not a true "array".
Once you accept that fact, you'll understand why PHP includes functions like ksort() for sorting an array by keys and why array_pop() doesn't always remove the highest key value.
It's a PHP thing. Other languages usually provide other structures to provide what is the default behaviour for arrays on PHP. JavaScript for instance will always sort the array:
a = [];
> []
a[1] = 'a';
> "a"
a[2] = 'b';
> "b"
a[0] = 'c';
> "c"
a
> ["c", "a", "b"]
In Java you would need to use a Hash Map or something else to do Associative Arrays. PHP handles data structures more loosely than other languages.
The index allows you to identify and access the elements of the array.
the reason is simple HashTables.
in php internal functions often use HashTables. basically an array is some data in memory and like in C - an array index can only hold integer values but not in php.
php solves this with hashtables. if you asign a index example foo this value is not directly assigned as foo it gets hashed and maybe end internal as 000000000111 and other hash functions.
so php doesn't work directly with your assigned value and this is the reason why you can set an array index like 0 as last index element. internal php work with hashtables that have a "list" with values which index value is assigned to which position in the array.
I have 2 arrays, I need to merge them into a single but using array_merge values fray and the final array incorrect. I try $array1+$array2, but the same thing happens.
And at the moment I have no idea how to fix it.
UPDATE: in my case array_merge and array_merge_recursive return the same result (data from the last of the array overwrites the rest)
UPDATE2: my fail for those arrays work well, I applied to other. Thanks problem solved (after Stijn I realized that it is better to merge arrays first and then apply my transformation)
Array
(
[0] => Array
(
[DOC_DATE] => 20.09.13
[LINK] => �������
[QUERY_ID] =>
[DOC] => 35428
[DOC_TYPE] =>
)
[1] => Array
(
[DOC_DATE] => 20.09.13
[LINK] => �������
[QUERY_ID] =>
[DOC] => 35428
[DOC_TYPE] =>
)
You'll want array_merge_recursive instead.
array_merge_recursive() merges the elements of one or more arrays together so that the values of one are appended to the end of the
previous one. It returns the resulting array.
If the input arrays have the same string keys, then the values for
these keys are merged together into an array, and this is done
recursively, so that if one of the values is an array itself, the
function will merge it with a corresponding entry in another array
too. If, however, the arrays have the same numeric key, the later
value will not overwrite the original value, but will be appended.
I think you are looking for array_merge_recursive
Just use array_merge_recursive($array1, $array2)
I can't see to find an answer for this, but it's been bugging me for a while.
When using multidimensional arrays in php (or any language for that matter), do you get any efficiency and productivity out of saving an array of a multidimensional array as its separate array or accessing that "array" element directly from the whole array?
For example,
print_r($myArray);
Array
(
[2] => Array
(
[7.0] => Array
(
[0] => 1
[1] => 23
)
[16.0] => Array
(
[0] => 4
[1] => 28
)
)
[5] => Array
(
[17.0] => Array
(
[0] => 1
[1] => 3
)
)
)
If I needed to REPEATABLY access the array of [16.0], would it be better to just save that entry as its own array until I don't need it anymore, or is it better just to access it directly?
Option 1:
$tempArray=$myArray[2]["16.0"];
echo "Index=".$tempArray[0].";
or
Option 2:
echo "Index=".$myArray[2]["16.0"][0];
Of course this is just a tiny example, but if the values in (arbitrary) $array[.][.][n][...] are being accessed more than once, and the value of n depends on the index of a loop, is there any difference between accessing the element directly or saving that array (deep down on the layers of the array) as its own array and accessing its values that way?
If you look at the bytecode generated for these statements:
$tempArray=$myArray[2]["16.0"];
echo $tempArray[0];
FETCH_DIM_R $3 $myArray, 2
FETCH_DIM_R $4 $3, '16.0'
ASSIGN $tempArray, $4
FETCH_DIM_R $6 $tempArray, 0
ECHO $6
echo $myArray[2]["16.0"][0];
FETCH_DIM_R $7 $myArray, 2
FETCH_DIM_R $8 $7, '16.0'
FETCH_DIM_R $9 $8, 0
ECHO $9
(I've reformatted and removed the EXT_STMT markers). You can see that the only difference is the actual assignment to $tempArray. You might think that an array assignment is expensive. However PHP uses a referencing model and does a lazy copy-on-write, so it is not, and the cost is the pretty much same whatever the array size.
(Except of course if you change elements after the assignemt, so $tempArray is no longer the same as its originating slice, and at that point the memory usage jumps as the assign triggers the clone of the slice as the references need to be split.)
OK, this approach might be worthwhile if you are doing a lot of localised readonly access to a slice to save the repeated FETCH_DIM_R lookups (the PHP compile does absolutely no local optimisation of repeated use of indexes). However, the opportunities to shoot yourself in the foot on update are significant.
Why not benchmark this yourself using a loop and microtime() and memory_get_usage()?
You can easily test this yourself by creating an immense multidimensional array with a for loop, but accessing it directly will generally be faster when indexing large arrays.
With smaller arrays (~500 items or less) it isn't going to make a noticeable difference.
I have an array like
array (
(1) => array => ([1]=aa, [2]=other keys and values),
(2) => array => ([1]=bb, [2]=other keys and values),
(3) => array => ([1]=cc, [2]=other keys and values),
(4) => array => ([1]=aa, [2]=other keys and values),
(5) => array => ([1]=bb, [2]=other keys and values),
(6) => array => ([1]=cc, [2]=other keys and values),
(7) => array => ([1]=bb, [2]=other keys and values)
)
I would like to make arrays based on [1] = aa/bb/cc/dd.
To divide the original array in to various unique arrays which have same value of [1] key.
I do not want to use foreach as the result set is expected to reach 10k rows.
Is this optimal at all??
No matter what solution you choose, you will have to loop over all of the rows in your array, no matter what. Either directly or indirectly. There is no magic code that can decide what's inside a sealed box without opening the box.
So, apart from the obvious foreach you can use array_walk
Is this optimal at all??
Definitely not.
Though it is not foreach to blame but a programmer who have 10k rows to loop over.
If it's regular web-page served on user's request, there shouldn't be 10k rows by any means. You have to reduce the number at least by factor of 100. Or let the data storage to do the necessary calculations.
When declaring an Array in PHP, the index's may be created out of order...I.e
Array[1] = 1
Array[19] = 2
Array[4] = 3
My question. In creating an array like this, is the length 19 with nulls in between? If I attempted to get Array[3] would it come as undefined or throw an error? Also, how does this affect memory. Would the memory of 3 index's be taken up or 19?
Also currently a developer wrote a script with 3 arrays FailedUpdates[] FailedDeletes[] FailedInserts[]
Is it more efficient to do it this way, or do it in the case of an associative array controlling several sub arrays
"Failures" array(){
["Updates"] => array(){
[0] => 12
[1] => 41
}
["Deletes"] => array(){
[0] => 122
[1] => 414
[1] => 43
}
["Inserts"] => array(){
[0] => 12
}
}
Memory effiency isn't really something you need to worry about in PHP unless you're dealing with really huge arrays / huge numbers of variables.
An array in PHP isn't really like an array in C++ or a similar lower-level language; an array in PHP is a map. You have a list of keys (which must be unique and all of type string or integer), and a list of values corresponding to the keys. So the following is a legal array:
array(0 => 'butt', 1 => 'potato', 2 => 'tulip')
but so is
array(5 => 'i', 'barry' => 6, 19 => array(-1 => array(), 7 => 'smock'))
In both cases there are 3 entries in the array, hence 3 keys and 3 values.
In addition to the keys and values in the array, one array may be distinguished from another by the order in which the key/value pairs occur. If you define an array so that it has nonnegative integers as keys, this will often be the expected order. The order matters when you use constructs like foreach().
array[3] will be undefined/unset but not causing an error, and the array will use only memory for that 3 values - php isn't like C where you have to look at those things.
Accessing $arr[3] gives a notice: Notice: Undefined offset: 3 in /data/home/sjoerd/public_html/svnreps/test/a.php on line 3. You can avoid this by checking with isset() or array_key_exists().
There are no nulls stored.
Having empty elements won't take up extra memory.
Whether you should use multiple variables or an array depends on the context and how you use the variables.