dsdioa9545 2014-08-26 10:51
浏览 49
已采纳

如何使用jquery或javascript从php数组中获取值?

I trying to get data from php array and put in java-script variable. Follwoing are the php arrays.

Array Name

Array
(
    [0] => username
    [1] => byusers
)

Array Value

Array
(
    [0] => user
    [1] => 1
)

What I have Try tried

Get php array value in javascript variable

var DATATABLE_SEARCH_NAMES      = new Array( "<?php echo (is_array($DATATABLE_SEARCH_DATA_NAMES)) ? $DATATABLE_SEARCH_DATA_NAMES['names'] : 0;?>");
var DATATABLE_SEARCH_VALUES     = new Array( "<?php echo (is_array($DATATABLE_SEARCH_DATA_VALUE)) ? $DATATABLE_SEARCH_DATA_VALUE['values'] : 0;?>");
  • 写回答

2条回答 默认 最新

  • dongyuan6949 2014-08-26 11:11
    关注

    This should do what you ask, it is just a case of converting the PHP arrays to a form that javascript can understand. You can use json_encode() to do that.

    $DATATABLE_SEARCH_DATA_NAMES = array('username','byusers');
    $DATATABLE_SEARCH_DATA_VALUE = array('user', 1);
    
    $js1 = json_encode($DATATABLE_SEARCH_DATA_NAMES);
    $js2 = json_encode($DATATABLE_SEARCH_DATA_VALUE);
    
    //echo $js1.PHP_EOL;
    //echo $js2.PHP_EOL;
    
    echo "<script>
    ";
    echo 'var names = ' . $js1 . ";
    ";
    echo 'var values = ' . $js2 . ";
    ";
    echo "</script>
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行