I know in PHP you can echo variables with double-quotes, but if I try to echo a multidimensional array, it doesn't work. Like...
echo "Here's my variable: $array[0][name]";
And that outputs : "Array[name]"
Is there a way to print its value without closing the quotes?
Is there a way to print its value without closing the quotes?
Yes, using Complex (curly) syntax:
echo "Here's my variable: {$array[0]['name']}";
Demo
To print a variable inside double quote use curly braces {}. I cannot find the good documentation for it though.
To dump array values you can use following functions.
print_r — Prints human-readable information about a variable
print_r() displays information about a variable in a way that's
readable by humans.
var_dump — Dumps information about a variable
This function displays structured information about one or more
expressions that includes its type and value. Arrays and objects are
explored recursively with values indented to show structure.
EG:
print_r($array) //or
var_dump($array)
Related
Using PHP, I printed out an array ($result) using print_r. But the format in which the array has been returned is a new one on me. Specifically, how do I retrieve the text found in the field "reviews_review" ("Wow, what a great...") when it's found in this particular format with its multiple values? Example:
stdClass Object
(
[node_reviews_nid] => 5270
[reviews_review] => a:2:{s:5:"value";s:38:"Wow, what a great place. The bestest!";s:6:"format";s:13:"filtered_html";}
Those extra values and curly brackets have got me stumped. If I wanted the node_reviews_nid I could simply use $result->node_reviews_nid" - but doing that to get reviews_review just retrieves too much ie,
a:2:{s:5:"value";s:38:"Wow, what a great place. The bestest!";s:6:"format";s:13:"filtered_html";})
The odd curly brackets you're seeing are the result of PHP's serialize function.
Basically it's intended to convert complex structures like objects, nested arrays and so on into simple strings which are easier and safer to transfer, for example over HTTP. Think of it as JSON representation, but specific to PHP (do not attempt to json_decode() a serialized value).
Also worth noting is that serialize-d string has a maximum length, beyond which it's simply truncated, so avoid using it for very large structures - you'll lose data.
The reverse of that is unserialize. So to read the review's text, you would first have to unserialize the "reviews_review" property, then reference the "value" index of the resulting array.
$review_data = unserialize($result->reviews_review);
$review_text = $review_data['value'];
print($review_text);
The data in reviews_review looks like a serialized array i.e. written using the serialize() function
So you will need to unserialize it using unserialize() before you can use it
$review = unserialize($obj->reviews_review);
print_r($review);
I have some little confusion. I want to access the array as follows.
$_POST['un'];
or
$arr['empno'];
But when I try in double quotes, it gives out a compile time error I tried the following:
echo "welcome $_POST['un']";
in un i saved username by query string which is out of this question i think.. .so i write welcome
i also tried
echo "$array['emp']";
it also gives me an error. Whats the problem?
When using double quotes in php strings you are telling php to interpret the string before printing it.
When using (associative) arrays or methods, you need to use the {} brackets around the value to make it work.
Simple variants, indexed arrays, etc. can be parsed by just using the double quotes, but when using associative arrays and methods, you will need the bracers.
More (and more in depth) info here: Php docs (strings)
echo "Some text... $array['key']"; // Bad
echo "Some text... {$array['key']}"; // Good
echo "Calling $var (simple) and {$var} (complex) is basically the same.";
If you want to do the 'cheapest' type, double quotes is not the way to go, rather go with concatenation and single quotes:
echo 'Some text... ' . $array['key'];
Further adding to Fred's comment, if you would like to use array variable, you can use it like:
echo "welcome $_POST[un]";
This will output the value of array variable
Enclose the references to the array with { and }
echo "welcome {$_POST['un']}";
and
echo "{$array['emp']}";
So I am working with PHP to pass a PHP array over a jQuery Ajax request to another PHP page. This is quite the task. For some reason, my json_encode is returning an array instead of a string, I am not quite sure why. Here is my PHP code:
$new_spreadsheet = nl2br($_POST['spreadsheet']);
$new_spreadsheet = explode('<br />', $new_spreadsheet);
array_shift($new_spreadsheet);
$new_spreadsheet = array_values($new_spreadsheet);
echo json_encode($new_spreadsheet);
I would show you the output, but it is really long. Basically this is outputting a PHP array which consists of each row on the spreadsheet. This is what I want to have, but the problem is that I don't want the quotes and everything in the array. I am pretty sure I need to run json_decode, but when I do that my code returns an error saying that the parameter needs to be a string. I can tell something is not right, but am not quite sure what I need to change. I would appreciate any advice.
Update: At this point, when I try to loop through the array and print each value, the first array index is equal to a double quote like so: ". There are double quotes in random values throughout the area. I am not quite sure about what is causing this.
If I echo the rows from within the json_encoded PHP array onto the console, I get the following output:
"
correct value
correct value
correct value
"
You're using JSON, which means you have to adhere to somewhat more stringent syntax rules than Javascript's. e.g.
<?php
$arr = array('This' => 'is', 'an' => 'array in php');
echo json_encode($array);
?>
output:
{"This":"is","an":"array in PHP"}
There is NO way to avoid getting quotes on the values, as they're a fundamental requirement of JSON (and Javascript). If you don't want quotes, then don't use JSON.
try only br.
$new_spreadsheet = explode("<br>", $new_spreadsheet);
It will work. and json_enode can never return an array. try var_dump and check.Also make sure before you post, use htmlspecialcharacters.
With
$maids = getMaids();
I get $maids to be an array of PHP objects each one with the same structure. Is there any way I can sprintf the values inside of these objects with just two lines of code?
foreach($maids as &$maid)
sprintf($form, $maid->name, $maid->pbx, $maid->id);
Why is the code above outputting nothing? Not even an error in the console.
I want to keep my code as clean as possible and using 3 lines just to assign the contents of the object to a variable and then echo() only that variable is too long. Why my shortcut is not working?
Thanks!
sprintf doesn't output anything, it returns a string that you can then echo to output it; or try printf() instead
Use printf() instead of sprintf().
printf() will output it's results to stdout while sprintf() will return the results as a string what is useful in some cases too
I am a newbie to php. I am working on a existing wordpress site. I want to print the values of a variable $post as output. What is the equivalent of Java's System.out.print in php.
While googling i found the same, system.out.print for php but its not working.
--
Thanks
You can use echo to print something: docs
$_POST is an array, if you want to examine its value for debug purposes use var_dump (docs):
var_dump($_POST);
To access array's elements, use square brackets: $_POST['index']. PHP arrays may be associative, which means they are indexed not only by numbers, but also by strings. $_POST is a pure associative array, indexed only with strings.
To print POST parameter called username use this code:
echo $_POST['username'];
or
echo($_POST['username']);
Both will work because echo is not a function, but a language construct (see docs linked above).
printf("var = %s", $var);
or
echo $var;
or (for objects, arrays, etc)
print_r($_POST);
or
var_dump($_POST);
Check php.net for more!
PHP's echo construct is probably what you want.
<?php echo $_POST['variable']; ?>
where variable is the name of the form element.