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)
I couldn't find any useful examples or help online via google search, and the solution should be simple, but the results are not what I'm expecting.
I have 3 arrays - to list a few:
$A = array(1,0,1,1,1,0,1,1,1);
$B = array(1,1,1,0,1,0,1,0,1);
$C = array(1,1,1,0,1,0,1,1,1,0,1);
And then I wanted to create an array where the elements would be [0]-[2] referencing each of the 3 arrays mentioned above. The declaration for that is:
$LETTERS= array($A,$B,$C);
I'm trying to get the elements of $A[x] with this code:
$LETTERS[0][0];
But the problem is that it outputs: Array[0] instead of 1
If I echo $A[0] the output will be 1 as expected.
So what am I doing wrong? I don't think there is a problem with reserved words &| using uppercase letters as variables, is there?
How can I access data in this 2-dimensional array?
Thanks in advance for any help.
EDIT
The problem was that I was trying to echo the output with this code:
echo "element [0][0]: $LETTERS[0][0]";
For some reason, this wont work. I needed to keep out everything except the actual variable. Anyone know why that's the case?
You need to re-check your code.
echo $LETTERS[0][0];
outputs 1
edited question answer
echo "element [0][0]: {$LETTERS[0][0]}";
Works here:
php > $A = array(1,0,1,1,1,0,1,1,1);
php > $B = array(1,1,1,0,1,0,1,0,1);
php > $C = array(1,1,1,0,1,0,1,1,1,0,1);
php > $LETTERS= array($A,$B,$C);
php > echo $LETTERS[0][0];
1
ok, given your edit... PHP's interpreter isn't greedy. It will not pull in multiple array dimensions when you embed that multidimensional array in a string. e.g
echo "$arr[0][0]"
is seen as
echo "$arr[0]", "[0]"
^^^^--arrray
^^^-random string
Doing echo $array forces the array into string context, but PHP will not dump the array's contents, it will simply print out Array, which is why you got Array[0].
To force PHP to see the entire array reference, you need to use the {} notation:
echo "{$arr[0][0]}";
which will properly handle the extra array dimensions.
Incidentally, this is also useful for objects-in-strings, e.g.
echo "$obj->attribute->attribute"
is better written as
echo "{$obj->attribute->attribute}"
That code is working correctly. Live demo here.
EDIT: After your edition, the correct way to do that would be:
echo "element [0][0]: {$LETTERS[0][0]}";
That way PHP will expand your variable properly.
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"];
In my form i have fields with name photoid[] so that when sent they will automatically be in an array when php accesses them.
The script has been working fine for quite some time until a couple days ago. And as far as i can remember i havent changed any php settings in the ini file and havent changed the script at all.
when i try to retrieve the array using $_POST['photoid'] it returns a string with the contents 'ARRAY', but if i access it using $_REQUEST['photoid'] it returns it correctly as an array. Is there some php setting that would make this occur? As i said i dont remember changing any php settings lately to cause this but i might be mistaken, or is there something else i am missing.
I had the same problem. When I should recieve array via $_POST, but var_dump sad: 'string(5) "Array"'. I found this happens, when you try use trim() on that array!
Double check your code, be sure you're not doing anything else with $_POST!
Raise your error_reporting level to find any potential source. It's most likely that you are just using it wrong in your code. But it's also possible that your $_POST array was mangled, but $_REQUEST left untouched.
// for example an escaping feature like this might bork it
$_POST = array_map("htmlentities", $_POST);
// your case looks like "strtoupper" even
To determine if your $_POST array really just contains a string where you expected an array, execute following at the beginning of your script:
var_dump($_POST);
And following for a comparison:
var_dump(array_diff($_REQUEST, $_POST));
Then verifiy that you are really using foreach on both arrays:
foreach ($_POST["photoid"] as $id) { print $id; }
If you use an array in a string context then you'll get "Array". Use it in an array context instead.
$arr = Array('foo', 42);
echo $arr."\n";
echo $arr[0]."\n";
Array
foo
$_POST['photoid'] is still an array. Just assign it to a variable, and then treat it like an array. ie: $array = $_POST['photoid'];
echo $array[0];
How can I print a $_POST?
Example:
echo $_POST['data'];
This returns nothing...
You can also wrap your code with <pre> tags to make your array prints out nicer instead of just 1 continuous line. A trick that was shown by a member on this site.
<pre>
<?php var_dump($_POST); ?>
</pre>
Your code is correct.
You can use either:
var_dump($_POST);
or
print_r($_POST);
to print out the entire POST array for debugging.
You can only show the values of keys that exist. array_keys() returns an array containing the keys that exist in the array. If there is no output for a key despite the fact that the key exists then the array may contain an empty value for that key.