douwei1128 2016-02-05 08:59
浏览 47

从角度js传递后,在php中检索一个数组

I have an array like this

 $scope.kk =  [
        { name:'Computer Architecture', price:65 }, 
        { name:'Advanced Composite Materials', price:45 }, 
        { name:'Stategies Unplugged', price:43 }, 
        { name:'Teaching Science', price:50 }, 
        { name:'Challenging Times', price:22 }];

I have passed this array to my controller through webapi like this

     var req1= {
                        method: 'POST',
                        url: apiPoint.url + 'sin.php',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                       params :{
                           "fulldet[]" : $scope.kk
                       }
 $http(req1).then(function (response) {
                    if (response.data.status) {
                        alert(JSON.stringify(response.data));
}
}

And in my controller i need to extract this array I have used foreach statement like this

$result['ppp'] = $fulldet;

foreach($fulldet as $e){
                $result['lm']=$e['name'];
            }

But am getting ILLEGAL STRING OFFSET 'name' Please help me.

For my reference i hv printed my array back after the response from controller. I get it like this

{"ppp":["
            {\" name\":\"Computer Architecture\", \"price\":65 }, 
            {\" name\":\"Advanced Composite Materials\", \"price\":45 }, 
            { \"name\":\"Stategies Unplugged\", \"price\":43 }, 
            {\" name\":\"Teaching Science\", \"price\":50 }, 
            {\" name\":\"Challenging Times\", \"price\":22 }"]}
  • 写回答

2条回答 默认 最新

  • douzi1117 2016-02-05 09:02
    关注

    Before trying to iterate over array apply json_decode to it.

    $result['ppp'] = json_decode($fulldet, true);
    
    foreach($fulldet as $e){
        $result['lm']=$e['name'];
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办