1

I'm trying to pass a php array to javascript, but the array has non-sequential keys.

Here is a portion of the array:

Array
(
    [1812] => Array
        (
            [RecID] => 1
            [team_num] => 20
            [player_id] => 1812
            [name] => Brady, Tom
            [id] => 1812
            [tm_abbr] => NE
            [tm_id] => 9003
            [pos] => QB
            [active] => 1
            [ListOrder] => 1
            [lg_pos] => QB
            [NFL_pos] => QB
            [data_type] => off
            [fa_sort_category] => 1
            [lineup_elig_pos] => Array
                (
                    [1] => RB1
                    [2] => RB2
                    [3] => R1
                    [4] => R2
                    [5] => R3
                )

        )

    [15574] => Array
        (
            [RecID] => 1
            [team_num] => 20
            [player_id] => 15574
            [name] => Goff, Jared
            [id] => 15574
            [tm_abbr] => LAR
            [tm_id] => 9025
            [pos] => QB
            [active] => 1
            [ListOrder] => 1
            [lg_pos] => QB
            [NFL_pos] => QB
            [data_type] => off
            [fa_sort_category] => 1
            [lineup_elig_pos] => Array
                (
                    [1] => RB1
                    [2] => RB2
                    [3] => R1
                    [4] => R2
                    [5] => R3
                )

        )

    [16381] => Array
        (
            [RecID] => 1
            [team_num] => 20
            [player_id] => 16381
            [name] => Jackson, Lamar
            [id] => 16381
            [tm_abbr] => BAL
            [tm_id] => 9030
            [pos] => QB
            [active] => 1
            [ListOrder] => 1
            [lg_pos] => QB
            [NFL_pos] => QB
            [data_type] => off
            [fa_sort_category] => 1
            [lineup_elig_pos] => Array
                (
                    [1] => RB1
                    [2] => RB2
                    [3] => R1
                    [4] => R2
                    [5] => R3
                )

        )

I have tried this:

var roster = {};
roster = "<?php echo json_encode($roster, true); ?>";

But I get an error "Uncaught SyntaxError: Unexpected number"

The key is important to match the player id so I can reference it later in the code easily.

1
  • I don't see any jquery in this question ...or Joomla! for that matter. Whenever you have pure php or javascript questions be sure to research at Stackoverflow before posting a question. It just so happens that this is a mega-duplicate question there and would have been very easy to find. For example: pass JSON object from php to javascript in the same script Commented May 31, 2019 at 10:48

1 Answer 1

1

Don't wrap JSON-encoded values in quotes.

var roster = <?php echo json_encode($roster, true); ?>;

Your Answer

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

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