2

I got a multidimensional array in php resulting from an sql query. I am interested how could i use my first value from the array as the name of a new variable in javascript and assign the second value from the array as the value of the newly created variable.

What i had tried it is this

  foreach ($language_array as $value) {


echo "<script> var ".$value[0]." = ". $value[1]."; </script>";

}

but when i try a document.write (variable); it is that is undefined.

 Array
(
    [0] => Array
        (
            [0] => el1
            [1] => Grouping
        )

    [1] => Array
        (
            [0] => el2
            [1] => Type
        )

    [2] => Array
        (
            [0] => el3
            [1] => Starting Date
        )

    [3] => Array
        (
            [0] => el4
            [1] => Ending Date
        )

    [4] => Array
        (
            [0] => el5
            [1] => Section
        )

    [5] => Array
        (
            [0] => el6
            [1] => Cell
        )

    [6] => Array
        (
            [0] => el7
            [1] => Client
        )

    [7] => Array
        (
            [0] => el8
            [1] => Status
        )

    [8] => Array
        (
            [0] => el9
            [1] => Article
        )

    [9] => Array
        (
            [0] => el10
            [1] => Search
        )

)

Thanks

Ervin

3
  • 4
    You might want to include what's in your array and look to see what PHP is actually printing out. Commented Oct 19, 2018 at 18:53
  • Please post array.. Commented Oct 19, 2018 at 18:54
  • Array ( [0] => Array ( [0] => el1 [1] => Grouping ) [1] => Array ( [0] => el2 [1] => Type ) [2] => Array ( [0] => el3 [1] => Starting Date ) [3] => Array ( [0] => el4 [1] => Ending Date ) Commented Oct 19, 2018 at 19:39

3 Answers 3

1

You're writing a script like:

<script> var el1 = Grouping; </script>

This will try to use Grouping as a variable, but you want it to be a literal string.

You should use json_encode() to convert $value[1] to a JavaScript literal. In the case of a PHP string, it will add the necessary quotes.

foreach ($language_array as $value) {
    echo "<script> var ".$value[0]." = ". json_encode($value[1]) ."; </script>";
}

And as the other answer says, there's no need to put each variable in its own script, although I don't think it will actually make much difference in how it's processed.

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

Comments

1
  1. Don't emit separate <script> tag for each variable, instead - emit <script> tags just once before/after your loop

  2. Now as it is - right side of assignment is interpreted as another variable name, not a value - so that's why you get undefined. You are missing quotes for a string variables values

Fixed code :

$language_array = [
         [ 'el1',  '"Grouping"'],
         [ 'el2', '"Type"' ],
         [ 'el3', '"Starting Date"'],
         [ 'el4', '"Ending Date"'],
         [ 'el5', '"Section"']
        ]; 

        echo "<script>";
        foreach ($language_array as $value) {
          echo "var ".$value[0]." = ". $value[1].";";
          echo "document.write({$value[0]}+'<br>');";
        }
        echo "</script>";

2 Comments

i created a local php file only with the code you provided and it is working. When i am using inside my proiect nothing is showed i don't understand the cause.
my array is in different format i think, i had just replaced my resulted array with you example and in that way it is working.
0

Can you use Alert(variable) instead of documenti.write...because when we put document.write() inside a function. When the function is invoked, all HTML elements will be overwritten and replaced with the new, specified text...

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.