0

I need to create an array in php and then to assign to java script function.I need something like this I need this array as same as it looks

$users = array(
array('username' => 'user1', 'email' => '[email protected]'),
array('username' => 'user2', 'email' => '[email protected]'),
array('username' => 'user3', 'email' => '[email protected]'),
...
);

For $users array i tried this code

 $users = array();
while($rw = $objApp->dbrow($res))
{
 $users []=array('username'=>$rw['username'],
      'email'=>$rw['email']
);
}

Is this OK?

If yes then i want to pass the same array to javascript function...here is what i tried

  objJS::importUsers( <?php echo json_encode($results) ?>);

This is simple,what is the issue in my code due to which it is not working

5
  • 2
    what is objJS::importUsers
    – srain
    Commented Jul 21, 2013 at 7:20
  • 2
    objJS::importUsers( <?php echo json_encode($results) ?>); this line is wrong in any way .
    – Arun Killu
    Commented Jul 21, 2013 at 7:28
  • Indeed, the asker is not showing any javascript...
    – luiges90
    Commented Jul 21, 2013 at 7:29
  • Have your problem be solved? If not try to use my code, I have tested.
    – srain
    Commented Jul 21, 2013 at 8:27
  • Not solved yet...can not create json as i need in my question
    – user2244804
    Commented Jul 21, 2013 at 8:30

3 Answers 3

3

If you want to pass arbitrary object or array from PHP to Javascript, you may want to try JSON. (untested)

<?php
    $data = array('whatever');
    $json = json_encode($data);
?>

<script>
    var json = '<?=$json?>';
    var obj = JSON.parse(json);

    // do with your obj.
</script>

PHP json_encode

Javascript JSON.parse

Note that it only works if your object/array does not contain any key/values containing the ' character.


EDIT: It appears that you could have mixed up syntaxes.

Javascript has no :: operator.

I believe that

objJS::importUsers( <?php echo json_encode($results) ?>);

should actually be

objJS.importUsers( <?php echo json_encode($results) ?>);

However, as you don't tell us what objJs::importUsers is, we can just wild guessing. Is it a Javascript function, or a PHP class method, or something else?

If it is a PHP function, I wonder why you would need to pass it to Javascript - just call that method directly!

EDIT So I read the comment from other post here, you want to pass a PHP array to Javascript and appears as an Javascript array. Unfortunately they are not the same. Javascript array keys cannot really be strings (like 'email'), they are actually Javascript objects

3
  • Keep in mind that this does NOT allow interoperability between PHP and JS
    – Cole Tobin
    Commented Jul 21, 2013 at 7:31
  • Should that php <?=> tag be wrapped in quotes?
    – nbrooks
    Commented Jul 21, 2013 at 7:36
  • I do wrap <?=?> in quotes.
    – luiges90
    Commented Jul 21, 2013 at 7:40
1
<?php
    $result =['your entire array']
    echo json_encode($result);
?>

In your javascript!!!

var JS_ARRAY=[];
$.ajax({
            url: "../xyz/xyz.php",
            type: 'POST',
            data: {//your data to be send},
            dataType: "json",
            success: function(data) {
                     console.log(JSON.stringify(data)
                     JS_ARRAY.push(JSON.stringify(data));
}
});
//use this JS_ARRAY in you js function. 
1
  • @Methew:you are making your task difficult by adding objJS::importUsers( <?php echo json_encode($results) ?>);......There is no need to do such thing..your data will reach the js function in a format given by json encode.You can then parse it the way you want.Did you try this code?? Commented Jul 21, 2013 at 9:04
1
    <?php
    $users = array(
        array('username' => 'user1', 'email' => '[email protected]'),
        array('username' => 'user2', 'email' => '[email protected]'),
        array('username' => 'user3', 'email' => '[email protected]'),
    );
    $users_json_str = json_encode($users);
    ?>
    <script type="text/javascript">
    // here is you java script
    // the variable users is the same with the $users in your php code
    var users = <?php echo "$users_json_str";?>;

    // you can access the first username
    var first_user_name = users[0]['username'];
    </script>
6
  • It still now working...here is what i got...var json = '[{"username":"admin","email":"[email protected]"},{"username":"ahmed.a","email":"[email protected]"}]'; SPC.userPrefs =$.parseJSON(json);
    – user2244804
    Commented Jul 21, 2013 at 8:30
  • It looks like as code..i need to view as SPC.userPrefs = $.parseJSON('my user array');
    – user2244804
    Commented Jul 21, 2013 at 8:36
  • var json = '[{"username":"admin","email":"[email protected]"},{"username":"ahmed.a","email":"gg‌​[email protected]"}]' is the $users variable assigned in php.
    – srain
    Commented Jul 21, 2013 at 8:42
  • My question i very simple,i need $users array as same as i shown in my question..and then i want to pass this array to javascript function...how to do this?
    – user2244804
    Commented Jul 21, 2013 at 8:48
  • :My 90% problem is solved.can you please only tell me that the user array i am creating in while loop is ok?How to develop array like $users = array( array('username' => 'user1', 'email' => '[email protected]'), array('username' => 'user2', 'email' => '[email protected]'), array('username' => 'user3', 'email' => '[email protected]'), );
    – user2244804
    Commented Jul 22, 2013 at 7:27

Your Answer

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