Storing array in db - php

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>";

Related

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

PHP encode json 3 in 1 response

I have in a file 3 json responses. I need to encode them in one Json with 3 Objects inside which are the three responses which I have separated. Any help?-
echo json_encode(array('result_temperatura'=>$output_result_temperatura));
echo json_encode(array('result_presion'=>$output_result_presion));
echo json_encode(array('result_altitud'=>$output_result_altitud));
Build an array or object with the data:
$result = array('result_temperatura'=>$output_result_temperatura,
'result_presion'=>$output_result_presion,
'result_altitud'=>$output_result_altitud);
echo json_encode($result);
Or if you actually want a multidimensional array:
$result[] = array('result_temperatura'=>$output_result_temperatura);
$result[] = array('result_presion'=>$output_result_presion);
$result[] = array('result_altitud'=>$output_result_altitud);
echo json_encode($result);
Combine the array elements first,
$arr = array(
'result_temperatura' => $output_result_temperatura,
'result_presion' => $output_result_presion,
'result_altitud' => $output_result_altitud
);
Then encode,
echo json_encode($arr);
Hope this helps.

Assign Key to Existing Array

Assuming i have a JSON array like this example below :
[{"type":"food","alias":"meal"}]
And i want to assign this whole array to a particular key called "Dish" for example.
How can i archive this ?
Expected output should be something like :
"Dish":[{"type":"food","alias":"meal"}]
I know how to create new key value pairs but never thought of assigning a key until now.
$json = '[{"type":"food","alias":"meal"}]';
$data = json_decode($json, true);
$data = array('Dish' => $data);
echo json_encode($data);
You could do like this..
<?php
$json = '[{"type":"food","alias":"meal"}]';
$arr = array('dish'=>json_decode($json,true));
echo json_encode($arr);
Demo
echo json_encode(array('Dish' => json_decode($json, true)));
//{"Dish":[{"type":"food","alias":"meal"}]}

Attempting to unserialize data not working

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));

PHP: Give a name to an array of JSON objects?

I have managed to get data from database in PHP file.
From there(data.php),
$output = json_encode($result);
The result would be like this,
$output=[{"kitty":"Whitely"},{"kitty":"Tabby"},{"kitty":"Ruby"},{"kitty":"Silver"}]
So how do I give name "kitten" an array of kitty objects in php format?
For example like
"kitten":[{"kitty":"Whitely"},{"kitty":"Tabby"},{"kitty":"Ruby"},{"kitty":"Silver"}]
You have to wrap your result in another array on the 'kitten' key :
$output = json_encode(['kitten' => $result]);
Try this:
<?php
$kitty = array('kitten' => array());
$kitty['kitty'][] = array('kitty' => 'Tabby');
$kitty['kitty'][] = array('kitty' => 'Ruby');
$kitty['kitty'][] = array('kitty' => 'Silver');
var_dump($kitty);
var_dump(json_encode($kitty));
which results in: {"kitty":[{"kitty":"Tabby"},{"kitty":"Ruby"},{"kitty":"Silver"}]}
Use nested encode and decode
$json = '[{"kitty":"Whitely"},{"kitty":"Tabby"},{"kitty":"Ruby"},{"kitty":"Silver"}]';
echo json_encode(array('kitten' => json_decode($json)));
try to use this
$output['kitty'][] = json_encode($result);
$result =array('kitten'=> $output);
output
{
"kitten":[
{"kitty":"Whitely"},
{"kitty":"Tabby"},
{"kitty":"Ruby"},
{"kitty":"Silver"}
]
}

Categories