0

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 :)

1
  • 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". Commented Sep 27, 2014 at 13:11

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

You need to correct your php script with this code:

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

echo $elements[$index[0]];

Comments

0

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]];

Comments

0

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";

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.