Php 获取数组中键的值?
我有一个数组作为订单注释从支付网关返回到woocommerce。 我需要访问它以获取值 阵列如下所示:Php 获取数组中键的值?,php,arrays,Php,Arrays,我有一个数组作为订单注释从支付网关返回到woocommerce。 我需要访问它以获取值 阵列如下所示: Array ( [0] => WP_Comment Object ( [comment_ID] => 149 [comment_post_ID] => 1686 [comment_author] => [comment_author_email] => [comme
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'];
我一直到[评论内容],但如何进入这个
:Payment done, Return data: Array
(
}
编辑我的答案。您的注释内容是字符串而不是数组。因此,您根本不必访问它,在它下面还有另一个嵌套数组
$array[0][0]['txId'];
此外,您的方法用于对象而不是数组。编辑我的答案。您的注释内容是字符串而不是数组。因此,您根本不必访问它,在它下面还有另一个嵌套数组
$array[0][0]['txId'];
您的方法也用于对象而不是数组。根据您向我们展示的输出,
注释内容是字符串,而不是结构。您可以使用preg_match
从中提取txId
的值,例如
preg_match('/\[txId\] => ([a-f0-9-]+)/', $array[0]->comment_content, $matches);
echo $matches[1];
输出:
edf3354c-af38-49f7-810a-e79234e6604d
您可能应该了解该元素是如何创建的,以便它可以是一个结构而不是一个字符串,这将使您今后的生活更加轻松。根据您向我们展示的输出,comment\u content
是一个字符串,而不是一个结构。您可以使用preg_match
从中提取txId
的值,例如
preg_match('/\[txId\] => ([a-f0-9-]+)/', $array[0]->comment_content, $matches);
echo $matches[1];
输出:
edf3354c-af38-49f7-810a-e79234e6604d
您可能应该了解该元素是如何创建的,以便它可以是一个结构而不是一个字符串,这将使您今后的生活更加轻松。可能重复的内容不会起作用,因为comment\u content
是一个字符串,而不是数组。您是对的,我编辑了我的答案。其中有两个嵌套字段。我在本地构建了数组,以查看它的正确结构。我对数组字段如何同时具有字符串和数组值嵌套感到困惑。感谢您的指点。“一个数组字段怎么能同时嵌套一个字符串和一个数组值”?不可能。整个字段是一个字符串。这就是为什么print\r
的输出显示问题的方式。是的,我已经编辑了我的答案。我在那里想了一会儿:)这行不通,因为comment\u content
是一个字符串,而不是数组。你说得对,我编辑了我的答案。其中有两个嵌套字段。我在本地构建了数组,以查看它的正确结构。我对数组字段如何同时具有字符串和数组值嵌套感到困惑。感谢您的指点。“一个数组字段怎么能同时嵌套一个字符串和一个数组值”?不可能。整个字段是一个字符串。这就是为什么print\r
的输出显示问题的方式。是的,我已经编辑了我的答案。我被困在那里想了一会儿:)那就行了。谢谢。顺便说一下,我不能控制输出。这样就可以了。谢谢。顺便说一下,我不能控制输出。