如何从json数组中打印单个字段数据

时间:2017-12-03 05:23:07

标签: php arrays json laravel

我使用laravel DB facade运行查询,它返回一个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] 时,这会显示错误。

1 个答案:

答案 0 :(得分:0)

数组中的

..你需要先获取索引,然后获取对象..

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

$dsale[0]->client_name;

或者如果该项是数组类型,请执行此操作..

$dsale[0]['client_name']