Inserting PHP array into Javascript array

后端 未结 5 430
后悔当初
后悔当初 2021-01-03 11:47

I have an array like this:

Array
(
[0] => Array
    (
        [id] => 9826
        [tag] => \"php\"
    )

[1] => Array
    (
        [id] =>          


        
相关标签:
5条回答
  • 2021-01-03 12:18
    var availableTags = [
    <?php
      $tag_strings = array();
      foreach($Tags as $tag){
            $tag_strings[] = '"'.$tag['tag'].'"';
      }
      echo implode(",", $tag_strings);
      ?>
    ];
    
    0 讨论(0)
  • 2021-01-03 12:24
    var availableTags = [
            <?php
                            foreach($Tags as $tag){
                                  echo '"'.$tag['tag'].'",';
                            }
                        ?>
        ];
    
    0 讨论(0)
  • 2021-01-03 12:26

    Save yourself some lines of code:

    var availableTags = <?php
    function get_tag($value) {
        return $value['tag'];
    }
    echo json_encode(array_map("get_tag", $Tags));
    ?>
    
    0 讨论(0)
  • 2021-01-03 12:27

    Try:

    var availableTags = <?php
    echo json_encode(array_map(create_function('$v','return $v[\'tag\'];'), $Tags));
    ?>;
    
    0 讨论(0)
  • 2021-01-03 12:30
    <?php 
    $arr = array(
    0 => array("id" => 9826, "tag" => "php"),
    
    1 => array("id" => 9680, "tag" => "perl")
    );
    
    $my_array;
    
    foreach($arr as $key=>$val) {
       $my_array[] = $arr[$key]['tag'];
    }
    
    $availableTags = json_encode($my_array);
    echo $availableTags;
    ?>
    
    0 讨论(0)
提交回复
热议问题