0

I have a line of code that has worked on my local develop machine but does not work (as expected) on another machine.

Here is my debugging of the call to array_unique:

debug("Parameter array size: ".sizeof($parameters));
debug("Sorted array size: ".sizeof(array_unique($parameters, SORT_REGULAR)));
debug_r($parameters);
debug_r(array_unique($parameters, SORT_REGULAR));

(I've coded the debug and debug_r functions to output the input, but nicely formatted.)

The results are:

Parameter array size: 10
Sorted array size: 0
Array ( [0] => ~oli_Search_Term [1] => ~oli_Search_Term [2] => ~oli_Search_Term [3] => ~oli_Search_Term [4] => ~tim_Time_From [5] => ~tim_Time_To [6] => ~tim_Time_From [7] => ~tim_Time_To [8] => ~tim_Time_From [9] => ~tim_Time_To )

Note - there is nothing output for that final debug call (intended to dump the contents of array_unique($parameters, SORT_REGULAR).

Why is the array empty (and only on one machine)?

The machine where the output is empty is running PHP Version 5.0.3.

Adding the following:

vardump($parameters);

... yields:

array(10) { [0]=> string(16) "~oli_Search_Term" [1]=> string(16) "~oli_Search_Term" [2]=> string(16) "~oli_Search_Term" [3]=> string(16) "~oli_Search_Term" [4]=> string(14) "~tim_Time_From" [5]=> string(12) "~tim_Time_To" [6]=> string(14) "~tim_Time_From" [7]=> string(12) "~tim_Time_To" [8]=> string(14) "~tim_Time_From" [9]=> string(12) "~tim_Time_To" }

youcantryreachingme
  • 1,065
  • 11
  • 17
  • Working fine for me https://eval.in/807346 – Sahil Gulati May 30 '17 at 06:25
  • Working fine on my other PC also, which is running PHP Version 5.6.25. – youcantryreachingme May 30 '17 at 06:26
  • Can you please update `var_dump($parameters)` in your post? – Sahil Gulati May 30 '17 at 06:28
  • Done - see bottom of post. – youcantryreachingme May 30 '17 at 06:31
  • Which version you are using currently? – Sahil Gulati May 30 '17 at 06:32
  • Where does this `$parameters` come from? _POST, session, database, ...? – LSerni May 30 '17 at 06:34
  • Failing php version is 5.0.3. Working php version is 5.6.25. The $parameters array is built up by parsing a text file. The text file contains SQL. At various points in the text file there may be a "parameter". A parameter begins with "~" and ends with any of the space character, single quote, percent, carriage return, closing bracket, semicolon or comma. In other words, a "while" loop uses strpos to find a tilde (~) character, then looks for any of the above other characters to identify the end of the parameter name, and puts that parameter name into the array. – youcantryreachingme May 30 '17 at 06:35

1 Answers1

1

After not finding a solution to the question above, I began seeing whether there was a workaround - to code this another way.

The workaround I found:

For all instances of:

array_unique($parameters, SORT_REGULAR)

Replace with:

array_unique($parameters)

I was unsure as to what this modified behaviour would be although at a glance it seemed to produce the result I wanted. In order to check explicitly what this change would do, I referred to the manual and found this:

"Added the optional sort_flags defaulting to SORT_REGULAR. Prior to 5.2.9, this function used to sort the array with SORT_STRING internally. "

So the root cause was that on the second server, running 5.0.3, the optional flag argument is not supported.

youcantryreachingme
  • 1,065
  • 11
  • 17