• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

如何从JSON数组

php 来源:Gabrielle 6次浏览

打印单场数据我一直在使用laravel DB门面运行一个查询,它是返回一个JSON数组对象,但是当我想从JSON数组访问单个字段值是显示错误。如何从JSON数组

这是我的查询部分。

$dsale=DB::table('directsales') 
      ->join('clients','directsales.client_id','=','clients.id') 
      ->join('products','directsales.product_id','=','products.id') 
      ->select('clients.client_name','clients.addr','directsales.*','products.name') 
      ->where('directsales.client_id','=',$client_id) 
      ->whereBetween('directsales.issue_date',[$fromdate,$todate]) 
      ->distinct() 
      ->get(); 

      return $dsale; 

这是我的结果。

[{"client_name":"client one","addr":"jdkdhjfkhdkshfds","id":1,"transaction_code":"IN--00000001","client_id":1,"product_id":1,"product_code":"P0007","unitperctn":20,"unitprice":100,"ctn":4,"pcs":5,"total":4500,"deliverd_by":"nazim","issue_date":"2017-12-01","created_at":"2017-11-28 13:55:29","updated_at":"2017-11-28 13:55:33","name":"Dano Instant 2.5 Kg Packet 6"},{"client_name":"client one","addr":"jdkdhjfkhdkshfds","id":2,"transaction_code":"IN--00000002","client_id":1,"product_id":1,"product_code":"P0001","unitperctn":12,"unitprice":100,"ctn":1,"pcs":1,"total":2,"deliverd_by":"majed","issue_date":"2017-12-01","created_at":null,"updated_at":null,"name":"Dano Instant 2.5 Kg Packet 6"},{"client_name":"client one","addr":"jdkdhjfkhdkshfds","id":3,"transaction_code":"IN--00000002","client_id":1,"product_id":4,"product_code":"P0004","unitperctn":12,"unitprice":90,"ctn":1,"pcs":3,"total":6,"deliverd_by":"majed","issue_date":"2017-12-01","created_at":null,"updated_at":null,"name":"All time bread"},{"client_name":"client one","addr":"jdkdhjfkhdkshfds","id":5,"transaction_code":"IN--00000004","client_id":1,"product_id":1,"product_code":"P0001","unitperctn":12,"unitprice":100,"ctn":1,"pcs":0,"total":1,"deliverd_by":"majed","issue_date":"2017-12-12","created_at":null,"updated_at":null,"name":"Dano Instant 2.5 Kg Packet 6"},{"client_name":"client one","addr":"jdkdhjfkhdkshfds","id":6,"transaction_code":"IN--00000005","client_id":1,"product_id":4,"product_code":"P0004","unitperctn":12,"unitprice":90,"ctn":3,"pcs":3,"total":3510,"deliverd_by":"add","issue_date":"2017-12-03","created_at":null,"updated_at":null,"name":"All time bread"},{"client_name":"client one","addr":"jdkdhjfkhdkshfds","id":10,"transaction_code":"IN--00000007","client_id":1,"product_id":4,"product_code":"P0004","unitperctn":12,"unitprice":90,"ctn":1,"pcs":6,"total":1620,"deliverd_by":"majed","issue_date":"2017-11-01","created_at":null,"updated_at":null,"name":"All time bread"}] 

当我打印CLIENT_NAME $ dsale – > [ ‘CLIENT_NAME’] [0]这样它的显示错误。


===========解决方案如下:

在阵列

..你需要先得到索引,那么获取对象..

例如: 如果该项目是一个对象,做这样的..

$dsale[0]->client_name;

或者,如果该项目是一个数组类型,做这样的..

$dsale[0]['client_name']


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)