Storing array in session - arrays

I’m trying to handle the contents of an array in WordPress, before I store the array within a session the array outputs as follows;
Array
(
[0] => yes
[1] => no
)
However after storing in $wp_session[‘test_array’] it outputs like;
Recursive_ArrayAccess Object
(
[container:protected] => Array
(
[0] => yes
[1] => no
)
[dirty:protected] => 1
)
Is there any advise on returning the array to it’s original format so I can handle it easily.
Cheers for any help

You can do it by serialize() the array before you store it and unserialize() it when you retrieve it.
For Example:
$wp_session['test_array'] = serialize( array( 'yes', 'no' ) );
$test_array = unserialize( $wp_session['test_array'] );
print_r( $test_array );

Related

How to access the specific elements in the array?

This is the assigning part of the smarty variable in controller file.
$objSmarty->assign( 'seleted_customer_subsidy_race_types', $this->getRequestData( array( 'customer_subsidy_race_types' ) ) );
We get this displayed:
Array
(
[1] => Array
(
[subsidy_race_type_id] => 1
)
[2] => Array
(
[subsidy_race_type_id] => 2
[subsidy_race_sub_type_id] => 2
)
)
How to access subsidy_race_type_id and subsidy_race_sub_type_id fields specifically?
Follow this
foreach($arr as $item) {
print_r($item['subsidy_race_type_id']);
print_r($item['subsidy_race_sub_type_id']);
}

Why is PHP prepending a numerical index to my associative array?

When I use this code,
$views = array();
$views[]=['somevalue'=>'customerview.php'];
$views[]=['anothervalue'=>'ordersview.php'];
I get this,
Array
(
[0] => Array
(
[somevalue] => customerview.php
)
[1] => Array
(
[anothervalue] => ordersview.php
)
)
How can I make it get rid of the initial array without using array_shift or the like? Why is it putting the numerical array in the first place instead of just this,
Array
(
[somevalue] => customerview.php
[anothervalue] => ordersview.php
)
EDIT: How can I use the short syntax for this? Is that possible?
When you do this:
$views[] = ['somevalue' => 'customerview.php'];
you're saying, "Push another element onto the array, and assign to it the following value:
'somevalue' => 'customerview.php'
But this quantity is an array key and an array value. So what you're doing is inserting into your $views array a single element that itself contains an array key and array value. This explains the behavior you're seeing.
This should give you the results you want:
$views = array();
$views['somevalue'] = 'customerview.php';
$views['anothervalue'] ='ordersview.php';
Or, in shorthand:
$views = [
'somevalue' => 'customerview.php',
'anothervalue' => 'ordersview.php'
];
or you can do:
$value1 = 'first';
$value2 = 'second';
$array = array(
$value1 => 'customerview.php',
$value2 => 'ordersview.php'
);
$views is already an array so when you use $views[], you are adding another array into the existing array.
You need to use
$views = array(
'somevalue' => 'customerview.php',
'anothervalue' => 'ordersview.php'
)

Get first Smarty Array-Element

How can i get the first smarty array element ?
Actually i know how to do this... but i have following issue.
I get a array passed that looks like this
[DATA] => Array
(
[12] => Array
(
[content] => foobar
)
[1] => Array
(
[content] =>
)
[2] => Array
(
[content] =>
)
[3] => Array
(
[content] =>
)
//this is a snipit of {$myVar|#print_r}
this goes down until [11]
For some reason there is no [0] and a [12] at this position.
I don't know if this will allways be 12 but I know it allways be at the first position.
I can't sort this array because there is another array that has the same sortorder and I have to keep this order for later output.
Is there a way to select the first element without using array[0] or array.0 ?
Info: The project im working on uses Smarty 2
EDIT
I would re-index the array if i knew how :)
Since there is now answere after a couple of hours, i solved my problem temporarly.
To solve this, I opened {php} in smarty, got the array I need, splitted it into two arrays (re-indexing them of course so it starts at 0). Than I temp-save pos 0 from each array to a temp array. Override the orig 0 with underscore (_) and than multisort them, put back the original value to 0 and pass them back to $this->_tpl_vars very complicated way. (all inside the tpl)
{php}
// get array data and re-index it
$i=0;
foreach( $this->_tpl_vars['options'] as $option )
{
foreach( $option['DATA'] as $data )
$array[$i][] = $data;
$i++;
}
//delete empty entrys
$i=0;
foreach( $array[1] as $data ){
if(trim($data['content']) != ""){
$s[] = $array[0][$i];
$a[] = $array[1][$i];
}
$i++;
}
//temp save first values
$tmp_s = $s[0];
$tmp_a = $a[0];
//override first values to have a clean sort and keep them values on pos 0
$s[0] = $a[0] = "_";
//sort the arrays
array_multisort($s,$a);
//putting back the original values
$s[0] = $tmp_s;
$a[0] = $tmp_a;
//pass the array back to tpl_vars
$this->_tpl_vars['new_options'][] = $s;
$this->_tpl_vars['new_options'][] = $a;
{/php}
IF in PHP you have:
$smarty->assign(
'myVar',
array('DATA' =>
array(
12 => array('content' => 'first element'),
1 => array('content' => 'second element')
))
);
In Smarty you can use:
{assign var=first value = $myVar.DATA|#key}
{$myVar.DATA.$first.content}
And you will get displayed:
first element
However if in PHP you use:
$data = array(
12 => array('content' => 'first element'),
1 => array('content' => 'second element')
);
next($data);
$smarty->assign(
'myVar',
array('DATA' => $data
)
);
And in Smarty have the same as I showed at the beginning, the result will be:
second element
You would need to call:
reset($data);
after
next($data);
I think you cannot call reset for array in Smarty but I could be wrong.
It's also possible to reset array in Smarty but it's not so easy.
If in PHP you have:
$data = array(
12 => array('content' => 'first element'),
1 => array('content' => 'second element')
);
next($data);
$smarty->assign(
'myVar',
array('DATA' => $data
)
);
In Smarty you could use:
{assign var=$myVar.DATA value=$myVar.DATA|#reset}
{assign var=first value = $myVar.DATA|#key}
{$myVar.DATA.$first.content}
And you will get:
first element
as result
If your data is in array {$data} then using Smarty 3 you can just
{$firstData = $data|reset}

save all data in single statement without loop

How to save all data in cakePHP. Suppose I have number of records regarding single model I wish to save all data at a time. My array is in format.
Array
(
[Attendance] => Array
(
[0] => Array
(
[date] => 2013-10-09
[user_id] => 10
[attendance] => 1
)
[1] => Array
(
[date] => 2013-10-09
[user_id] => 8
[attendance] => 0
)
)
)
Is this possible to save all data a single time. I tried to do it using saveMany but it was not successful.
Do I need to do it in loop
foreach ($result as $data) {
$this->Attendance->create();
$this->request->data['Attendance'] = $data;
$this->Attendance->save($this->request->data);
}
Try saveMany instead of save
$this->Attendance->saveMany($this->request->data);
data should be in this format :
Array
(
[1] => Array
(
[Attendance] => Array
(
[field] => value
[field] => value
)
)
[2] => Array
(
[Attendance] => Array
(
[field_1] => value
[field_2] => value
)
)
)
CakePHP will save each record for each id you have on the array. The only requirement is to use the array structure convention by them, then use:
// Check method request
if ($this->request->is('post')) {
// Check not empty data
if (!empty($this->data)) {
// Save the data
if ($this-> Attendance->save($this->request->data)) {
// Set a session flash message and redirect.
$this->Session->setFlash('Attendance saved.');
$this->redirect('/Attendances');
} else {
$this->Session->setFlash('Error saving the data.');
}
}
}

Save multiple records for one model in CakePHP

I would like to save several records for one model. This would have been done pretty easily with saveAll() if it hadn't been for a problem:
I have a notification form, where I select multiple users from a <select> and two fields, for subject and content respectively. Now, when I output what $this->data contains, I have:
Array([Notification] => Array
(
[user_id] => Array
(
[0] => 4
[1] => 6
)
[subject] => subject
[content] => the-content-here
)
)
I've read on Cake 1.3 book, that in order to save multiple records for a model, you have to have the $this->data something like:
Array([Article] => Array(
[0] => Array
(
[title] => title 1
)
[1] => Array
(
[title] => title 2
)
)
)
So, how do I 'share' the subject and content to all selected users?
First off, this database design needs to be normalized.
It seems to me like a Notification can have many Users related to it. At the same time, a User can have many Notifications. Therefore,
Introduce a join table named users_notifications.
Implement the HABTM Relationship: Notification hasAndBelongsToMany User
In the view, you can simply use the following code to automagically bring up the multi-select form to grab user ids:
echo $this->Form->input('User');
The data that is sent to the controller will be of the form:
Array(
[Notification] => Array
(
[subject] => subject
[content] => contentcontentcontentcontentcontentcontent
),
[User] => Array
(
[User] => Array
(
[0] => 4
[1] => 6
)
)
)
Now, all you have to do is called the saveAll() function instead of save().
$this->Notification->saveAll($this->data);
And that's the Cake way to do it!
Those values have to be repeat like this
Array([Notification] => Array(
[0] => Array
(
[user_id] => 4
[subject] => subjects
[content] => content
)
[1] => Array
(
[user_id] => 6
[subject] => subject
[content] => contents
)
)
)
$this->Notification->saveAll($data['Notification']); // should work
If you don't pass any column value, this cake will just ignore it
You're going to have to massage your form's output to suit Model::saveAll. In your controller:
function action_name()
{
if ($this->data) {
if ($this->Notification->saveMany($this->data)) {
// success! :-)
} else {
// failure :-(
}
}
}
And in your model:
function saveMany($data)
{
$saveable = array('Notification'=>array());
foreach ($data['Notification']['user_id'] as $user_id) {
$saveable['Notification'][] = Set::merge($data['Notification'], array('user_id' => $user_id));
}
return $this->saveAll($saveable);
}
The benefit here is your controller still knows nothing about your model's schema, which it shouldn't.
In fact, you could probably redefine saveAll in your model, which hands off correctly formatted input arrays to parent::saveAll, but handles special cases itself.
There might be a more cakey way of doing this, but I've used this kind of technique: First, change the form so that you get an array like this:
Array(
[Notification] => Array
(
[subject] => subject
[content] => contentcontentcontentcontentcontentcontent
),
[selected_users] => Array
(
[id] => Array
(
[0] => 4
[1] => 6
)
)
)
(Just change the multiselect input's name to selected_users.id)
Then loop through the user ids and save each record individually:
foreach( $this->data[ 'selected_users' ][ 'id' ] as $userId ) {
$this->data[ 'Notification' ][ 'user_id' ] = $userId;
$this->Notification->create(); // initializes a new instance
$this->Notification->save( $this->data );
}

Resources