Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

as you must have guessed I'm new to php so please bear with me. At the moment I'm writing some server side scripting and while I've been at it one particular thing is bothering me lately. The situation is as follows. I have two arrays: one with elements and the other with keys (indices) for the elements of the first array. What bothers me is when I'm trying to output an element via double array variables in a string:

echo "$elements[$index[0]]";

I get "Parse error: syntax error, unexpected '[', expecting ']' in C:\xampp\htdocs\test1.php on line 5" error. However if only single array is used as in:

echo "$elements[0]";

It works just fine (outputs a).

I have searched the forums and found little help with this. Also read through What is the difference between single-quoted and double-quoted strings in PHP? played around with single and double quotations but with little success. The workaround I'm using at the moment is that I'm saving the value from $index array into a variable and using it as an index into the $elements array:

$key = $index[0];
echo "$elements[$key]";

Works fine, bus feels like a level of indirection.

The code:

<?php

    $elements   = ["a", "b", "c"];
    $index  = [2 , 1 , 0];

    echo "$elements[$index[0]]";

?>

Error:

Parse error: syntax error, unexpected '[', expecting ']' in C:\xampp\htdocs\test1.php on line 5

Thanks to anyone giving their time to read and answer this.

EDIT: Thank you all for the prompt replies. Now I see that I was too naive thinking that I can simulate my exact problem using a simple example. The actual code I'm dealing with is:

mysql_query( "INSERT INTO pics (filename, date) VALUES( $dirArray[$keys[$index]] , $timestamp )" )
                or die(mysql_error()); 

The error:

Parse error: syntax error, unexpected '[', expecting ']' in C:\xampp\htdocs\dbsetup.php on line 47

The irony is also strong as I have created a level of indirection with my question trying to solve a level of indirection in the code :)

share|improve this question
    
It would be better to start a new question with the material in your edit. You should also use the corresponding mysqli_* functions as mysql_* is deprecated. Note that you are essentially passing a string to mysql_query, so you can construct it in the same way that you would construct as string to be echoed - i.e. "some text " . $variable[$index[0]] . " more text". –  i alarmed alien Sep 27 at 13:11

4 Answers 4

up vote 2 down vote accepted

If you are just echoing a variable, you don't need to use quotes in PHP:

echo $elements[$index[0]];

If you want to add some plain text, you will need to use quotes:

echo "element at index 0: " . $elements[$index[0]] . "\n";

Depending on the version of PHP you are using, you may also encounter an error with using [ ... ] as the array constructor; in PHP 5.3 and earlier, arrays have to be declared using the syntax

$fruits = array('apple', 'orange', 'banana');

whereas in PHP 5.4 onwards, you can use the shorthand

$fruits = ['apple', 'orange', 'banana'];

There is much more information about arrays in the PHP array documentation.

share|improve this answer

You need to correct your php script with this code:

$elements   = array("a", "b", "c");
$index  = array(2 , 1 , 0);

echo $elements[$index[0]];
share|improve this answer

You are not creating an array that's why you are getting error and also you are not printing array properly so change your code

$elements   = array("a", "b", "c");
$index  = array(2 , 1 , 0);

echo $elements[0];
echo $index[0];
echo $elements[$index[0]];
share|improve this answer

You should not use the echo command in conjunction with an array variable if you use the "" instead of parenthesis (). Assign the array construction to a temporary variable before you echo it.

<?php

    $elements   = ["a", "b", "c"];
    $index  = [2 , 1 , 0];
    $value = $elements[$index[0]];

    echo "$value";

?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.