dounieyan2036 2017-08-25 13:05
浏览 512
已采纳

在PHP中删除json数组中的键

I have this

[{"cname":"NCPB"},{"cname":"NBK"}]

Which I'm getting from json_encode

on mysql result

How can I achive this ["NCPB","NBK"] From JSON OBJECT ABOVE.

  • 写回答

1条回答 默认 最新

  • dtn51137 2017-08-25 13:13
    关注

    I guess I can answer this without seeing your exact code. Basically, you're currently fetching your query results with something like this and using json_encode on the result of that:

    while ($row = $query->some_fetch_method()) {
        $results[] = $row;               // you're building an array of associative arrays
    }
    echo json_encode($results);
    

    Instead, just fill the results array with that specific column instead of the entire row.

    while ($row = $query->some_fetch_method()) {
        $results[] = $row['cname'];      // you need to build an array of strings instead
    }
    echo json_encode($results);
    

    You should get the array of query results into the format you want before you use json_encode. Otherwise, you'll have to decode the JSON string you just made, reformat the result of that, and reencode it:

     $json = json_encode(array_column(json_decode($json), 'cname'));
    

    or use some weird regex or something. It doesn't seem to make sense to undo what you've already done, though.

    If you don't get what I mean, edit your question to add the code that fetches your query results, and I'll show you an example based on your exact code, but this should give you the general idea.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数