PHP ksort unexpected behavior [duplicate] - php

This question already has answers here:
ksort produces wrong result when dealing with alphanumeric characters
(6 answers)
Closed 4 years ago.
Let me tell you from the start: I know about the sort flags of this function. When I use SORT_STRING it works well, but the default flag SORT_REGULAR works weird, or doesn't work at all.
Consider an array like below.
$arr = [
'27a' => 'foo',
'27b' => 'foo',
'27c' => 'foo',
'27' => 'foo',
];
When I try to sort using ksort, it gives an array without being sorted in any obvious logic.
ksort($arr);
print_r($arr);
// this prints
Array
(
[27a] => foo
[27] => foo
[27b] => foo
[27c] => foo
)
As one could say the keys are neither numerically nor alphanumerically nor naturally sorted. Even more strangely, when I change the order of the source array, it gives a different result:
$arr = [
'27a' => 'foo',
'27' => 'foo',
'27b' => 'foo',
'27c' => 'foo',
];
ksort($arr);
print_r($arr);
// this prints
Array
(
[27b] => foo
[27c] => foo
[27] => foo
[27a] => foo
)
Does anyone know the logic behind this? Is this a bug or am I missing something?
EDIT: Thank you all for being interested in and answering my question. Although it's marked as duplicate, the other question didn't mention the weirder part: Why changing the order of the source array changes the result? It should give the same result with the same input set. Shall we discuss this too?

The reason that is happening is because it sees keys like '27a' as a string, and keys like '27' as an integer, even though it is quoted. You'll see the same results if you remove the quotes from the 27 key.
And, as the ksort page says: "Warning: Be careful when sorting arrays with mixed types values because sort() can produce unpredictable results."
Weird behavior for sure- unfortunately the best way to produce expected results when you have keys that look like integers (even if they are strings), is to specify the sort flag like SORT_STRING to ensure that you get the expected results every time.

Related

Check, if array A contains all items from array B, when both arrays are multidimensional

I want to check, if array A contains all the items from array B (may contain others, but must contain all), when both arrays are multidimensional, i.e. can contains different variable types.
I've seen a lot (particularly this, this, this, this, this and this, also this, this and this as well). I've read PHP doc. Everything, that I checked, fails with "Array to string conversion" notice. Especially wen using array_intersect() or array_diff().
I'm using strict error checking, so notices actually holds further execution of entire script and are something, I don't generally like and want to avoid. Is it possible in this case?
My array A is:
Array
(
[0] => content/manage/index
[Content] => Array
(
[title] =>
[type] => 5
[category] =>
[recommended] =>
[featured] =>
[status] =>
[views] =>
[last_access_date] =>
[creation_date] =>
[modification_date] =>
[availability_date] =>
[author_id] =>
)
)
My array B is:
Array
(
[0] => /content/manage/index
[Content] => Array
(
[type] => 1
)
)
So, is there any way I can if I can use array_intersect on multidimensional arrays containing different variable types without getting notice?
My problem (and question) came out of misunderstanding, what "Array to string conversion" notice really means. In my case, it was trying to tell me, that I'm trying to walk multidimensional array with functions designed to be used on single dimension array.
Understanding that led me to a solution within few seconds. There are many of them here, on SO, but the one given by deceze here looked the best for me. So I adopted it into the form of such function:
function recursiveArrayIntersect($array1, $array2)
{
$array1 = array_intersect_key($array1, $array2);
foreach($array1 as $key=>&$value)
{
if(is_array($value)) $value = recursiveArrayIntersect($value, $array2[$key]);
}
return $array1;
}
I adopted it to my project and my way of coding, but all the credits still goes to deceze (his answer here)!
Now I can find an intersection of virtually any array, no matter what kind of variable types it contain and no matter of, how deep it is (how many subarrays it contains).

Search sub arrays for specific key [duplicate]

This question already has answers here:
How to search by key=>value in a multidimensional array in PHP
(17 answers)
Closed 9 years ago.
What's the most efficient way to search an array element's sub arrays to check the value of a specific key? For example, given the following array, where I want to check both subarrays "msg" value, and if either is populated, return a boolean result:
[TGMN02] => Array
(
[2] => Array
(
[id] => 93143
[msg] =>
)
[3] => Array
(
[id] => 24876
[msg] =>
)
)
What I have at the moment is simply looping through and checking, which feels quite clunky.
I don't know about "most" efficient but this won't necessarily have to iterate through the whole array as it breaks the loop on the first value found, so technically more efficient.
function hasMsg($a){
foreach($a as $b)
if(!empty($b['msg'])) return true;
return false;
}
Okay... since some meager comments weren't accompanied by alternative suggestions - you could try using some PHP>5.3 - I really can't see how it would be any more efficient though - it must still loop through the array at some level (but I'm not 100% sure on the inner workings of the PHP interpreter - perhaps there is some internal magic that could speed things up), so this is probably purely aesthetic:
$hasMsg = !!(count(array_filter($a,function($b){ return !empty($b['msg']); })));
... if anything less efficient. There's nothing wrong with "looping" through an array - it's a tried and tested language construct that's been around since the dawn of digital time (almost).
First write some custom func and then try to use it with array_walk_recursive(array &$input , callable $funcname [, mixed $userdata = NULL ]) function. Php manual.

Why does the sort order of multidimensional child arrays revert as soon as foreach loop used for sorting ends?

I have a very strange array sorting related problem in PHP that is driving me completely crazy. I have googled for hours, and still NOTHING indicates that other people have this problem, or that this should happen to begin with, so a solution to this mystery would be GREATLY appreciated!
To describe the problem/question in as few words as possible: When sorting an array based on values inside a multiple levels deeply nested array, using a foreach loop, the resulting array sort order reverts as soon as execution leaves the loop, even though it works fine inside the loop. Why is this, and how do I work around it?
Here is sample code for my problem, which should hopefully be a little more clear than the sentence above:
$top_level_array = array('key_1' => array('sub_array' => array('sub_sub_array_1' => array(1),
'sub_sub_array_2' => array(3),
'sub_sub_array_3' => array(2)
)
)
);
function mycmp($arr_1, $arr_2)
{
if ($arr_1[0] == $arr_2[0])
{
return 0;
}
return ($arr_1[0] < $arr_2[0]) ? -1 : 1;
}
foreach($top_level_array as $current_top_level_member)
{
//This loop will only have one iteration, but never mind that...
print("Inside loop before sort operation:\n\n");
print_r($current_top_level_member['sub_array']);
uasort($current_top_level_member['sub_array'], 'mycmp');
print("\nInside loop after sort operation:\n\n");
print_r($current_top_level_member['sub_array']);
}
print("\nOutside of loop (i.e. after all sort operations finished):\n\n");
print_r($top_level_array);
The output of this is as follows:
Inside loop before sort operation:
Array
(
[sub_sub_array_1] => Array
(
[0] => 1
)
[sub_sub_array_2] => Array
(
[0] => 3
)
[sub_sub_array_3] => Array
(
[0] => 2
)
)
Inside loop after sort operation:
Array
(
[sub_sub_array_1] => Array
(
[0] => 1
)
[sub_sub_array_3] => Array
(
[0] => 2
)
[sub_sub_array_2] => Array
(
[0] => 3
)
)
Outside of loop (i.e. after all sort operations finished):
Array
(
[key_1] => Array
(
[sub_array] => Array
(
[sub_sub_array_1] => Array
(
[0] => 1
)
[sub_sub_array_2] => Array
(
[0] => 3
)
[sub_sub_array_3] => Array
(
[0] => 2
)
)
)
)
As you can see, the sort order is "wrong" (i.e. not ordered by the desired value in the innermost array) before the sort operation inside the loop (as expected), then is becomes "correct" after the sort operation inside the loop (as expected).
So far so good.
But THEN, once we're outside the loop again, all of a sudden the order has reverted to its original state, as if the sort loop didn't execute at all?!?
How come this happens, and how will I ever be able to sort this array in the desired way then?
I was under the impression that neither foreach loops nor the uasort() function operated on separate instances of the items in question (but rather on references, i.e. in place), but the result above seems to indicate otherwise? And if so, how will I ever be able to perform the desired sort operation?
(and WHY doesn't anyone else than me on the entire internet seem to have this problem?)
PS.
Never mind the reason behind the design of the strange array to be sorted in this example, it is of course only a simplified PoC of a real problem in much more complex code.
Your problem is a misunderstanding of how PHP provides your "value" in the foreach construct.
foreach($top_level_array as $current_top_level_member)
The variable $current_top_level_member is a copy of the value in the array, not a reference to inside the $top_level_array. Therefore all your work happens on the copy and is discarded after the loop completes. (Actually it is in the $current_top_level_member variable, but $top_level_array never sees the changes.)
You want a reference instead:
foreach($top_level_array as $key => $value)
{
$current_top_level_member =& $top_level_array[$key];
EDIT:
You can also use the foreach by reference notation (hat tip to air4x) to avoid the extra assignment. Note that if you are working with an array of Objects, they are already passed by reference.
foreach($top_level_array as &$current_top_level_member)
To answer you question as to why PHP defaults to a copy instead of a reference, it's simply because of the rules of the language. Scalar values and arrays are assigned by value, unless the & prefix is used, and objects are always assigned by reference (as of PHP 5). And that is likely due to a general consensus that it's generally better to work with copies of everything expect objects. BUT--it is not slow like you might expect. PHP uses a lazy copy called copy on write, where it is really a read-only reference. On the first write, the copy is made.
PHP uses a lazy-copy mechanism (also called copy-on-write) that does
not actually create a copy of a variable until it is modified.
Source: http://www.thedeveloperday.com/php-lazy-copy/
You can add & before $current_top_level_member and use it as reference to the variable in the original array. Then you would be making changes to the original array.
foreach ($top_level_array as &$current_top_level_member) {

Difference between echo and print_r in php? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What’s the difference between echo, print, and print_r in PHP?
I am able to use both of these with seemingly the same effect. Is there a difference between the two? And is one preferred over the other?
Thanks!
The difference is that print_r recursively prints an array (or object, but it doesn't look as nice), showing all keys and values. echo does not, and is intended for scalar values.
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
echo array($a); // returns "Array"
print_r($a); // returns
Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
print_r prints human-readable information about a variable, while echo is used only for strings.
Echo just gives the value, print_r gives more information about the variable itself, such as the type of data and the elements in the array, if applicable.
I think you must mean print and echo rather than print_r.... print_r is very clearly different.
First, check out the docs. On the surface, there isn't much difference:
http://php.net/manual/en/function.print.php
http://php.net/manual/en/function.echo.php
The main difference is that print can behave as a function OR a language construct. Either of these will work:
print('Something');
print 'Something';
The former method (with the parenthesis) returns a value after it prints. Now, this begs the question "Why would I need a return value after printing?" The answer is, simply, you don't. The two methods of output are both language constructs, and there isn't a clear performance difference between the two. On an extremely large scale, echo may be marginally faster than print because of the return value, but it is so negligible as to be almost impossible to measure.
There are some tricks you can do to take advantage of the fact that print will behave like a function, though I am hard pressed to give you a real-world example. Here's a not-so-realistic example:
if (print('Test')) {
// do something after the string is printed
}
Again, not so useful, but there you have it.

PHP Array Efficiency and Memory Clarification

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.

Categories