I have 2 files that put something in the $_SESSION array.
file1.php
<?php
session_start();
$_SESSION[] = 'Hi';
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
That prints
Array
(
[0] => Hi
)
And file2.php that is similar to file1
<?php
session_start();
$_SESSION[] = 'There!';
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
I suppose to go to file1 at first and then move to file2.
Printing $_SESSION in file2 should output
Array
(
[0] => Hi
[1] => There!
)
am I wrong?
I've to mention that I get the notice: Unknown skipping numeric key 0 in Unknown on line 0.
And the register_globals in my php.ini is set to Off.
As I see in the comments for someone of you file2 prints an array of 2 items and for someone else (like me) the 'hi' items get lost. This seems to happen, but not for Marc B, only if we use a number as index of the session array, not with a string.
For Marc B the session behaves as I expected. Can you post your php.ini here? So I can compare yours with mine?
Superglobals like $_SESSION are not normal arrays. You should store an array inside $_SESSION, like so:
file 1: $_SESSION['foo'][] = 'Hi!';
file 2: $_SESSION['foo'][] = 'there';
You aren't giving $_SESSION the appropriate key.
No, that should work. In fact, if you just kept reloading file1, you'd just get a series of "Hi", "Hi", "Hi", etc.. array entries.
Is there a particular reason why you did
$_SESSION[]='Hi' instead of $_SESSION["Greet"]='Hi'?
I have tested your code. when i started file1.php i have the following:
Array
(
[0] => hi
)
with the following notice: Unknown skipping numeric key 0 in Unknown on line 0
and after that i went on to file2.php i have the following:
Array
(
[0] => there!
)
with the same notice. simply put to answer your question you are wrong :).
If you added the indexes ("greet" and "meet" respectively) to the session variable this would be the output on page 1:
Array
(
[greet]=> hi
)
and when you go on file2.php you would have:
Array
(
[greet] => hi
[meet] => there!
)
file1:
<?php
session_start();
$_SESSION['0'] = 'Hi';
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
That prints
Array
(
[0] => Hi
)
And file2.php that is similar to file1 but different session index
<?php
session_start();
$_SESSION['1'] = 'There!';
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
Now this prints
Array
(
[0] => Hi
[1] => There!
)
Related
I have a variable and when I output it with print_r like this:
print_r($sort_order[$field->name]);
I get this:
Array ( [0] => Array ( [sort_order] => 92 ) )
but I only need the value which is 92. How can I do so it outputs only that when echoing it? example:
echo $sort_order[$field->name];
should output simple
92
Your $sortOrder array is actually an array of arrays, like:
[
[ 'sort_order' => 92 ]
]
That's why you can't print it like you expect.
Try:
echo $sort_order[0]['sort_order'];
Output:
92
The print_r() function is used to print human-readable information about a variable.
You can do both print and echo to output the required value:
echo $sort_order[$field->name];
print $sort_order[$field->name];
Hope this helps.
The command print_r displays the variable in a human readable way. So if you need to know all the info in a variable (in particular for large arrays), then you use that. For other use, e.g. when you only need to know the content (in I guess 99.999% of all the cases) you should either use echo as you already mentioned it or print (althoug, they are more or less the same).
Please consider this links for futher information
http://php.net/manual/en/function.print-r.php
What's the difference between echo, print, and print_r in PHP?
echo "{$line['text_1']}";
the above echo works fine ,however when it comes to 2d array, in my sublime, only {$line['text_2']} this part work fine. output error both sublime and browser
echo "$array_2d[{$line['text_1']}][{$line['text_2']}]";
any idea?
update
echo "$array_2d[$line['text_1']][$line['text_2']]";
using xampp, error Parse error: syntax error, unexpected '[', expecting ']' in C:\xampp\htdocs
and I'm just outputting a value from the mysql_fetch_assoc. I can do it in another way by echo '' however I'm trying to make my code easier for future editting and code copy paste
and yes I'm doing things like
echo "The price is $array_2d[$line['text_1']][$line['text_2']]"
with lots of html code in the double quote.
Why are you trying to output the array?
if it is for debugging purposes, you can just use the native php functions print_r() or var_dump()
You should be able to say
echo "item is {$array_2d[$line['text1']][$line['text2']]}";
to get to a subelement.
Of course, this is only really useful when it's not the only thing in the string. If you're only echoing the one value, you don't need the quotes, and things get simpler.
echo $array_2d[$line['text1']][$line['text2']];
this should work :
echo $array_2d[$line['text_1']][$line['text_2']];
When echoing variables, you don't have to use the quotes:
echo $array_2d[$line['text_1']][$line['text_2']];
If you do need to output something with that string, the concatentation operator can help you:
echo "Array: " . echo $array_2d[$line['text_1']][$line['text_2']];
You can use print_r() to echo the array.
e.g.:
print_r($array);
Output will be:
Array ( [test] => 1 [test2] => 2 [multi] => Array ( [multi] => 1 [multi2] => 2 ) )
Also you can use this to make it more readable in a HTML context:
echo '<pre>';
print_r($array);
echo '</pre>';
Output will be:
Array
(
[test] => 1
[test2] => 2
[multi] => Array
(
[multi] => 1
[multi2] => 2
)
)
You can use print_r() or var_dump() to echo an array.
The print_r() displays information about a variable in a way that's readable by humans whereas the var_dump() function displays structured information about variables/expressions including its type and value.
$array = 'YOUR ARRAY';
echo "<pre>";
print_r($array);
echo "</pre>";
or
$array = 'YOUR ARRAY';
var_dump($array);
Example variations
I'm wondering why you would try using the $line array as a key to access data in $array_2d.
Anyway, try this:
echo($line['text_1'].'<br>');
this:
echo($array_2d['text_1']['text_2'].'<br>');
and finally this (based on your "the $line array provides the keys for the $array_2d" array example)
$key_a = $line['text_1'];
$key_b = $line['text_2'];
echo($array_2d[$key_a][$key_b].'<br>');
Which can also be written shorter like this:
echo($array_2d[$line['text_1']][$line['text_2']].'<br>');
Verifying/Dumping the array contents
To verify if your arrays hold the data you expect, do not use print_r. Do use var_dump instead as it will return more information you can use to check on any issues you think you might be having.
Example:
echo('<pre>');
var_dump($array_2d);
echo('</pre>');
Differences between var_dump and print_r
The var_dump function displays structured information of a variable (or expression), including its type and value. Arrays are explored recursively with values indented to show structure. var_dump also shows which array values and object properties are references.
print_r on the other hand displays information about a variable in a readable way and array values will be presented in a format that shows keys and elements. But you'll miss out on the details var_dump provides.
Example:
$array = array('test', 1, array('two', 'more'));
output of print_r:
Array
(
[0] => test
[1] => 1
[2] => Array
(
[0] => two
[1] => more
)
)
output of var_dump:
array(3) {
[0]=> string(4) "test"
[1]=> int(1)
[2]=> array(2)
{
[0]=> string(3) "two"
[1]=> string(4) "more"
}
}
I've outputted my array to file using the print_r($myArray, true) method, but am having trouble re-importing it as an array.
I keep returning a string with the array, not the array itself. I've tried a few different combinations including print_r and serialize, but can't seem to get it right. What am I missing?
Here's what I have:
$myArray = print_r(file_get_contents($logFile), true);
for reference the log file content looks like so:
Array
(
[0] => Array
(
[0] => blah
[1] => blah
)
...
Thanks
EDIT: Solution - Here is what I came up with:
I changed the file contents to include php tags and declared the array there using var_export instead of print_r.
Here is what I used as my content string when writing to file:
<?php $myArray = '.var_export($myArray, true).'; ?'.'>
From there it was a simple include to get the array back.
You should just store the array as ... an array, like so:
file_put_contents('myfile.txt', '<?php return ' . var_export($array, true) . '; ?>');
Then, to read:
$array = include 'myfile.txt';
See also: var_export()
I am using the following code to get the number of likes on a page.
<?php
$site="http://graph.facebook.com/?ids=http%3a%2f%2fXXXXXXXX/svce.php";
$graph= file_get_contents($site);
$json_string=$graph;
$array = json_decode($json_string, true);
//echo "<pre>";
//print_r($array);
$var = $array['shares'];
echo $var;
?>
But whenever i try to echo out the following code. I always get an unidentified index Notice which is as following: Notice: Undefined index: shares in C:\xampp\htdocs\graphapi.php on line 19
Where am i going wrong?
Here's the print_r output:
Array
(
[http://xxxxxxxxx/svce.php] => Array
(
[id] => http://xxxxxxxxx/svce.php
[shares] => 7
[comments] => 3
)
)
According your print out looks like there is an array more in $array. Try this;
echo $array['http://xxxxxxxxx/svce.php']['shares'];
You have to use the site-name as an key before.
Structure:
- http://example.com
- id
- shares
This means in PHP:
$array["http://example.com/path/to/site"]["shares"];
I have the following array (in php after executing print_r on the array object):
Array (
[#weight] => 0
[#value] => Some value.
)
Assuming the array object is $arr, how do I print out "value". The following does NOT work:
print $arr->value;
print $val ['value'] ;
print $val [value] ;
So... how do you do it? Any insight into WHY would be greatly appreciated! Thanks!
echo $arr['#value'];
The print_r() appears to be telling you that the array key is the string #value.
After quickly checking the docs, it looks like my comment was correct.
Try this code:
print $arr['#value'];
The reason is that the key to the array is not value, but #value.
You said your array contains this :
Array (
[#weight] => 0
[#value] => Some value.
)
So, what about using the keys given in print_r's output, like this :
echo $arr['#value'];
What print_r gives is the couples of keys/values your array contains ; and to access a value in an array, you use $your_array['the_key']
You might want to take a look at the PHP manual ; here's the page about arrays.
Going through the chapters about the basics of PHP might help you in the future :-)