javascript - "Array to string conversion error"尝试获取字段值时

标签 javascript php jquery datatables octobercms

我正在尝试将数据表作为服务器端处理。它以某种方式起作用。 我想将字段传递给 ajax 文件并使用它们来获取数据。我将字段名称设为字符串,以使用分隔符分隔它们。然后我传递了字符串并获取它并将它们作为一个数组。 问题是,当我尝试通过它们获取值时,会显示“数组到字符串转换错误”。

October CMS 中的 PHP block

<?php

function onStart()
{
    $this['tableName'] = 'BYAPPS_apps_data';
    $this['fields'] = "app_id|app_name|app_ver|byapps_ver";
}
?>

October CMS 中的 Javascript(与 php block 相同的页面)

  var table = $('#' + tableId).DataTable( {
      processing: true,
      serverSide: true,
      ajax: {
          url: '/ajax?tb={{ tableName|raw() }}&fd={{ fields|raw() }}',
          type: 'GET',
          error: function(e) {
            console.log(e);
          }
      },
      paging: true,
      pageLength: 50,

Ajax 页面

function onStart()
{
   $table = $_GET['tb'];
   $length = $_GET['length'];
   $start = $_GET['start'];
   $fields = explode("|", $_GET['fd']);

   $result = DB::table($table)->skip($start)->limit($length)->get();
   $data = array();

   foreach($result as $row) {
      $sub_array = array();

      for ($i = 0; $i < count($fields); $i++) {

        echo "<script>console.log('".$fields[$i]."')</script>";
        $sub_array[] = $row->$fields[$i];

      }
      $data[] = $sub_array;
   }

$fields[$i] 显示'app_id', 'app_name'...但是我不知道为什么我不能这样获取值。 $row->$fields[$i] 我该如何解决这个问题?

$result 包含如下这些数据。

object(October\Rain\Support\Collection)#947 (1) { ["items":protected]=> array(50) { [0]=> object(stdClass)#949 (88) { ["idx"]=> int(1) ["o_idx"]=> int(0) ["mem_id"]=> string(20) "epicegirls@naver.com" ["app_id"]=> string(10) "epicegirls" ["recom_id"]=> string(6) "byapps" ["app_cate"]=> string(2) "01" ["app_process"]=> int(8) ["app_name"]=> string(12) "에피스걸" ["service_type"]=> string(3) "biz" ["server_group"]=> string(1) "2" ["apps_type"]=> string(19) "안드로이드+iOS" ["app_os_type"]=> string(7) "android" ["byapps_ver"]=> string(3) "2.1" ["app_ver"]=> string(3) "1.6" ["app_ver_ios"]=> string(3) "1.1" ["app_build"]=> string(0) "" ["app_build_ios"]=> string(0) "" ["app_lang"]=> string(2) "ko" ["packagename"]=> NULL ["bundleid"]=> NULL ["app_android_url"]=> string(66) "http://play.google.com/store/apps/details?id=com.epicegirls.byapps" ["app_ios_url"]=> string(58) "http://itunes.apple.com/kr/app/episeugeol/id648794513?mt=8" ["app_icon"]=> string(14) "1366951826.png" ["noti_gcm"]=> string(39) "AIzaSyB4uXkiTltMx3yjQNo26uyfpopMB8vpd94" ["noti_ios"]=> string(14) "1369015681.pem" ["noti_gcm_num"]=> NULL ["noti_fcm_num"]=> NULL ["noti_ios_cerp"]=> string(10) "byapps2013" ["push_server"]=> string(7) "default" ["developer_account"]=> string(1) "N" ["developer_info"]=> string(2580) "- 개발자아이디: 

最佳答案

试试这个

$sub_array[] = $row->{$fields[$i]};

关于javascript - "Array to string conversion error"尝试获取字段值时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57194782/

相关文章:

javascript - 在 Vue 文件/Vue 组件之间传递数据

javascript - 添加事件处理程序时未定义不是函数

javascript - 克隆表单内容、更改属性、附加到 jQuery 或 vanilla JS 表单

php - 类连接器中的 PDO 实例

php - fatal error : Uncaught Error: Call to undefined function mysql_connect()

javascript - 用 HTML 元素替换文本

javascript - 引导模式 : is not a function

javascript - vue 中的动态路由器路径验证

php - 如何使用相同的 CakePhp 设置开发网站和 API

javascript - 如何判断在表中的 href 中单击了哪个行号?