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.

I'm just trying to convert this php object into javascript multidimensional array but it keeps on getting this error.

Parse error: syntax error, unexpected '[', expecting ')' in line 3.

I really need your help. I can't move an inch with my codes. Thank you.

<?php 
    $phpArray = Array ( [0] => 
        Array ( [0] => January [1] => Array ( [0] => Array ( [0] => Medical [1] => 12 ) [1] => Array ( [0] => Financial [1] => 13 ) [2] => Array ( [0] => Burial [1] => 12 ) [3] => Array ( [0] => Financial [1] => 1 ) [4] => Array ( [0] => Burial [1] => 2 ) ) )
        [1] => Array ( [0] => February [1] => Array ( [0] => Array ( [0] => Medical [1] => 12 ) [1] => Array ( [0] => Financial [1] => 13 ) [2] => Array ( [0] => Burial [1] => 12 ) [3] => Array ( [0] => Financial [1] => 1 ) [4] => Array ( [0] => Burial [1] => 2 ) ) )
        [2] => Array ( [0] => March [1] => Array ( [0] => Array ( [0] => Medical [1] => 12 ) [1] => Array ( [0] => Financial [1] => 13 ) [2] => Array ( [0] => Burial [1] => 12 ) [3] => Array ( [0] => Financial [1] => 1 ) [4] => Array ( [0] => Burial [1] => 2 ) ) )
        [3] => Array ( [0] => April [1] => Array ( [0] => Array ( [0] => Medical [1] => 12 ) [1] => Array ( [0] => Financial [1] => 13 ) [2] => Array ( [0] => Burial [1] => 12 ) [3] => Array ( [0] => Financial [1] => 1 ) [4] => Array ( [0] => Burial [1] => 2 ) ) )
        [4] => Array ( [0] => May [1] => Array ( [0] => Array ( [0] => Medical [1] => 12 ) [1] => Array ( [0] => Financial [1] => 13 ) [2] => Array ( [0] => Burial [1] => 12 ) [3] => Array ( [0] => Financial [1] => 1 ) [4] => Array ( [0] => Burial [1] => 2 ) ) )
        [5] => Array ( [0] => June [1] => Array ( [0] => Array ( [0] => Medical [1] => 12 ) [1] => Array ( [0] => Financial [1] => 13 ) [2] => Array ( [0] => Burial [1] => 12 ) [3] => Array ( [0] => Financial [1] => 1 ) [4] => Array ( [0] => Burial [1] => 2 ) ) )
        [6] => Array ( [0] => July [1] => Array ( [0] => Array ( [0] => Medical [1] => 12 ) [1] => Array ( [0] => Financial [1] => 13 ) [2] => Array ( [0] => Burial [1] => 12 ) [3] => Array ( [0] => Financial [1] => 1 ) [4] => Array ( [0] => Burial [1] => 2 ) ) )
        [7] => Array ( [0] => August [1] => Array ( [0] => Array ( [0] => Medical [1] => 12 ) [1] => Array ( [0] => Financial [1] => 13 ) [2] => Array ( [0] => Burial [1] => 12 ) [3] => Array ( [0] => Financial [1] => 1 ) [4] => Array ( [0] => Burial [1] => 2 ) ) )
        [8] => Array ( [0] => September [1] => Array ( [0] => Array ( [0] => Medical [1] => 12 ) [1] => Array ( [0] => Financial [1] => 13 ) [2] => Array ( [0] => Burial [1] => 12 ) [3] => Array ( [0] => Financial [1] => 1 ) [4] => Array ( [0] => Burial [1] => 2 ) ) )
        [9] => Array ( [0] => October [1] => Array ( [0] => Array ( [0] => Medical [1] => 12 ) [1] => Array ( [0] => Financial [1] => 13 ) [2] => Array ( [0] => Burial [1] => 12 ) [3] => Array ( [0] => Financial [1] => 1 ) [4] => Array ( [0] => Burial [1] => 2 ) ) )
        [10] => Array ( [0] => November [1] => Array ( [0] => Array ( [0] => Medical [1] => 12 ) [1] => Array ( [0] => Financial [1] => 13 ) [2] => Array ( [0] => Burial [1] => 12 ) [3] => Array ( [0] => Financial [1] => 1 ) [4] => Array ( [0] => Burial [1] => 2 ) ) )
        [11] => Array ( [0] => December [1] => Array ( [0] => Array ( [0] => Medical [1] => 12 ) [1] => Array ( [0] => Financial [1] => 13 ) [2] => Array ( [0] => Burial [1] => 12 ) [3] => Array ( [0] => Financial [1] => 1 ) [4] => Array ( [0] => Burial [1] => 2 ) ) ) );
?>

<script type="text/javascript">

    var jArray= <?php echo json_encode($phpArray ); ?>;

    for(var i=0;i<6;i++){
        alert(jArray[i]);
    }

 </script>
share|improve this question
1  
That's not a PHP object. What you've got there is a string representation of a PHP array (from the output of print_r() by the look of it). You'll need the actual array in order to run it through json_encode() –  Phil May 16 at 0:46
    
Do you need a one time static conversion? .. –  Daniel May 16 at 1:12
    
@Daniel ,yes. yes. –  ssie_28 May 16 at 1:17

2 Answers 2

I don't know if it will do you any good or what you plan with it, but here is the php array derived from that dump in your post.

<?php
$phparray = array(0 => array(
        0 => "January",
        1 => array(
            0 => array(
                0 => "Medical",
                1 => 12
            ),
            1 => array(
                0 => "Financial",
                1 => 13
            ),
            2 => array(
                0 => "Burial",
                1 => 12
            ),
            3 => array(
                0 => "Financial",
                1 => 1
            ),
            4 => array(
                0 => "Burial",
                1 => 2
            )
        ),
        1 => array(
            0 => "February",
            1 => array(
                0 => array(
                    0 => "Medical",
                    1 => 12
                ),
                1 => array(
                    0 => "Financial",
                    1 => 13
                ),
                2 => array(
                    0 => "Burial",
                    1 => 12
                ),
                3 => array(
                    0 => "Financial",
                    1 => 1
                ),
                4 => array(
                    0 => "Burial",
                    1 => 2
                )
            )
        ),
        2 => array(
            0 => "March",
            1 => array(
                0 => array(
                    0 => "Medical",
                    1 => 12
                ),
                1 => array(
                    0 => "Financial",
                    1 => 13
                ),
                2 => array(
                    0 => "Burial",
                    1 => 12
                ),
                3 => array(
                    0 => "Financial",
                    1 => 1
                ),
                4 => array(
                    0 => "Burial",
                    1 => 2
                )
            )
        ),
        3 => array(
            0 => "April",
            1 => array(
                0 => array(
                    0 => "Medical",
                    1 => 12
                ),
                1 => array(
                    0 => "Financial",
                    1 => 13
                ),
                2 => array(
                    0 => "Burial",
                    1 => 12
                ),
                3 => array(
                    0 => "Financial",
                    1 => 1
                ),
                4 => array(
                    0 => "Burial",
                    1 => 2
                )
            )
        ),
        4 => array(
            0 => "May",
            1 => array(
                0 => array(
                    0 => "Medical",
                    1 => 12
                ),
                1 => array(
                    0 => "Financial",
                    1 => 13
                ),
                2 => array(
                    0 => "Burial",
                    1 => 12
                ),
                3 => array(
                    0 => "Financial",
                    1 => 1
                ),
                4 => array(
                    0 => "Burial",
                    1 => 2
                )
            )
        ),
        5 => array(
            0 => "June",
            1 => array(
                0 => array(
                    0 => "Medical",
                    1 => 12
                ),
                1 => array(
                    0 => "Financial",
                    1 => 13
                ),
                2 => array(
                    0 => "Burial",
                    1 => 12
                ),
                3 => array(
                    0 => "Financial",
                    1 => 1
                ),
                4 => array(
                    0 => "Burial",
                    1 => 2
                )
            )
        ),
        6 => array(
            0 => "July",
            1 => array(
                0 => array(
                    0 => "Medical",
                    1 => 12
                ),
                1 => array(
                    0 => "Financial",
                    1 => 13
                ),
                2 => array(
                    0 => "Burial",
                    1 => 12
                ),
                3 => array(
                    0 => "Financial",
                    1 => 1
                ),
                4 => array(
                    0 => "Burial",
                    1 => 2
                )
            )
        ),
        7 => array(
            0 => "August",
            1 => array(
                0 => array(
                    0 => "Medical",
                    1 => 12
                ),
                1 => array(
                    0 => "Financial",
                    1 => 13
                ),
                2 => array(
                    0 => "Burial",
                    1 => 12
                ),
                3 => array(
                    0 => "Financial",
                    1 => 1
                ),
                4 => array(
                    0 => "Burial",
                    1 => 2
                )
            )
        ),
        8 => array(
            0 => "September",
            1 => array(
                0 => array(
                    0 => "Medical",
                    1 => 12
                ),
                1 => array(
                    0 => "Financial",
                    1 => 13
                ),
                2 => array(
                    0 => "Burial",
                    1 => 12
                ),
                3 => array(
                    0 => "Financial",
                    1 => 1
                ),
                4 => array(
                    0 => "Burial",
                    1 => 2
                )
            )
        ),
        9 => array(
            0 => "October",
            1 => array(
                0 => array(
                    0 => "Medical",
                    1 => 12
                ),
                1 => array(
                    0 => "Financial",
                    1 => 13
                ),
                2 => array(
                    0 => "Burial",
                    1 => 12
                ),
                3 => array(
                    0 => "Financial",
                    1 => 1
                ),
                4 => array(
                    0 => "Burial",
                    1 => 2
                )
            )
        ),
        10 => array(
            0 => "November",
            1 => array(
                0 => array(
                    0 => "Medical",
                    1 => 12
                ),
                1 => array(
                    0 => "Financial",
                    1 => 13
                ),
                2 => array(
                    0 => "Burial",
                    1 => 12
                ),
                3 => array(
                    0 => "Financial",
                    1 => 1
                ),
                4 => array(
                    0 => "Burial",
                    1 => 2
                )
            )
        ),
        11 => array(
            0 => "December",
            1 => array(
                0 => array(
                    0 => "Medical",
                    1 => 12
                ),
                1 => array(
                    0 => "Financial",
                    1 => 13
                ),
                2 => array(
                    0 => "Burial",
                    1 => 12
                ),
                3 => array(
                    0 => "Financial",
                    1 => 1
                ),
                4 => array(
                    0 => "Burial",
                    1 => 2
                )
            )
        )
    ));
?>
share|improve this answer

If this is your actual code, you will need to put quotes around the values you are trying to insert:

<?php 
    $phpArray = 
    Array ( [0] => 
        Array ( [0] => January, [1] => 
                   // ^^ This needs quotes!
                   // You also need to put a comma after each element.
            Array ( [0] => 
                Array ( [0] => Medical, [1] => 12 ) 

To put string values inside an array, you need to put them inside quotes (assuming they are not constants - which I would doubt.

Array ( [0] => 'Medical', [1] => 12 ) 

or

Array ( [0] => "Medical", [1] => 12 ) 

or

$var='Medical';
$something=Array ( [0] => $var, [1] => 12 ) ;
share|improve this answer
    
It needs more than just quotes. –  scrowler May 16 at 0:51
    
@scrowler This is true, but with quotes, the parse error should be solved - assuming the right number of close brackets are there - which I am not going to check :) –  Fluffeh May 16 at 0:53
1  
What about commas? –  scrowler May 16 at 0:53
    
@scrowler LMAO< yes. Indeedy. I need coffee... +1 –  Fluffeh May 16 at 0:55
    
it still returns the same error. thanks for your answer, :) –  ssie_28 May 16 at 0:56

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.