darzu
darzu

Reputation: 33

javascript variable declaration from php array value

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

Upvotes: 2

Views: 214

Answers (3)

Barmar
Barmar

Reputation: 781974

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.

Upvotes: 1

Agnius Vasiliauskas
Agnius Vasiliauskas

Reputation: 11277

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

Upvotes: 1

Ferdinando
Ferdinando

Reputation: 964

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...

Upvotes: 0

Related Questions