You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从PHP生成的JSON输出中移除name字段?

移除JSON中的name字段解决方案

当然可以移除name字段啦!你只需要在PHP构建数组的阶段做调整就行,下面有两种常见的做法供你参考:

方法一:直接不添加name字段到数组里

修改你原本构建产品数组的代码,删掉'name' => $result['name'],这一行即可:

$data['products'][] = array( 
    'product_id' => $result['product_id'], 
    'thumb' => $image, 
    // 移除这一行:'name' => $result['name'], 
    'description' => $desc, 
    'price' => $price, 
    'special' => $special, 
    'tax' => $tax, 
);

方法二:事后用unset()移除name字段

如果因为某些原因你已经先把name字段加入数组了,也可以用unset()函数删掉它再存入$data:

// 先完整构建产品数组
$product = array( 
    'product_id' => $result['product_id'], 
    'thumb' => $image, 
    'name' => $result['name'], 
    'description' => $desc, 
    'price' => $price, 
    'special' => $special, 
    'tax' => $tax, 
);
// 移除name字段
unset($product['name']);
// 再将处理后的数组加入到$data中
$data['products'][] = $product;

调整后再执行json_encode($data['products']),生成的JSON就会自动去掉name字段,最终结果会是:

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

内容的提问来源于stack exchange,提问作者user7837718

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:25:27