获取数组中键的值?

时间:2019-01-25 08:05:49

标签: php arrays

我有一个从付款网关返回到woocommerce的数组作为订单注释。 我需要访问它才能获得价值。

数组是这样的:

    Array
(
[0] => WP_Comment Object
    (
        [comment_ID] => 149
        [comment_post_ID] => 1686
        [comment_author] => 
        [comment_author_email] => 
        [comment_author_url] => 
        [comment_author_IP] => 
        [comment_date] => 2019-01-24 14:30:31
        [comment_date_gmt] => 2019-01-24 12:30:31
        [comment_content] => Payment done, Return data: Array
(
[key] => wc_order_9Do9rqvyn29EP
[uniqueID] => 1548333013414
[lang] => HE
[authNumber] => 4318927
[responseMac] => 
b73efff34b5ba63fa8a281a6acc7fda201fb85f2c432386b70d451efb1ce35dd
[cardToken] => 1090669523792340
[cardExp] => 0220
[personalId] => 314603556
[cardMask] => 432484******2340
[txId] => edf3354c-af38-49f7-810a-e79234e6604d
[numberOfPayments] => 
[firstPayment] => 
[periodicalPayment] => 
[userData5] => 0542167008
[userData4] => user name
[userData1] => yes
)

        [comment_karma] => 0
        [comment_approved] => 1
        [comment_agent] => WooCommerce
        [comment_type] => order_note
        [comment_parent] => 0
        [user_id] => 0
        [children:protected] => 
        [populated_children:protected] => 
        [post_fields:protected] => Array
            (
                [0] => post_author
                [1] => post_date
                [2] => post_date_gmt
                [3] => post_content
                [4] => post_title
                [5] => post_excerpt
                [6] => post_status
                [7] => comment_status
                [8] => ping_status
                [9] => post_name
                [10] => to_ping
                [11] => pinged
                [12] => post_modified
                [13] => post_modified_gmt
                [14] => post_content_filtered
                [15] => post_parent
                [16] => guid
                [17] => menu_order
                [18] => post_type
                [19] => post_mime_type
                [20] => comment_count
            )

    )

)

如何获取[txId]键的值?

我尝试过:

$array[0]->comment_content->txId;
$array[0]->comment_content['txId'];

我要读到[comment_content],但如何输入

:Payment done, Return data: Array
(

}

2 个答案:

答案 0 :(得分:2)

编辑我的答案。您的comment_content是字符串而不是数组。因此,您根本不必访问它,在它下面有另一个嵌套数组。

$array[0][0]['txId'];

您的方法也用于对象而不是数组。

答案 1 :(得分:0)

根据您显示给我们的输出,comment_content是字符串,而不是结构。您可以使用txId例如{p>从中提取preg_match的值

preg_match('/\[txId\] => ([a-f0-9-]+)/', $array[0]->comment_content, $matches);
echo $matches[1];

输出:

edf3354c-af38-49f7-810a-e79234e6604d

您可能应该研究如何创建该元素,以便它可以是结构而不是字符串,这将使您的生活更加轻松。