drduh44480 2017-05-13 08:49
浏览 204
已采纳

如何从PHP中的多维JSON字符串数组中获取元素的值

This is the array:

Array
(
[0] => Array

   (
        [product_details] => {"5f93f983524def3dca464469d2cf9f3e":{"id":"110","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":1400,"name":"Foot Massage","tax":null,"image":"http:\/\/acme.dev\/uploads\/product_image\/product_110_1_thumb.jpg","coupon":"9","book_date_":"2017-04-19","book_date_name_":"wed","start_timeslot_":"09:00:00","end_timeslot_":"10:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"5f93f983524def3dca464469d2cf9f3e","subtotal":1400}}
    )

[1] => Array
    (
        [product_details] => {"2723d092b63885e0d7c260cc007e8b9d":{"id":"109","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":700,"name":"Body Massage","tax":0,"image":"http:\/\/acme.dev\/uploads\/product_image\/product_109_1_thumb.jpg","coupon":"","book_date_":"2017-04-18","book_date_name_":"tue","start_timeslot_":"09:00:00","end_timeslot_":"10:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"2723d092b63885e0d7c260cc007e8b9d","subtotal":700}}
    )

[2] => Array
    (
        [product_details] => {"a3c65c2974270fd093ee8a9bf8ae7d0b":{"id":"108","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":3000,"name":"Alo","tax":0,"image":"http:\/\/acme.dev\/uploads\/product_image\/default.jpg","coupon":"","book_date_":"2017-04-21","book_date_name_":"fri","start_timeslot_":"10:00:00","end_timeslot_":"12:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"a3c65c2974270fd093ee8a9bf8ae7d0b","subtotal":3000}}
    )

[3] => Array
    (
        [product_details] => {"a3c65c2974270fd093ee8a9bf8ae7d0b":{"id":"108","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":3000,"name":"Alo","tax":0,"image":"http:\/\/acme.dev\/uploads\/product_image\/default.jpg","coupon":"","book_date_":"2017-04-12","book_date_name_":"wed","start_timeslot_":"08:00:00","end_timeslot_":"10:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"a3c65c2974270fd093ee8a9bf8ae7d0b","subtotal":3000}}
    )

)

What I need is to create a new simple array containing the values from all the "id" elements.

  • 写回答

5条回答 默认 最新

  • douqianbiao4216 2017-05-13 08:58
    关注

    Hope this simple foreach will be helpful for you.

    Solution 1: Try this code snippet here

    $result=array();
    foreach($array as $value)
    {
        $array=  json_decode($value["product_details"],true);
        $result[]=$array[key($array)]["id"];
    }
    print_r($result);
    

    Here we are using array_column to extract product_details then we are using to array_map to iterate over $personalDetails which contain all the JSON's then we are using to key function which will return first key of the array, and through that key we are accessing, its id.

    Solution 2: Try this code snippet here

    <?php
    
    ini_set('display_errors', 1);
    $array = Array
        (
        0 => Array
            (
            "product_details" => '{"5f93f983524def3dca464469d2cf9f3e":{"id":"110","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":1400,"name":"Foot Massage","tax":null,"image":"http:\/\/acme.dev\/uploads\/product_image\/product_110_1_thumb.jpg","coupon":"9","book_date_":"2017-04-19","book_date_name_":"wed","start_timeslot_":"09:00:00","end_timeslot_":"10:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"5f93f983524def3dca464469d2cf9f3e","subtotal":1400}}'
        ),
        1 => Array
            (
            "product_details" => '{"2723d092b63885e0d7c260cc007e8b9d":{"id":"109","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":700,"name":"Body Massage","tax":0,"image":"http:\/\/acme.dev\/uploads\/product_image\/product_109_1_thumb.jpg","coupon":"","book_date_":"2017-04-18","book_date_name_":"tue","start_timeslot_":"09:00:00","end_timeslot_":"10:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"2723d092b63885e0d7c260cc007e8b9d","subtotal":700}}'
        ),
        2 => Array
            (
            "product_details" => '{"a3c65c2974270fd093ee8a9bf8ae7d0b":{"id":"108","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":3000,"name":"Alo","tax":0,"image":"http:\/\/acme.dev\/uploads\/product_image\/default.jpg","coupon":"","book_date_":"2017-04-21","book_date_name_":"fri","start_timeslot_":"10:00:00","end_timeslot_":"12:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"a3c65c2974270fd093ee8a9bf8ae7d0b","subtotal":3000}}'
        ),
        3 => Array
            (
            "product_details" => '{"a3c65c2974270fd093ee8a9bf8ae7d0b":{"id":"108","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":3000,"name":"Alo","tax":0,"image":"http:\/\/acme.dev\/uploads\/product_image\/default.jpg","coupon":"","book_date_":"2017-04-12","book_date_name_":"wed","start_timeslot_":"08:00:00","end_timeslot_":"10:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"a3c65c2974270fd093ee8a9bf8ae7d0b","subtotal":3000}}'
        )
    );
    $personalDetails=  array_column($array, "product_details");
    
    $result=array_map(function($value){
        $array=json_decode($value,true);
        return $array[key($array)]["id"];
    }, $personalDetails);
    print_r($result);
    

    Output:

    Array
    (
        [0] => 110
        [1] => 109
        [2] => 108
        [3] => 108
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongliao6777 2017-05-13 08:56
    关注

    You "product_details" seems to be a JSON string. Loop through your array, decode the JSON and store the "id" in a new array.

    评论
  • dongqie8661 2017-05-13 08:57
    关注

    you may use array_map & array_value to achieve this,

    here is a quick example, and you need to modify it to be fit with your needs :

    $ar = [
        0 => ['product_details' => '{"5f93f983524def3dca464469d2cf9f3e":{"id": 3}}'],
        1 => ['product_details' => '{"2723d092b63885e0d7c260cc007e8b9d":{"id": 8}}'],
        2 => ['product_details' => '{"a3c65c2974270fd093ee8a9bf8ae7d0b":{"id": 5}}'],
        3 => ['product_details' => '{"a3c65c2974270fd093ee8a9bf8ae7d0b":{"id": 1}}'],
    ];
    
    $ar = array_map(function ($value) {
        return array_values(json_decode($value['product_details'], true))[0]['id'];
    }, $ar);
    
    print_r($ar);
    

    live demo : https://3v4l.org/koXee

    评论
  • dongxiao_0528 2017-05-13 09:03
    关注

    use array_column and json_decode

        $new_one = array_column($array,'product_details');
        $new_array=[];
        foreach($new_one as $key=>$row)
        {    
         foreach(json_decode($row,true) as $key1=>$row1)
         {
             $new_array[]=$row1['id'];
         }
    
        }
    
        print_r($new_array);
    
    评论
  • dqbn76906 2017-05-13 09:17
    关注

    Try this code, live demo

    print_r(array_column(array_map(function($v){return current(json_decode($v));},array_column($array, 'product_details')), 'id'));
    
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 如何仅使用递归法改变链表顺序
  • ¥30 频率与占空比均可调的方波发生器
  • ¥15 VB6.0中PICTUREBOX加载本地图片无法显示
  • ¥100 关于游戏app session获取的问题
  • ¥15 MYSQL数据库建表
  • ¥15 爬虫程序爬取TTGChina网站文章代码
  • ¥35 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作。
  • ¥15 如何用下图方法在AMESim中搭建离心泵模型
  • ¥15 C#连接服务器,请求时报Ssl/Tsl未能建立安全通道
  • ¥15 xcode15build的c++ dylib在10.15上不兼容