如何从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.

这是数组: p>

  Array 
(\  n [0] =>数组
 
(
 [product_details] => {“5f93f983524def3dca464469d2cf9f3e”:{“id”:“110”,“qty”:1,“选项”:“{\”颜色 \“:{\”title \“:\”Color \“,\”value \“:null}}”,“price”:1400,“name”:“Foot Massage”,“tax”:null,“image  “:” HTTP:\ / \ / acme.dev \ /上传\ / product_image \ /product_110_1_thumb.jpg “ ”优惠券“: ”9“, ”_ book_date“: ”2017年4月19日“, ”book_date_name_“:” 星期三 “ ”start_timeslot _“: ”09:00:00“, ”end_timeslot _“: ”10:00:00“, ”has_already_rescheduled“:0, ”打折_“: ”0“, ”ROWID“: ”5f93f983524def3dca464469d2cf9f3e“,” 小计“:1400}} 
)
 
 [1] =>数组
(
 [product_details] => {”2723d092b63885e0d7c260cc007e8b9d“:{”id“:”109“,”qty“:1  ,“选项”:“{\”color \“:{\”title \“:\”Color \“,\”value \“:null}}”,“price”:700,“name”:“身体按摩 “ ”税“:0, ”图像“: ”HTTP:\ / \ / acme.dev \ /上传\ / product_image \ /product_109_1_thumb.jpg“, ”优惠券“: ”“, ”_ book_date“:” 2017-04  -18" , “book_date_name _”: “星期二”, “start_timeslot _”:“0  9时00" 分00秒, “_ end_timeslot”: “10:00:00”, “has_already_rescheduled”:0, “折扣_”: “0”, “ROWID”: “2723d092b63885e0d7c260cc007e8b9d”, “小计”:700}} 
  )
 
 [2] => 数组
(
 [product_details] => {“a3c65c2974270fd093ee8a9bf8ae7d0b”:{“id”:“108”,“qty”:1,“选项”:“{\”color \“:{\”title \“  :\ “颜色\”,\ “价值\”:空}} “ ”价格“:3000, ”名“: ”ALO“, ”税“:0, ”形象“:” HTTP:\ / \ / ACME  .dev \ /上传\ / product_image \ /default.jpg “ ”优惠券“: ”“, ”book_date _“: ”2017年4月21日“, ”book_date_name _“: ”星期五“, ”start_timeslot _“:” 10:00  :00“,”end_timeslot _“:”12:00:00“,”has_already_rescheduled“:0,”discount _“:”0“,”rowid“:”a3c65c2974270fd093ee8a9bf8ae7d0b“,”小计“:3000}} 
)
  
 [3] => 数组
(
 [product_details] => {“a3c65c2974270fd093ee8a9bf8ae7d0b”:{“id”:“108”,“qty”:1,“选项”:“{\”color \“:{\”title \“  :\ “颜色\”,\ “价值\”:空}} “ ”价格“:3000, ”名“: ”ALO“, ”税“:0, ”形象“:” HTTP:\ / \ / ACME  .dev \ /上传\ / product_image \ /default.jpg “ ”优惠券“: ”“, ”_ book_date“: ”2017年4月12日“, ”_ book_date_name“: ”星期三“, ”_ start_timeslot“:” 08:00  :00“,”end_timeslot _“:”10:00:00“,”has_already_rescheduled“:0,”discount _“:”0“,”rowid“:”a3c65c2974270fd093ee8a9bf8ae7d0b“,”小计“:3000}} 
)
  
)
  code>  pre> 
 
 

我需要创建一个包含所有“id”元素值的新简单数组。 p> div>

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
)

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

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

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);

Try this code, live demo

print_r(array_column(array_map(function($v){return current(json_decode($v));},array_column($array, 'product_details')), 'id'));