微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 json_encode 中插入函数参数“key”而不是变量“$key1”?

如何解决如何在 json_encode 中插入函数参数“key”而不是变量“$key1”?

$date_range_list['week'][$key1]['option'] :这是一个传递给blade.PHP 模板的关联数组。

下面显示函数一个 JavaScript 函数

function durationClick(btnValue,key,index) {

                    if ( btnValue == "week"){
                        ops = {!!json_encode($date_range_list['week'][$key1]['option']) !!};
                    }else if( btnValue == "month" ){
                        ops = {!!json_encode($date_range_list['month'][$key1]['option']) !!};
                    }else if( btnValue == "halfYear" ){
                        ops = {!!json_encode($date_range_list['halfYear'][$key1]['option']) !!};
                    }else if( btnValue == "year" ){
                        ops = {!!json_encode($date_range_list['year'][$key1]['option']) !!};
                    }else if( btnValue == "all" ){
                        ops = {!!json_encode($question['option']) !!};
                    }

}

解决方法

你不能通过执行这样的 JavaScript 函数来影响 PHP 变量(至少需要一个 AJAX 请求)。

如果可能,尝试将所有数据发送到客户端,并以这种形式使用它。例如

const date_range_list = {!!json_encode($date_range_list) !!};

function durationClick(btnValue,key,index) {
  if ( btnValue == "week"){
    ops = date_range_list.week[key].option;
  }else if( btnValue == "month" ){
    ops = date_range_list.month[key].option;
  }else if( btnValue == "halfYear" ){
    ops = date_range_list.halfYear[key].option;
  }else if( btnValue == "year" ){
    ops = date_range_list.year[key].option;
  }else if( btnValue == "all" ){
    ops = {!!json_encode($question['option']) !!};
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。