php unserialize data from table not working - php

i need to unserialize a string to an array.Here is the stringwhich needs to be unserialized in php to assosiative array.
a:1:{i:0;s:158:"a:6:{s:5:"rowid";s:32:"94ca9ee0c4e3184b50e89e82f80332fb";s:2:"id";
s:2:"68";s:3:"qty";s:1:"1";s:5:"price";
s:2:"20";s:4:"name";
s:5:"Bread";s:8:"subtotal";i:20;}";
}

It looks like this has been doubly serialized. It expands to an array with a single element, and that element is a serialized associative array. So you need to do:
$temp = unserialize($data);
$result = unserialize($temp[0]);
var_dump($result);
Result:
array(6) {
["rowid"]=>
string(32) "94ca9ee0c4e3184b50e89e82f80332fb"
["id"]=>
string(2) "68"
["qty"]=>
string(1) "1"
["price"]=>
string(2) "20"
["name"]=>
string(5) "Bread"
["subtotal"]=>
int(20)
}
If there can be more that one element in the top-level serialized array, use array_map to unserialize all of them:
$result = array_map('unserialize', $temp);
$result will now be a 2-dimensional array.
I'm not sure why you stored your data this way. Why not just serialize the original 2-d array all at once, instead of nesting them?

Related

Accessing single elements PHP / mongoDB output

I have finally managed to retrive data from mongoDB within PHP. How ever I am not able to retrieve single elements from this array looking. I can only vardump() the cursor. How is it possible to print single elements from this array that seems to be made up of objects?
object(stdClass)#11 (7) { ["_id"]=> object(MongoDB\BSON\ObjectID)#9 (1) { ["oid"]=> string(24) "5a4a2cf55ff0f310cbf1c3a4" } ["Category"]=> string(9) "Allgemein" ["DateAdded"]=> object(MongoDB\BSON\UTCDateTime)#10 (1) { ["milliseconds"]=> string(13) "1514810613331" } ["Name"]=> string(4) "Welt" ["Website"]=> string(11) "www.welt.de" ["Active"]=> bool(true) ["Country"]=> string(2) "DE" }
I couldnt find anything on goolgle or PHP/mongodb documentation. Why cant I just do $array["_id"]? And how can I retrieve _id for example?
The resource is an object of stdClass. So you need to use:
echo $array->_id;
In case, if you want to use arrays, use get_object_vars() function. That way:
$array = get_object_vars($array);
echo $array["_id"];
And then you can use objects as arrays.

How to get the difference of two arrays

So I have two arrays which looks like this when I do a var_dump :
array(4) {
["DatabinFieldName_1"]=> string(7) "Heading"
["DatabinFieldType_1"]=> string(13) "VARCHAR (255)"
["DatabinFieldName_3"]=> string(11) "DateCreated"
["DatabinFieldType_3"]=> string(8) "DATETIME"
}
array(8) {
["DatabinFieldName_1"]=> string(7) "Heading"
["DatabinFieldType_1"]=> string(13) "VARCHAR (255)"
["DatabinFieldName_2"]=> string(4) "Copy"
["DatabinFieldType_2"]=> string(4) "TEXT"
["DatabinFieldName_3"]=> string(11) "DateCreated"
["DatabinFieldType_3"]=> string(8) "DATETIME"
["DatabinFieldName_4"]=> string(8) "Comments"
["DatabinFieldType_4"]=> string(4) "TEXT"
}
I need to get the difference in a result. Which I have tried using this code.
// Get POST Array
$databinPostArray = $_POST;
// Get Databin Array
$databinObject =json_decode($nbase->getwhere("Databins","ID='".$databinID."' LIMIT 1;",$_SESSION["UserDB"]));
$databinArray= unserialize($databinObject[0]->DatabinArray);
var_dump($databinPostArray);
var_dump($databinArray);
$result = array_diff($databinPostArray, $databinArray);
print_r($result);
Problem is I keep getting Array() back which means its not finding any differences even though there is.
array_diff() returns the elements of the second array which are not in the first one.So the answer to your question is:
$result = array_diff($databinPostArray, $databinArray);
if (couunt($result) == 0) {
$result = array_diff($databinArray, $databinPostArray);
}
This way, the difference will be returned, whether there is more keys in $databinPostArray or in $databinArray.
If what you want is only to check which elements are in $databinArray, but not in $databinPostArray, please do:
$result = array_diff($databinArray, $databinPostArray);
You need to reverse the arguments:
$result = array_diff($databinArray, $databinPostArray);
array_diff returns an array with everything in the first array that isn't in the second array.
If you want to get all the elements that are unique to either array, you can use:
$result = array_diff(array_unique(array_merge($databinArray, $databinPostArray)),
array_intersect($databinArray, $databinPostArray));

PHP Array_intersect on multidimensional array with unknown number of keys

I'm trying to make advanced search filters in an application that holds resources (people). I've got all the results in 1 multidimensional array. A user of the application can search for the persons Job title, skills, work field and country.
I've already made the part where I look up the people that meet the criteria given by the user. These results are stored in a multidimensional array. If the user is looking for someone with a specific resource with a job title and a specific skill the return value is this:
$realfilters = array(2) {
["resourcesWithJobtitle"]=> array(6) {
[0]=> string(1) "1"
[1]=> string(2) "48"
[2]=> string(2) "88"
}
["resourcesWithSkill"]=> array(9) {
[0]=> string(1) "4"
[1]=> string(1) "8"
[2]=> string(1) "48"
[3]=> string(2) "50"
}
When the user also looks for a work field this is added to the result:
["resourcesWithWorkfield"]=> array(3) {
[0]=> string(2) "48"
[1]=> string(2) "96"
[2]=> string(2) "97"
}
I need to know which resources meet all dimensions of the array so I can display them. (So in this example I need an array with just 1 value: 48). I think I need to use array_intersect but can't seem to get it right.
One of the possible solutions: you may first extract() the $realfilters array values to variables, and then apply the array_intersect() to them. But this solution is applicable only if there are not many possible filters.
Another one and probably the best solution would be to intersect in a loop, something like:
$res_arr = array_shift($realfilters);
foreach($realfilters as $filter){
$res_arr = array_intersect($res_arr, $filter);
}
$intersection = call_user_func_array('array_intersect', $array);
That will give you the elements present in all the sub arrays of $array.
edit-
This above is like a shortcut for writing:
$intersection = array_intersect($array['a'], $array['b'], ...and so on for all elements...);
A loop could be used as well
$intersection = reset($array);
foreach ($array as $subarr) {
$intersection = array_intersect($intersection, $subarr);
}
print_r($intersection);

Modifying an array inside an array

So I need to modify the array in a memcached key-value pair. I need to remove one of the arrays inside the array. An example of what it looks like:
array(2) { [0]=> array(3) { ["username"]=> string(3) "Bob" ["id"]=> string(5) "14537" ["comment"]=> string(4) "cool"} [1]=> array(3) { ["username"]=> string(3) "Tom" ["id"]=> string(5) "14538" ["comment"]=> string(3) "yes"}}
If I know the values of username, id, and comment, how can I delete it? The generic queston: How can I delete array 0?
Considering the answer of doing a foreach loop, I tried
foreach($memcachedarray as $f){
if ($f['id'] == '14537'){
echo key($f);
}
}
But it spits out username
Edit- Ok
I searched some more and found I need to do this:
foreach($memcachedarray as $key => $f){
if ($f['id'] == '14537'){
echo $key;
}
}
That works!
If the Id's are unique across the system then you could use an associative array to store you data then unset the key, otherwise you would want to use a foreach loop to get the array key, then unset that key and recommit your new array back into memcache.

PHP var_dump array

array(14) {
[0]=>
string(1) "1"
["id"]=>
string(1) "1"
[1]=>
string(7) "myUserName"
["UserID"]=>
string(7) "myUserName"
[2]=>
string(10) "myPassword"
["passwordID"]=>
string(10) "myPassword"
[3]=>
string(24) "myEmail#domain.com"
["emailAddress"]=>
string(24) "myEmail#domain.com"
[4]=>
string(7) "myFirstName"
["firstName"]=>
string(7) "myFirstName"
[5]=>
string(8) "myLastName"
["lastName"]=>
string(8) "myLastName"
[6]=>
string(1) "1"
["active"]=>
string(1) "1"
}
how do i access the contents of this array using PHP?
the above was a var_dump($info)
It depends on which part of the array you are trying to access.
If you are trying to access a specific item, you can access it by its index ; for instance :
echo $info['passwordID'];
Should give you :
myPassword
(edit after the comment)
For the email address, there is this portion in your var_dump's output :
["emailAddress"]=>
string(24) "myEmail#domain.com"
This indicates that the e-mail address is stored in the array as an element that has the key "emailAddress".
Which means you should be able to get that e-mail address like this :
echo $info['emailAddress'];
And as you also have this portion of text in the var_dump's output :
(About that duplication of data, you should read Pekka's answer, who provides an idea of why your data is in your array twice, with both integers and strings as keys)
[3]=>
string(24) "myEmail#domain.com"
You could also use :
echo $info[3];
(of course, in each of those cases, you could also store this to a variable for futures re-use)
Another solution, if you want to access each item, would be to use some foreach loop ; for instance :
foreach ($info as $key => value) {
echo "Value for key $key is $value <br />";
}
You might want to go through the arrays section of the PHP manual, for more informations.
And, also, the section about array functions.
You can use either the numeric or the associative key:
echo $array[0]; // outputs 1
echo $array["id"]; // outputs 1
I'm guessing this is the result of a mysql_fetch_array() operation, isn't it? You may want to specify whether you want a numeric or associative array using the second parameter to that function.
Example:
$record = mysql_fetch_array($query, MYSQL_ASSOC); // for associative keys only
$record = mysql_fetch_array($query, MYSQL_NUM); // for numeric keys only
The array appears to have both string and numeric keys. You can access the fields using the array index operator []. Supply either the numeric key or the column name:
echo $info['UserID']; // output "myUserName"
echo $info['emailAddress']; // output "myEmail#domain.com"
if $info is the array, then you can echo $info[6] for example.
If you want it as a string then $s=print_r($info,true);
It seems you are doing some thing wrong, as there shouldn't be a need to access array like that, and there other ways to access objects as arrays.

Categories