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}
Related
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 );
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'
)
I have started to export csv files with the help of CsvView plugin, but am stuck at one place.
So here is the controller code:
function export(){
$data = array(
array('37899810', '50001', '1', '7616'),
);
$_serialize = 'data';
$this->response->download('export.csv');
$this->viewClass = 'CsvView.Csv';
$this->set(compact('data', '_serialize'));
}
The problem is that the whole array is printed in a single cell, where I would like every item of an array to be placed in its own cell.
Any help is much appreciated.
this seems working for me :-
$result contains :-
Array
(
[0] => Array
(
[Eshop] => Array
(
[name] => test shop
)
[Productlps] => Array
(
[name] => product
)
[ProductViewLog] => Array
(
[credits_left] => 4999
[ip_address] => 10.0.0.1
[created] => 2013-12-02 06:25:07
)
)
)
$excludePaths = array(); // Exclude all id fields
$_extract = $this->CsvView->prepareExtractFromFindResults($results, $excludePaths);
$customHeaders = array('ProductViewLog.created' => 'Date Accessed','Eshop.name'=>'Shop Name','Productlps.name'=>'Product Name','ProductViewLog.credits_left'=>'Credit Balance','ProductViewLog.ip_address'=>"Accessed From");
$_header = $this->CsvView->prepareHeaderFromExtract($_extract, $customHeaders);
$_serialize = 'results';
$this->response->download('my_file.csv');
$this->viewClass = 'CsvView.Csv';
$this->set(compact('results', '_serialize', '_header', '_extract'));
I'm trying to come up with a single array of all values in specific custom fields. The values themselves are also arrays. I've tried all sorts of array functions but haven't come across the right one or the right combination. Here is my code thus far:
$args = array(
'post_type' => 'match_report',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'report_home-scorers'
),
array(
'key' => 'report_away-scorers'
)
)
);
$reportscore = new WP_Query($args);
$scorersResults = array();
if ( $reportscore->have_posts() ) {
while ( $reportscore->have_posts() ) {
$reportscore->the_post();
$homescorers = get_post_meta($post->ID,'report_home-scorers',false);
$awayscorers = get_post_meta($post->ID,'report_away-scorers',false);
foreach ($homescorers as $homescorer){
array_push($scorersResults, $homescorer);
}
foreach ($awayscorers as $awayscorer){
array_push($scorersResults, $awayscorer);
}
?>
<?php } wp_reset_postdata(); //endif
}//endwhile
$scorerResults = remove_empty($scorersResults);
function remove_empty($array) {
return array_filter($array, '_remove_empty_internal');
}
function _remove_empty_internal($value) {
return !empty($value) || $value === 0;
}
Here what I get if I print_r($scorerResults); :
Array
(
[1] => Array
(
[0] => 1
[1] => 63
)
[2] => Array
(
[0] => 263
[1] => 195
)
[3] => Array
(
[0] =>
)
[4] => Array
(
[0] =>
)
)
I just want the values in the internal arrays in an array.
Assuming you want the $scoreResults array to end up as array(1,63,263,195) you could use the array_reduce function like this:
function gatherScores($lhs, $rhs) {
foreach ($rhs as $key => $value)
if ($value)
$lhs[] = $value;
return $lhs;
}
$scorerResults = array_reduce($scorerResults, "gatherScores", array());
I'm not sure what the blank values are in your third and fourth arrays and how they should be handled, so you may need to change the if ($value) condition to check for something different. As it stands it'll obviously also filter out zero scores.
I'm trying to build an array and update a few fields in a loop.
This is my request->data
Array
(
[list] => Array
(
[4] => null
[2] => null
[3] => null
[5] => 3
)
)
It's the return value from a jquery serialized list. The key being the row id and the value being the rows parent_id.
So I loop through the array in the controller:
foreach ($this->request->data['list'] as $key => $value) {
(!isset($orders[$value])) ? $orders[$value]=0 : $orders[$value]++;
$data = array('id' => $key, 'parent_id' => (int)$value, 'display_order' => $orders[$value]);
$this->Category->save($data);
}
The $data array that I create in the loop is correct but the sql logs only shows SELECT COUNT(*) etc. for each row and no UPDATE commands.
I have tried all manner of ways to save this: using set() method, using $this->Category->id = $key; instead of directly adding the key in the data array.
Any ideas why this is not saving? I'm sure it is something simple...
i think u forgot the cake convention in the save method, you have to put all the field values inside an array with the Model name FirstCap too, something like this:
Array
(
[ModelName] => Array
(
[fieldname1] => 'value'
[fieldname2] => 'value'
)
)
Else you can use set for each and every value, and also youre forgetting to create one row for every insert youre making so try something like this:
foreach ($this->request->data['list'] as $key => $value) {
(!isset($orders[$value])) ? $orders[$value]=0 : $orders[$value]++;
$data = array( 'Category' => array('id' => $key, 'parent_id' => (int)$value, 'display_order' => $orders[$value]));
$this->Category->save($data);
}
Also you can set each and every id and then iterates over the values
foreach ($this->request->data['list'] as $key => $value) {
(!isset($orders[$value])) ? $orders[$value]=0 : $orders[$value]++;
$this->Category->id = $key;
$this->Category->set(array('parent_id' => (int)$value,
'display_order' => $orders[$value]
));
$this->Category->save();
}
try each and every answer but i think the last one will fit better at your problem.
From the CakePHP book.
When calling save in a loop, don’t forget to call create().
echo $data and match it with save() method's syntax.also do not slip to match data types of fields in table.