serialize value in array - php

I am trying to post serialize value to an array, is it possible.. check following...
$product = serialize($_POST['product']);
$values = array('name'=>$_POST['name'], 'product'=>$product);
print_r($values);
please let me know your suggestion to achieve this. thanks.

i solved the problem, i have used '' instead of "" for serialized data and it worked. thanks.

Related

Get value from Array inside of an Array

I have the following string output if I run print_r($val):
{"next_offset":-1,"records":[{"id":"e3266222-5389-11ed-ab30-0210c01ad3d2","name":"That is a nice name"}]}
Now, I need the value of attribute "id". Sounds simple but I'm not able getting there.
Does something like this work?
I'm assuming $val is a json string.
<?php
$val = "{\"next_offset\":-1,\"records\":[{\"id\":\"e3266222-5389-11ed-ab30-0210c01ad3d2\",\"name\":\"That is a nice name\"}]}";
$val = json_decode($val);
print_r($val->records[0]->id);
?>

extract specific data from array in mysql database using php

This is the data that i need to extract like example profile_contact_numbers
so the output will be +639466276715
how can i do it in php code??
any help will do regards
a:2:
{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}
I'm not sure 100% this can go into an array but try the unserialize function
$json_resp = {your values};
$array[] = unserialize($json_resp);
To check if it has gone into an array print_r on $array.
Read this link if the code above doesn't work
http://php.net/manual/en/function.unserialize.php
I have managed to fix it
$serialized = array(unserialize('a:2:{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}'));
var_dump($serialized);
use code :
$var = preg_split('["]','{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}');
echo $var[1].'='.$var[3]; // profile_contact_numbers=+639466276715
echo $var[5].'='.$var[7]; // profile_position=Courier

Codeigniter sending 2 values to the view from controller

Im trying to send 2 values to the view to be used from the controller.
$data1= $this->datalib->is_data();
$data2['name']=$this->namelib->getName();
Im looking to send data1 and data2 so i can use both in two places. What changes i need?
$this->load->view('person_view', array('value'=> $data1));
thanks.
You just add $data1 to the $data array, like this;
$data['name'] = $this->namelib->getName();
$data['is_data'] = $this->datalib->is_data();
Then, pass the $data array to the view;
$this->load->view('person_view', $data);
You will be able to access the data array from the view, like this;
print_r($name);
print_r($is_data);
Hope this helps.
try this,
$data['info1']= $this->datalib->is_data();
$data['info2']=$this->namelib->getName();
Pass array $data to view,
$this->load->view('person_view',$data);
In your view person_view.php you can get the values of info1 and info2. Like,
print_r($info1);
print_r($info2);
This will do the job
$this->load->view('person_view', array('data1'=> $data1,'data2'=>$data2));
You might want to do something like this
$data['temp']= $this->datalib->is_data();
$data['name']=$this->namelib->getName();
Then pass data to view
$this->load->view('person_view', $data);
In view you can use that two variables as $temp and $name.
echo $temp;
echo $name;
Change the code to
$data['data1']= $this->datalib->is_data();
$data['name']=$this->namelib->getName();
Then pass $data to the view
$this->load->view('person_view', $data);
In the view, use $data1 and $name to access these values.
use this one
$dataToShow['data'] = $this->datalib->is_data();
$dataToShow['name'] = $this->namelib->getName();
then to load view
$this->load->view('person_view', $dataToShow);
so you can access your data on your view with using this variable
$data and $name
hope will help cheers
figured it out. this was the change i needed, ty #Rupam was looking for something like that.
$this->load->view('person_view', array('value'=> $data1,'name'=>$data2['name']));

how to use JSON.stringify and json_decode() properly

Im trying to pass a mulitidimensional Javascript array to another page on my site by:
using JSON.stringify on the array
assigning the resultant value to an input field
posting that field to the second page
using json_decode on the posted value
then var_dump to test
(echo'ing the posted variable directly just to see if it came through
at all)
Javascript on page one:
var JSONstr = JSON.stringify(fullInfoArray);
document.getElementById('JSONfullInfoArray').value= JSONstr;
php on page two:
$data = json_decode($_POST["JSONfullInfoArray"]);
var_dump($data);
echo($_POST["JSONfullInfoArray"]);
The echo works fine but the var_dump returns NULL
What have I done wrong?
This got me fixed up:
$postedData = $_POST["JSONfullInfoArray"];
$tempData = str_replace("\\", "",$postedData);
$cleanData = json_decode($tempData);
var_dump($cleanData);
Im not sure why but the post was coming through with a bunch of "\" characters separating each variable in the string
Figured it out using json_last_error() as sugested by Bart which returned JSON_ERROR_SYNTAX
When you save some data using JSON.stringify() and then need to read that in php. The following code worked for me.
json_decode( html_entity_decode( stripslashes ($jsonString ) ) );
Thanks to #Thisguyhastwothumbs
When you use JSON stringify then use html_entity_decode first before json_decode.
$tempData = html_entity_decode($tempData);
$cleanData = json_decode($tempData);
You'll need to check the contents of $_POST["JSONfullInfoArray"]. If something doesn't parse json_decode will just return null. This isn't very helpful so when null is returned you should check json_last_error() to get more info on what went wrong.
None of the other answers worked in my case, most likely because the JSON array contained special characters. What fixed it for me:
Javascript (added encodeURIComponent)
var JSONstr = encodeURIComponent(JSON.stringify(fullInfoArray));
document.getElementById('JSONfullInfoArray').value = JSONstr;
PHP (unchanged from the question)
$data = json_decode($_POST["JSONfullInfoArray"]);
var_dump($data);
echo($_POST["JSONfullInfoArray"]);
Both echo and var_dump have been verified to work fine on a sample of more than 2000 user-entered datasets that included a URL field and a long text field, and that were returning NULL on var_dump for a subset that included URLs with the characters ?&#.
stripslashes(htmlspecialchars(JSON_DATA))
jsonText = $_REQUEST['myJSON'];
$decodedText = html_entity_decode($jsonText);
$myArray = json_decode($decodedText, true);`
I don't how this works, but it worked.
$post_data = json_decode(json_encode($_POST['request_key']));

How can I get all posted data as one single line in php?

I want to capture all posted data in one single line like name=asd&age=12&city=something and as an array while the data were posted by using form. (I don't wanna capture values like "$name=$_POST['name']")
(Ques-1: as a single line.
Ques-2: as an array.)
How can I do that ?
-Thanks.
I don't quite understand what you want, but I think you're looking for the following:
On a single line:
$raw_data = file_get_contents("php://input");
in an array
$array_data = $_POST // this is already an array?
$postAsLine = file_get_contents("php://input");
$postAsArray = $_POST;
echo $_SERVER['QUERY_STRING'] for current GET query
http_build_query for any array
in your case http_build_query($_POST)
You can use http_build_query($yourKeyValArray) which creates a query string from an array.
it sound like it:
$getAll = serialize($_POST);
$getAsArray = $_POST;
GOOD LUCK

Categories