PHP: `strcmp` serialization of identical object and array failing - php

I have a legacy app where i am trying to migrate changes from the old into the new while generating a log of changes. Things are going well; however, I keep running into "changes" that change nothing. After digging into this, I found that the legacy code is using arrays and the new code is using objects. If serialized, I thought they would be identical. After all, if they are dumped via print_r they are identical. But that is not the case. Even more astounding, the objects keep their integer keys even after serialize-unserialize cycling them.
The request is: how can I show these two strings are identical since their resulting object/array is identical save for key typing.
<?php
$v3v = 'a:2:{s:9:"lastindex";s:1:"1";i:1;s:1:"1";}';
$v4v = 'a:2:{s:9:"lastindex";i:1;i:1;s:1:"1";}';
$v3 = unserialize($v3v);
$v4 = unserialize($v4v);
die('<pre>'.print_r($v3,true).' '.print_r($v4,true));
outputs (the identical):
Array
(
[lastindex] => 1
[1] => 1
)
Array
(
[lastindex] => 1
[1] => 1
)
so let's now bring them "back to life":
$v3v = serialize($v3);
$v4v = serialize($v4);
die('<pre>'.print_r($v3v,true).PHP_EOL.print_r($v4v,true));
whaaa? how did you remember your integer keys??"
a:2:{s:9:"lastindex";s:1:"1";i:1;s:1:"1";}
a:2:{s:9:"lastindex";i:1;i:1;s:1:"1";}
and how can i get you to stop???

You can use array_diff instead of strcmp. You can try this -
$v3v = 'a:2:{s:9:"lastindex";s:1:"1";i:1;s:1:"1";}';
$v4v = 'a:2:{s:9:"lastindex";i:1;i:1;s:1:"1";}';
$v3 = unserialize($v3v);
$v4 = unserialize($v4v);
echo empty(array_diff($v3, $v4)) ? 'Identical' : 'Not Identical';
array_diff($v3, $v4) will return empty array if they are indentical.
Working code

Related

mysql data insertion line by line

asking for insert multiple lines in one mysql column (with something like line break like show didn't mean inserting data in multiple rows have multiple variables contain different urls like
$var_one = "http://example.com";
$var_two = "http://example.org";
$var_two = "http://example.net";
want to store these values in one mysql column ever value should be in new line refer image describe better
asking for insert multiple lines in one mysql column (with something like line break like didn't mean inserting data in multiple rows
If you trying in php, then you can serialize the data and store as single column in DB.
While retriving data, we can unserialize and get the data as array. Find below example.
<?php
$var = array();
$var[0] = "http://example.com";
$var[1] = "http://example.org";
$var = array("1","2","3");
print_r($var); // Array ( [0] => http://example.com [1] => http://example.org )
$b=serialize($var);
echo $b; // a:2:{i:0;s:18:"http://example.com";i:1;s:18:"http://example.org";}
$c=unserialize($b);
print_r($c); // Array ( [0] => http://example.com [1] => http://example.org )

Double array elements in php

I have found some code which is really confusing for me. Maybe it's my mistake or I misunderstand. I have seen some code like this:
function my_compare($a, $b) {
if ($a['practice_id']['practice_url'] == $b['practice_id']['practice-url'])
return $a['practice_location_id']['practice-url'] - $b['practice_location_id']['practise_url'];
else
return $a['practice_id']['practice_url'] - $b['practice_id']['practise_url'];
}
I just need to know the use of practice_url and practise_location_id and practice_url .
Are these both embedded in html name or value? Please help me to understand these.
$a is an associative array. "practice_id" and "practice_url" are keys. As usual, the PHP manual has good info: http://us1.php.net/manual/en/language.types.array.php.
it's a "simple" multi-dimensional php array
In php, arrays can contains number, string, object, or an other array.
for example
$a = array('practice_id' => array('practice_url' => 2));
$b = array('practice_id' => array('practice_url' => 1));
echo $a['practice_id']['practice_url']; // display "2"
This code takes two arrays of arrays. As an example:
$a = array(
'practice_id' => array(
'practice_url'=>'some url'
),
'practice_location_id' => array(
'practice-url'='some other url'
)
);
Of course without seeing the code the arrays could be anything.
$a is an array above. $a['practice_id'] refers to the array inside $a with key 'practice_id' (as an aside this is a strangely named key as it would suggest to me that the entry is a string or number rather than an array). Likewise $a['practice_id']['practice_url'] refers to the some url value.
The function is therefore just checking if certain parts of the array are equal and returning based on that. I.e.
return $a['practice_id']['practice_url'] - $b['practice_id']['practise_url'];
Note that the above is the second strange part. Either practice_url is a number and has an oddly named key or it is indeed a url and the return will attempt to convert both to an integer before returning their difference.

PHP adodb COM recordset returning variant:__set_state for dates/times, how can I just get them as strings?

I'm accessing an mssql db in PHP and creating the connection as a new COM() object. My query runs perfectly fine. When I do the query in Microsoft SQL Server 2008, everything looks great, the dates/times show up in columns and are readable.
But when I run the same query and return the results in PHP, I go to iterate through the results like so:
$selected_fields = array("FS.fund_name", "M.media_id", "MT.media_type_name", "modified_date", "file_upload_date", "issued_date", "filename");
//query goes here
$record_set = new COM("ADODB.Recordset");
while(!$record_set->EOF){ //loop through query results and add to inline cache variable
$temp_record = array();
foreach($selected_fields as $fieldname){ //go through fields and add to array
$fieldname_array = explode(".", $fieldname); //separate table and column
$fieldname = $fieldname_array[sizeof($fieldname_array)-1];
$temp_record[$fieldname] = $record_set->fields[$fieldname]->value;
}
print_r($temp_record);
$rs_array[] = $temp_record;
$record_set->movenext();
}
As I print each $temp_record, they end up looking like this:
Array ( [fund_name] => TEST [media_id] => 1001 [media_type_name] => 8937 Tax Forms [modified_date] => variant Object [file_upload_date] => variant Object [issued_date] => variant Object [filename] => form8937test.pdf )
So the dates are some kind of object, and I don't know how to access them in PHP. And when I go to save them as a cache file, they have this value:
variant::__set_state(array())
How can I retrive the date/time in a format that I can then manipulate using PHP? I want to filter them by year, and sort them by most recent.
According to Variant Object documentation, you should be able to access it by
$temp_record['modified_date']->value;
or
$val = (string)$temp_record['modified_date'];
From the same doc:
the variant is converted to a PHP value only when there is a direct
mapping between the types that would not result in a loss of
information. In all other cases, the result is returned as an instance
of the VARIANT class.
So to check if cast is needed, simply check if you have an object of Variant class:
if (is_object($var) && $var instanceof Variant) {
$val = (string)$var;
} else {
$val = $var;
}

How to use the value of a variable that is declared as an array ( ending with '[ ]' )?

<?php $postid[] = get_the_ID(); // capture the id (a number) ?>
Now If I echo $postid I just get: Array
and when I do the following:
<?php
$default = array(
'posts_per_page' => $postid
);
?>
I don't get anything either.
Any suggestions?
When working with arrays in PHP, you can use the following to assign an array to a variable :
// initalize empty array
$a = array();
var_dump($a);
(The array() could be non-empty, of course)
Or you can use the following syntax, without the indexes :
// push items at the end of the array
$a[] = 10;
$a[] = 20;
var_dump($a);
And, finally, you can set an item at a key of your choice :
// put an item at a given index
$a['test'] = 'plop';
var_dump($a);
For more informations, see the Arrays sections of the manual.
Doing so, thanks to the three var_dump() calls, I'll get :
array
empty
array
0 => int 10
1 => int 20
array
0 => int 10
1 => int 20
'test' => string 'plop' (length=4)
Note : many use print_r() instead of var_dump() -- I tend to prefer var_dump(), which displays more informations, especially when the Xdebug extension is installed.
But note that, in any case, calling echo on an array itself :
echo $a;
Will get nothing else as output than :
Array
Which is not quite useful ;-)
Still, you can display the value of a single item of that array :
echo $a['test'];
Which, in this case, would get you this output :
plop
Basically : echo is not what you should use when you want to display an array :
Either use var_dump() if you want to inspect an array for debugging purposes,
or loop over the array with foreach, displaying each item with echo
Note : you might have to do some recursion, to inspect sub-arrays ;-)

array_push and in_array problems

I am having a bit of difficulty with this. I want to allow a user to check if a username is available through an AJAX request. The AJAX request calls my php and PHP returns true if the username is not available or false if available.
I wanted to merge the username into the array (if found) and then use in_array to locate a match. It isn't working this way however.
$res = // database returns any username that matches - (not an array)
$banned = // database returns an assoc array of banned names
array_push($banned, strtolower($res['user']));
if(!in_array(strtolower($requested), $banned)){
echo 'available';
} else {
echo 'not available';
}
Here is a sample array from the banned variable:
Array
(
[0] => bad1
[1] => bad2
[3] =>
)
The 3rd key is null because it wasn't found in the $res variable.
Is there a better way to do this? I also need to convert the values in the array to lowercase as well.
For readability, I reckon this would look better
if (isset($res['user'])) { // is this key set for this array?
$banned[] = strtolower($res['user']); // append the strtolower`d version
}

Categories