Attempting to unserialize data not working - php

I am really going crazy at the moment, i have the following serialized, however when i try to unserialized it returns false.
Serialized array:
Array
(
[0] => a:1:{i:0;s:9:"714443801";}
)
Current Code:
<?php
$votesArray = unserialize($Vzzz);
echo "<pre>";
print_r($votesArray);
echo "</pre>";
?>

You are not unserializing it:
try:
print_r(unserialize($votesArray[0]))

If you are storing the serialized string, then store like this:
$serialisedData = base64_encode(serialize($arr));
After Retrieving,
//to unserialize...
$arr = unserialize(base64_decode($serialisedData));

Related

Storing array in db

I need to create an array of keys and store it in a db table.
$myArr = array();
foreach($keys AS $key) {
$myArr[$r->id] = $r->key;
}
before storing it I serialize it
$db_arr = serialize($myjArr);
Later I need to get the stores array and loop through it to perform some action. However, when I unserialize stored array and do print_r my output looks like this:
Array ( [5981] => 7u7Dj [5982] => mVmx4 )
It appears that the array is malformed. What am I missing?
You should take a look at this, I think you may need to unserialize the data before trying to use it php unserialize
$array = unserialize($serialized_array);
Here is an example
$original = [
"who" => "you",
"me" => "yes"
];
echo "<pre>";
print_r($original);
echo "</pre>";
$ser = serialize($original);
echo "<pre>";
print_r($ser);
echo "</pre>";
$un = unserialize($ser);
echo "<pre>";
print_r($un);
echo "</pre>";

delete an element of given key of array in php

$nesAry=array();
$nesAry["name"]="abc";
$nesAry["email"]="abc#email.com";
$nesAry1=array();
$nesAry1["name"]="abc1";
$nesAry1["email"]="abc1#email.com";
$nesAry2=array();
$nesAry2["name"]="abc2";
$nesAry2["email"]="abc2#email.com";
$responseAry = array();
$responseAry[0]=$nesAry;
$responseAry[1]=$nesAry1;
$responseAry[2]=$nesAry2;
echo json_encode($responseAry); // here output like this => [{"name":"abc","email":"abc#email.com"},{"name":"abc1","email":"abc1#email.com"},{"name":"abc2","email":"abc2#email.com"}]
unset($responseAry[1]);
echo "------------removed 1--------";
echo json_encode($responseAry); // but here output like this => {"0":{"name":"abc","email":"abc#email.com"},"2":{"name":"abc2","email":"abc2#email.com"}}
I want Out put Like this after removing an element \n [{"name":"abc","email":"abc#email.com"},{"name":"abc2","email":"abc2#email.com"}]
Please Help me
Try to regenerate your array after unset an item:
$nesAry=array();
$nesAry["name"]="abc";
$nesAry["email"]="abc#email.com";
$nesAry1=array();
$nesAry1["name"]="abc1";
$nesAry1["email"]="abc1#email.com";
$nesAry2=array();
$nesAry2["name"]="abc2";
$nesAry2["email"]="abc2#email.com";
$responseAry = array();
$responseAry[0]=$nesAry;
$responseAry[1]=$nesAry1;
$responseAry[2]=$nesAry2;
echo json_encode($responseAry); // __here output like this => [{"name":"abc","email":"abc2#email.com"},{"name":"abc1","email":"abc1#email.com"},{"name":"abc2"}]__
unset($responseAry[1]);
$responseAry = array_values($responseAry); //regenerate array(reindexing)
echo "------------removed 1--------";
echo json_encode($responseAry); //[{"name":"abc","email":"abc#email.com"},{"name":"abc2","email":"abc2#email.com"}]
EDIT:
As other option you can use array_splice method http://php.net/manual/en/function.array-splice.php
$nesAry=array();
$nesAry["name"]="abc";
$nesAry["email"]="abc#email.com";
$nesAry1=array();
$nesAry1["name"]="abc1";
$nesAry1["email"]="abc1#email.com";
$nesAry2=array();
$nesAry2["name"]="abc2";
$nesAry2["email"]="abc2#email.com";
$responseAry = array();
$responseAry[0]=$nesAry;
$responseAry[1]=$nesAry1;
$responseAry[2]=$nesAry2;
echo json_encode($responseAry); // __here output like this => [{"name":"abc","email":"abc2#email.com"},{"name":"abc1","email":"abc1#email.com"},{"name":"abc2"}]__
array_splice($responseAry,1,1);
echo "------------removed 1--------";
echo json_encode($responseAry);
Your array when first converted to json is a so called "associative" array, and json_encode then exports it to the object you see in the first echo.
After your unset, the array is changed to a "numeric" array and json_encode will export the array with array keys.
Php it self does not care about how the array is used, but json_encode does.
You can use
echo json_encode(array_values($responseAry));
Or not change the final array you want to export

How to insert an array in a json object in PHP

I have a string on my databas, that I'm trying to insert into a json object as an Array, not a string..:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
$jsonObject = array('numbers' => $arrayInString);
$json = json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
echo $json;
When I execute this.. my Json object is..
numbers: "[2,3,5,5,6]";
and not
numbers: [2,3,5,5,6];
Like I originally wanted.. can't get this to work, can anyone help?
Like was said you need to decode the passed data from the database and then build your array output. Something like this:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
// decode the JSON from the database as we build the array that will be converted back to json
$jsonObject = array('numbers' => json_decode($arrayInString));
echo json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
The slightly modified code above outputs:
{"numbers":[2,3,5,5,6]}
You need to json_decode your $arrayInString variable before you add it to the associative array.

Php json decode with child

I been looking thru the posts here all day but can't figure out what I'm doing wrong. (I'm new to php and json)
Here is my code that work.
$json = '{"id":1234,"img":"1.jpg"}';
$data = json_decode($json, true);
echo $data["img"];
But when the json respond is this
$json = '{"demo1":[{"id":1234,"img":"1.jpg"}],"userId":1}';
it's a big harder for me. then img is a child of demo1? How to get it?
Thx. :)
Figuring out the array indices
As you're new to PHP, I'll explain how to figure out the array indces of the required array value. In PHP, there are many functions for debugging — print_r() and var_dump() are two of them. print_r() gives us a human-readable output of the supplied array, and var_dump() gives us a structured output along with the variable types and values.
In this case, print_r() should suffice:
$json = '{"demo1":[{"id":1234,"img":"1.jpg"}],"userId":1}';
$data = json_decode($json, true);
// wrap the output in <pre> tags to get a prettier output
echo '<pre>';
print_r($data);
echo '</pre>';
This will produce the following output:
Array
(
[demo1] => Array
(
[0] => Array
(
[id] => 1234
[img] => 1.jpg
)
)
[userId] => 1
)
From there, it should be pretty easy for you to figure out how to access the required vaule.
$data['demo1'][0]['img'];
Creating a helper function for ease of use
For ease of use, you can create a helper function to make this process easier. Whenever you want to view the contents of an array, you can simply call dump_array($array); and be done with it. No more messing around with <pre> tags or print_r().
Function code:
function dump_array($array) {
echo '<pre>' . print_r($array, TRUE) . '</pre>';
}
Usage example:
$arr = ['foo' => range('a','i'), 'bar' => range(1,9)];
dump_array($arr);
after decoding :
echo $data->demo[0]->img;
Basically, a { in JSON leads to a -> (it's an object).
And a [ to a [], it's an array.

PHP json_encoded object not passing to other page

I'm using json_encode on an object and stored on a hidden text field, When i was passing to the next page i didn't get any data
Code:
$flight = json_encode($od->FlightSegments);
Response page:
<?php print_r($_POST); ?> printed Array ( [flight] => { )
serialize and unserialize not working on my object.
Can any one tell me what's going wrong ?
To see XML literally on the web page, use:
<?php echo '<pre>' . htmlentities(print_r($_POST, true)) . '</pre>'; ?>
Since serialize and unserialize are not giving me the correct output for me, I solved my problem using below code.
$count = 0;
$_SESSION['fl'] = [];
foreach($response->Response__Depart->OriginDestinationOptions->OriginDestinationOption as $od){
$flight = json_encode($od->FlightSegments);
$_SESSION['fl'][$count] = $flight;
}
Stored entire object into a session array

Categories