I am trying to set a associative array to a cookie variable in cakephp. The array is :
$recent_designers = array(
"0"=>
array(
"name" => "Hello",
),
"1"=>
array(
"name" => "Hi",
)
);
And to set this array to a cookie recent_designers :
$this->Cookie->write('recent_designers', $recent_designers);
$cookies = $this->Cookie->read('recent_designers');
$this->set("recent_designers", $cookies);
But I am getting an notice
Notice (8): Array to string conversion [CORE\cake\libs\controller\components\cookie.php, line 458] on the ctpfile ! If my array is in this format:
$recent_designers = array(
"0"=>"Hello","1"=>"Hi","2"=>"Namaste"
);
I did not get any error.
you can store a plain array in cakephp using cookie::write, but not a nested array.
http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#using-the-component.
Passing an array of 3 elements is the same of saving 3 cookies
so doing
$recent_designers = array(
"0"=>"Hello",
"1"=>"Hi",
"2"=>"Namaste"
);
$this->Cookie->write('recent_designers', $recent_designers);
is the same of
$this->Cookie->write('recent_designers.0', 'Hello');
$this->Cookie->write('recent_designers.1', 'Hi');
$this->Cookie->write('recent_designers.2', 'Namaste');
Related
I am looking for a way to parse strings in an array to an array which has a similar pattern to how CakePHP handles POST data. Or even a function in CakePHP that would do it.
UPDATED:
Current array:
array(
'data[callers]' => (int) 4,
'data[status]' => 'Unknown',
'data[country_id][107]' => (int) 1,
'data[country_id][150]' => (int) 0
)
Desired result:
array(
'callers' => (int) 4,
'status' => 'Unknown',
'country_id' => array(
(int) 107 => (int) 1,
(int) 150 => (int) 0
)
)
The purpose is saving serialized form data which can later be passed to a PHP function without having to POST the data from the browser.
The data comes from a form which was serialized and saved in the database. CakePHP generates input names in the form with brackets like this: data[country_id][107] and inside the controller you can access it like this $this->request->data['country_id']['107']
But when I serialize the form with javascript and save the raw JSON string in the database I need a way to make it into an array like CakePHP does.
Firstly make sure your array is valid first like:
$data = array (
'callers' => 4,
'status' => 'Unknown',
'country_id' => array(
'107' => 0,
'150' => 0
)
);
JSON ENCODE
Now you can json encode it
$json = json_encode($data);
echo $json; // prints: {"callers":4,"status":"Unknown","country_id":{"107":0,"150":0}}
See ^ it is now a string.
http://php.net/manual/en/function.json-encode.php
JSON DECODE
Then when you need it as an array call json_decode()
json_decode($data, true);
Note the second parameter is setting return array to true else you will get an the json returned as an object.
http://php.net/manual/en/function.json-decode.php
I would like to add an array to within an existing array.
I am tryin to use array_push which works as long as i dont try to assign a key to the array (if i try to add a key i get a syntax error... :-()
This is my initial array:
$ResultArray = array(
"TransactionDate" => "$TransactionDate",
"tx"=>array(
"0"=>array(
"TxIndex" => "$TxIndex",
"value" => "$Value",
"PaymentConfirmedCount" => "$PaymentConfirmedCount"
),
"1"=>array(
"TxIndex" => "$TxIndex",
"value" => "$Value",
"PaymentConfirmedCount" => "$PaymentConfirmedCount"
)
)
);
i would then like to add:
$ArrayTOAdd = array(
"0"=>array(
"TxIndex" => "$TxIndex",
"value" => "$Value",
"PaymentConfirmedCount" =>
"$PaymentConfirmedCount"
)
);
if I try:
array_push($ResultArray->tx, $ArrayTOAdd);
BUT this does not work and results in a warning of "array_push() [function.array-push]: First argument should be an array"
if i try this :
array_push($ResultArray, $ArrayTOAdd);
it just adds the array but not to $ResultArray->tx
Any suggestions would be greatly welcomed!
You have to access the element in the array with $ResultArray["tx"] and not $ResultArray->tx. The second one is for the access to members in a php class. So an
array_push($ResultArray["tx"], $ArrayTOAdd);
should work.
i seem to be stuck at this problem for quiet a few time, basically i have used cookie in codeigniter, and passed an array with different names to different functions, the code to set the cookie is
$data = array (
'client_block_ID' => $client_block_ID,
'client_unit_ID' => $client_unit_ID,
'blockUnits' => $blockUnits
);
$cookieName ='tab'.$counter;
$cookie = array(
'name' => $cookieName,
'value' => $data,
'expire' => '86500',
);
$this->input->set_cookie($cookie);
now i just dont know how to get the variables inside the array i.e what will be the syntax to get client_block_ID??
Now your cookie array will look like this.........
$cookie = array(
'name' => $cookieName,
'value' => array(
'client_block_ID' => $client_block_ID,
'client_unit_ID' => $client_unit_ID,
'blockUnits' => $blockUnits
);
'expire' => '86500',
);
So to get your client_block_ID from your $cookie array you have to loop through that array like below.
foreach($cookie as $c)
{
echo $c['name'];
foreach($c['value'] as $v)
{
echo $v['client_block_ID'];
}
}
You did not expect array as value;
I did and I see:
A PHP Error was encountered
Severity: Warning
Message: setcookie() expects parameter 2 to be string, array given
Filename: core/Input.php
Line Number: 404
use
$this->input->cookie()
Lets you fetch a cookie. The first parameter will contain the name of the cookie you are looking for (including any prefixes)
The function returns FALSE (boolean) if the item you are attempting to retrieve does not exist.
try this,
$cookievalue= $this->input->cookie('value');
if($cookievalue){
//cookie exists
foreach($cookievalue as $cookie){
echo $cookie['client_block_ID'];
}
}else{
//cookie doesnot exists
}
It is possible to put an array into a multi dim array? I have a list of user settings that I want to return in a JSON array and also have another array stored in that JSON array...what is the best way to do that if it isn't possible?
A multi dimension is already an array inside an array. So there's nothing stopping you from putting another array in there. Sort of like dreams within dreams :P
Just use associative arrays if you want to give your array meaning
array(
'SETTINGS' => array(
'arr1' => array( 0, 1),
'arr2' => array( 0, 1)
),
'DATA' => array(
'arr1' => array( 0, 1),
'arr2' => array( 0, 1)
)
)
EDIT
To answer your comment, $output_files[$file_id]['shared_with'] = $shared_info; translates to (your comment had an extra ] which I removed)
$shared_info = array(1, 2, 3);
$file_id = 3;
$output_files = array(
'3' => array(
'shared_with' => array() //this is where $shared_info will get assigned
)
);
//you don't actually have to declare it an empty array. I just did it to demonstrate.
$output_files[$file_id]['shared_with'] = $shared_info; // now that empty array is replaced.
any array key can have an array value in php, as well as in json.
php:
'key' => array(...)
json:
"key" : [...]
note: php doesn't support multidimensional arrays as in C or C++. it's just an array element containing another array.
I'm storing images links into the database separating them with ,, but I want to transform this string into an array, but I'm not sure how to do it.
So my array looks like this:
$array = array(
"name" => "Daniel",
"urls" => "http:/localhost/img/first.png,http://localhost/img/second.png"
);
So I'd like to have it in the following form:
$array2 = array(
"name" => "Daniel",
"urls" => array("http:/localhost/img/first.png",
"http://localhost/img/second.png" )
);
I haven't been PHP'ing for a while, but for that simple use-case I would use explode.
$array['urls'] = explode(',', $array['urls']);
Uncertain if I interpreted your question correct though?
You can use array_walk_recursive like in the following example.
function url(&$v,$k)
{
if($k=='urls') {
$v = explode(",",$v);
}
}
$array = array(
"name" => "Daniel",
"urls" => "http:/localhost/img/first.png,http://localhost/img/second.png"
);
array_walk_recursive($array,"url");
You can check the output on PHP Sandbox