Why can't I access this array in Smarty? - php

Given this in the Smarty template:
<pre>{$user->settings['sendStats']|#print_r:1}</pre>
The output in the browser is this:
Array
(
['period'] => daily
['ofPeriod'] => year
['points'] => 1000
)
Doing any of these:
<pre>{$user->settings['sendStats']['period']|#print_r:1}</pre>
<pre>{$user->settings['sendStats'][ofPeriod]|#print_r:1}</pre>
<pre>{$user->settings['sendStats'].points|#print_r:1}</pre>
<pre>{$user->settings.{'sendStats'}.{'period'}|#print_r:1}</pre>
<pre>{$user->settings.{sendStats}.{period}|#print_r:1}</pre>
with or without the |#print_r:1 gives no output in the browser.
I also tried assigning $user->settings to a Smarty variable and I get the exact same result (as expected).
How do I access the elements of the $user->settings['sendStats'] array?

{$user->settings.sendStats.period|#print_r:1} should work just fine. Also have a look at the Variables page in the docs…

The array values itself are not arrays (your array is not multidimensional), so you should drop the |#print_r:1 and you should be fine. Should look something like:
<pre>{$user->settings['sendStats']['period']}</pre>

Finally figured it out. The array keys contained single quotes, so the answer would have been:
{$user->settings['sendStats']["'period'"]}
I fixed it so the keys didn't contain quotes anymore.

Related

how to read string in Array ([2622232] => [] => apple ) PHP

I am trying to read string in session array.
Here is the code I entered $fruit_type into my session:($number is 2622232 here)
$_SESSION['fruit'][$number]=$fruit_type;
When I used print_r($_SESSION['fruit']), I get the following array:
Array ( [2622232] => [] => apple )
My question is how can I get the string "apple"? My editor give me error message when I tried to use $_SESSION['fruit'][$number][] to read string.
Any idea about my situation?
The only way to get that print_r() output is with an empty string as key [''], so you need to find where you do that and fix it. However you can access it:
echo $_SESSION['fruit'][2622232][''];
$_SESSION['fruit'][$number][] is wrong as you don't pass an index. Something like $_SESSION['fruit'][$number][0] should work.
You can find more information about arrays in the PHP documentation (here the example that solves your problem)

Make array from array values and save it to a file

I am working on a project in which I will get an array.
When I use print_r($arr) it looks like this:
Array
(
[0] => games,
[1] => wallpapers,
.....
)
What i want to do is that i want it's value to be in an array like array('games','wallpapers') and save it to a file called data.txt using file_put_contents.
I did it once myown using implode() but sometimes it gets error. Is there a good way?
You need to serialize it and save it to a file. You can do it as a comma-separated values using implode(), or as a json string using json_encode() or using serialize(). All three of those links have excellent documentation and examples.
If you still have troubles, please edit your question with more specific details and the code you've worked on so far.
Try this,
<?php
$a=array
(
0 => 'games',
1 => 'wallpapers'
);
$str= implode(',',$a);
file_put_contents('data.txt', $str);
?>
data.txt
games,wallpapers
To retrieve the values, use array_values(). To store this array to disk, first serialize(), then store the output anyway you like - file_put_contents is the simplest way I believe.

can not call array values from php ini file using parse_ini_file

I have tried reading parameters from php ini files using parse_ini_file
Here is my code
$param_array=parse_ini_file("test.ini");
print $param_array[0];
print $param_array[1];
Above code returns nothing
test.ini file contains,
[names]
me = Robert
you = Peter
[urls]
first = "http://www.example.com"
second = "http://www.w3schools.com"
Now i need to call the Robert & Peter
If you'd have rather consulted the Manual Pages instead of w3fools, you'd have seen a reliable example where it's shown that the returned array is associative, that is, the names of the properties are the keys of the array.
So instead of
$param_array[0]
you should use
$param_array['me']
to access "Robert"
parse_ini_file returns an associative array. You are trying to access the fields in your array using numerical indices, but they aren't defined.
Try accessing those fields using the names you gave them in the ini-file as the key:
echo $param_array['me']; // will yield Robert
Helpful tip: investigate the structure of arrays and variables to find out what they look like using print_r or var_dump
Following code worked!
print $param_array["me"];
print $param_array["you"];

Remove an item from an array?

I've created an array from a PHP session variable, and now I'm trying to use ajax (within jQuery) to remove an element from the array.
I've got the following code so far:
$val = $_SESSION['enquiry-basket'];
$array = explode($val);
foreach ($enquiries as $a => $q) {
if ($q == $_POST['product_id']) {
unset($array[$a]);
}
}
The only problem is, it doesn't remove the item.
Can anyone explain why, and tell me how to fix it?
Edit
Sorry guys. The reason I mentioned jQuery is because I use a jQuery ajax call to process the PHP I displayed above.
The ajax query runs fine because it processes some javascript goodies (remove's a div from the HTML) once the ajax returns a success.I've added the delimiter (can't believe I missed it) but the element doesn't get removed from the array still.
I've never been good at multi-dimensional arrays, so here's the array printed:
Array ( [0] => 6 [1] => 8 )
It looks right to me, but I'm an amateur in arrays. (6 and 8 are of course my strings I inserted)
explode is missing the first argument:
explode(',', $val);
You are removing item from $array, not from $_SESSION['enquiry-basket'].
The explode function should have two parameters. But you given only the name of the array.
explode(separator,string,limit);
If I understand correctly what you are trying to do, the problem is that JQuery runs client side, which means that your PHP arrays on the server side disappear between each request from Ajax. The only array that remains is $_SESSION.
If you want to use AJAX, you need to remove from $_SESSION directly. Anything else is just useless because the arrays and variables "disappear" between each call.
Mostly an issue with the explode function, the second parameter is missing:
Change from:
$array = explode($val);
To:
$array = explode('~',$val); // ~ is a delimiter

How to access array element "Array ( [#text] =>"

I had an XML that I turned into an array to sort it, now I want to save it as an XML again. This would not be a problem if I didn't have the following: [caseid] => Array ( [#text] => 885470 ...
I need: <caseid> 885470 </caseid>
Writing the DOM has been fine for the fieldname "caseid", but the fieldvalue is an array titled "#text" that I cannot figure out how to parse.
I can't search google for "#" symbols, so searching has been pretty tough.
I was able to access the number in the array by referencing it as a string. Stupid way to do it, but it works; the best solution would be to edit the array to xml conversion, but accessing array elements via strings worked too.
I was previously accessed array elements like so:
print_r($array[caseid][#text]); //<--Does not work with #
print_r($array[caseid]['#text']); //works
Again, not the prettiest, but a viable workaround.

Categories