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?
Related
Hi I recently came across an example of json_encode function. Very confused about 1 part:
<?php
$runners=array{
'fname'=>5
'you' => 6
};
echo json_encode (array("runners"=>$runners));
?>
Question is, why can't the code on the last row simply be:
echo json_encode ($runners);
Thanks,
First of all, your array declaration is incorrect and you will get a syntax error if you run the code. You should use array(...) not array{...}. And the values need to be comma-separated. For example:
array(
key => value,
key2 => value2,
key3 => value3,
...
)
The following should work:
$runners = array(
'fname' => 5,
'you' => 6
);
echo json_encode($runners);
Output:
{"fname":5,"you":6}
How are these two different
The end result is different for both cases. When you do json_encode(array("runners"=>$runners));, the array is multi-dimensional, and the JSON output will change, too:
{"runners":{"fname":5,"you":6}}
Which one should you use
Depends. In the first array, you are simply creating two keys named fname and you, and in the second, you also add another key, runners, thereby making the array multi-dimensional. If you want that information to be present in the resulting JSON string, you should use the second one. If not, use the first one.
First you have use { in array it is not correct. and you have not have , between array elements.
you can use both. but you have to access 2 json by different ways. depending in your choice.
for first (it should be best choice) echo json_encode ($runners); you have one dimensional array.
$runners=array(
'fname'=>5,
'you' => 6
);
echo json_encode ($runners);
OUTPUT:
{"fname":5,"you":6}
In second you have 2d array.
$runners=array(
'fname'=>5,
'you' => 6
);
echo json_encode (array("runners"=>$runners));
OUTPUT:
{"runners":{"fname":5,"you":6}}
Live Demo : https://eval.in/92104
Firstly your array is wrogly used. It will use small brackets () not curly {}. So your array will become :
$runners=array(
'fname' => 5,
'you' => 6
);
Now when you do json_encode() as: echo json_encode ($runners); you will get the output:
{"fname":5,"you":6}
And if you do : echo json_encode (array("runners"=>$runners)); you will get output:
{"runners":{"fname":5,"you":6}}
I'm just starting out learning PHP, learning from a few sources. I would like to know why the print_r function requires the boolean value.
<?php
$names = array('Jeff','James','Jeremy');
echo '<pre>', print_r($names), '</pre>';
?>
OUTPUT:
Array
(
[0] => Jeff
[1] => James
[2] => Jeremy
)
If i don't include the boolean value, the output comes with an integer "1" at the end of the array. Like this:
Array
(
[0] => Jeff
[1] => James
[2] => Jeremy
)
1
Can anybody help me out with this? Would be greatly appreciated.
Thanks
Specifying the second argument as true means that print_r returns the contents for you to do something with, mostly store in a variable. Specifying it as false, or omitting it, means the contents get printed.
according to the documentation at php.net the boolean suppresses the output and returns it (using output buffers).
at this point of code, this makes no sense, until you want to assign the output of print_r() into a variable.
So these both lines are in fact the same:
echo print_r($names, true);
print_r($names);
see docu for details: http://php.net/manual/en/function.print-r.php
since you just edited your question, here is the answer:
you want to put pre-tags arouond your code. and usually you want to escape html-characters.
so you suppress the output to get it parsed by a call of htmlspecialchars()
echo '<pre>', htmlspecialchars(print_r($names,true)) , '</pre>';
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 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 :-)
foreach ($_GET as $field => $label)
{
$datarray[]=$_GET[$field];
echo "$_GET[$field]";
echo "<br>";
}
print_r($datarray);
This is the output I am getting. I see the data is there in datarray but when
I echo $_GET[$field]
I only get "Array"
But print_r($datarray) prints all the data. Any idea how I pull those values?
OUTPUT
Array (
[0] => Array (
[0] => Grade1
[1] => ln
[2] => North America
[3] => yuiyyu
[4] => iuy
[5] => uiyui
[6] => yui
[7] => uiy
[8] => 0:0:5
)
)
EDIT: When I completed your test, here was the final URL:
http://hofstrateach.org/Roberto/process.php?keys=Grade1&keys=Nathan&keys=North%20America&keys=5&keys=3&keys=no&keys=foo&keys=blat&keys=0%3A0%3A24
This is probably a malformed URL. When you pass duplicate keys in a query, PHP makes them an array. The above URL should probably be something like:
http://hofstrateach.org/Roberto/process.php?grade=Grade1&schoolname=Nathan®ion=North%20America&answer[]=5&answer[]=3&answer[]=no&answer[]=foo&answer[]=blat&time=0%3A0%3A24
This will create individual entries for most of the fields, and make $_GET['answer'] be an array of the answers provided by the user.
Bottom line: fix your Flash file.
Use var_export($_GET) to more easily see what kind of array you are getting.
From the output of your script I can see that you have multiple nested arrays. It seems to be something like:
$_GET = array( array( array("Grade1", "ln", "North America", "yuiyyu", "iuy", "uiyui", "yui","uiy","0:0:5")))
so to get those variables out you need something like:
echo $_GET[0][0][0]; // => "Grade1"
calling echo on an array will always output "Array".
print_r (from the PHP manual) prints human-readable information about a variable.
Use <pre> tags before print_r, then you will have a tree printed (or just look at the source. From this point you will have a clear understanding of how your array is and will be able to pull the value you want.
I suggest further reading on $_GET variable and arrays, for a better understanding of its values
Try this:
foreach ($_GET as $field => $label)
{
$datarray[]=$_GET[$field];
echo $_GET[$field]; // you don't really need quotes
echo "With quotes: {$_GET[$field]}"; // but if you want to use them
echo $field; // this is really the same thing as echo $_GET[$field], so
if($label == $_GET[$field]) {
echo "Should always be true<br>";
}
echo "<br>";
}
print_r($datarray);
It's printing just "Array" because when you say
echo "$_GET[$field]";
PHP can't know that you mean $_GET element $field, it sees it as you wanting to print variable $_GET. So, it tries to print it, and of course it's an Array, so that's what you get. Generally, when you want to echo an array element, you'd do it like this:
echo "The foo element of get is: {$_GET['foo']}";
The curly brackets tell PHP that the whole thing is a variable that needs to be interpreted; otherwise it will assume the variable name is $_GET by itself.
In your case though you don't need that, what you need is:
foreach ($_GET as $field => $label)
{
$datarray[] = $label;
}
and if you want to print it, just do
echo $label; // or $_GET[$field], but that's kind of pointless.
The problem was not with your flash file, change it back to how it was; you know it was correct because your $dataarray variable contained all the data. Why do you want to extract data from $_GET into another array anyway?
Perhaps the GET variables are arrays themselves? i.e. http://site.com?var[]=1&var[]=2
It looks like your GET argument is itself an array. It would be helpful to have the input as well as the output.