从Json Encode中删除名称

问题描述 投票:0回答:2

目前我的JSON输出来自以下PHP:

$data['products'][] = array(
                    'product_id'  => $result['product_id'],
                    'thumb'       => $image,
                    'name'        => $result['name'],
                    'description' => $desc,
                    'price'       => $price,
                    'special'     => $special,
                    'tax'         => $tax,  
                );

有了这个($products = json_encode ($data['products']);)产生以下内容:

[{"product_id":"28",
"thumb":"x",
"name":"name",
"description":"abc",
"price":"$123.00",
"special":false,
"tax":"$100.00"}]

是否可以在不修改php“$data['products'][] = array();”的情况下删除名称?我想要实现:

 ["28",
   "x",
   "name",
   "abc",
   "$123.00",
   false,
   "$100.00"]

第一次使用JSON编码,所以任何其他建议将是真棒!

php arrays json
2个回答
3
投票

您可以使用array_map循环遍历您的数组并使用array_values作为回调函数将关联数组转换为简单数组

$arr = array_map('array_values', $data['products'] );
$products = json_encode ($arr);

这将导致:

[["28","x","name","abc","$123.00",false,"$100.00"]] 

Live Example


0
投票

您可以使用array_values获取$data['products']中第一个/唯一条目的值,然后对其进行编码:

$json = json_encode(array_values($data['products'][0]));

这产生了

["28","x","name","abc","$123.00",false,"$100.00"]

Live Example

© www.soinside.com 2019 - 2024. All rights reserved.