how to read string in Array ([2622232] => [] => apple ) PHP - 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)

Related

Not sure why I can't get the values of this array or decode the json

I am making a call to an API which should return a JSON array. I receive this:
Array
(
[0] => {"msg_id":"0t5OxT1ZP9VcF45L2","_text":"I want to reserve","entities":{"intent":[{"confidence":1,"value":"make_reservation","type":"value"}]}}
)
I know it's JSON because a) the docs say that's what I should be getting and b) I ran isJson($response) and got true.
I have tried to use json_decodebut the code just dies when I do (it errs saying it's expecting a string and got an array which makes sense but if I do json_encode that would just further encode the json from what I can understand).
As I understand it, I just need a way to traverse this array and get the "value:" key inside entities: intent:. However I can't figure out how to get it or where I'm wrong.
I have tried doing:
$val = $jsonArray[0]['entitites']['intent'][0]['value'] but nothing comes out.
You are trying to decode a PHP array that has encoded values.
You should try json_decode($jsonArray[0]) instead, so that you decode the value of the first array key, as that is the actual json string.
The data you posted is a php array where the value of the first element of the array is a json string.
json_decode($response[0]);

PHP API returns an array - need to reuse it in variables

I have a PHP script that dumps data from an API.
The dump is an array
print_r($answer);
outputs
Array ( [success] => 1 [serial] => s001 [url] => http://gooole.com )
I want to have another variable called $url that holds the value url from the array (held in $answer) in PHP.
I'm unfamiliar with this.
check out extract() it will take the keys from an array, and create variable of the same name to store them in. There are a few flags you can pass it, to determine exactly what it does with things like pre-existing variables of the same name.
EDIT: as mentioned in the comments on your question, though, $url = $answer['url']; is probably the simplest way to go.

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.

Why can't I access this array in Smarty?

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.

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