php数组转js数组

文章描述:

php数组转换成js数组操作

 

php数组

$php_array = array(1,2,3);

js转换

<script type="text/javascript">
var js_array = new Array();

<?php
foreach($php_array as $value) {
    echo "js_array.push('$value');";
}
?>

console.log(js_array);
</script>

打印

[
“1”,
“2”,
“3”
]

 

二维数组

<script>
$(function () {

    var json = '<?=json_encode($employee_lists); ?>';
    console.log(JSON.parse(json))

    var js_array = new Array();

    <?php
    foreach($employee_lists as $key => $value) {
//        echo "js_array.push('$value');";
        echo "js_array.push({id:'$key',name:'$value'});";
    }
    ?>

    console.log(js_array);
})
</script>

 

发布时间:2023/10/16

发表评论