How to deal with the 3D array passed by PHP in HTML

requirement description:
get a three-dimensional array from PHP, and now you want to get the data through JQ/JS.

  1. how do I correctly get an array of {$layer} in the page (no need to display)?
  2. JQ/JS gets the data separately?

HTML

<volist name="layer" id="layer">
    <php> print_r($layer);</php>
</volist>

print results

Array(
    [id] => 2387
    [image] => /Public/upload/users/2018/06-01/5b10f39be1305.jpg
    [layername] => 
    [floorid] => 1
    [icon] => Array
        (
            [0] => Array
                (
                    [posx] => 0.2541
                    [posy] => 0.3444
                    [onegoodscategoryid] => 868
                    [portnum] => 0
                    [portid] => 
                    [image] => /Public/upload/category/2018/04-12/5acee8b14f930.png
                    [iconname] => IC
                    [name] => 
                )

            [1] => Array
                (
                    [posx] => 0.684
                    [posy] => 0.5306
                    [onegoodscategoryid] => 868
                    [portnum] => 0
                    [portid] => 
                    [image] => /Public/upload/category/2018/04-12/5acee8b14f930.png
                    [iconname] => IC
                    [name] => 
                )

        )
)
Array(
    [id] => 2388
    [image] => /Public/upload/users/2018/06-01/5b10f3d576921.jpg
    [layername] => 
    [floorid] => 2
    [icon] => Array
        (
            [0] => Array
                (
                    [posx] => 0.3859
                    [posy] => 0.474
                    [onegoodscategoryid] => 950
                    [portnum] => 0
                    [portid] => 
                    [image] => /Public/upload/category/2018/04-17/5ad565ce07a13.png
                    [iconname] => GS
                    [name] => 
                )
        )
)
Mar.16,2021

volist nesting


<script>
var layer = JSON.parse('{:json_encode($layer)}');
</script>

Hello, developer, hey, hey,

first of all, you may need to know that the php variable is assigned to the js variable. Normally, you can go like this:

<?php 

    $str = "val";
 ?>

 <script type="text/javascript">
     var str = "<?php echo $str;?>";
 </script>

in this case, you assign a simple php variable to the js variable; if you ask a question, how do you assign the php array to the js variable? One thing they have in common between the two languages is that they can operate json at the same time (array types cannot be assigned directly because of different languages), which can be changed as follows:

<?php 
    $arr = [
        0 => [
            'name'      => 'xiaoming',
            'girlfrind' => ['xiaohong', 'xiaoli']
        ]
    ];

    // json
    $json_arr = json_encode($arr);
 ?>

 <script type="text/javascript">
    // jsonjson
    var json = "<?php echo $json_arr?>";
    // json
    //  arrjs
    var arr = JSON.parse( json );
 </script>

so you can manipulate the js array with js/jq;

Note :

  1. when the php variable is assigned to the js variable, be sure to put quotation marks; without quotation marks, js will report a fatal error when the php variable is empty.
  2. assignment must be performed under .php file type; if it is a .js file type, php code cannot be executed. Js does not support php language, but php files support js language;
Menu