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

时间:2017-05-13 08:49:41

标签: php arrays json multidimensional-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}}
    )

)

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

5 个答案:

答案 0 :(得分:3)

希望这个简单foreach对您有所帮助。

解决方案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);

我们正在使用array_column提取product_details,然后我们使用array_map来迭代$personalDetails,其中包含所有JSON,然后我们正在使用key函数,它将返回数组的第一个键,并通过我们正在访问的那个键id

解决方案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);

<强>输出:

Array
(
    [0] => 110
    [1] => 109
    [2] => 108
    [3] => 108
)

答案 1 :(得分:1)

你可以使用array_map&amp; array_value实现这一目标,

这是一个简单的示例,您需要对其进行修改以满足您的需求:

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

现场演示:https://3v4l.org/koXee

答案 2 :(得分:1)

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

答案 3 :(得分:1)

试试此代码 live demo

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

答案 4 :(得分:0)

你&#34; product_details&#34;似乎是一个JSON字符串。循环遍历您的数组,解码JSON并存储&#34; id&#34;在一个新阵列。