0

I have an array returned to woocommerce from payment gateway as order comment. I need to access it to get a value.

The array is this:

    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
            )

    )

)

How to get the value of [txId] key?

I have tried:

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

Im getting till [comment_content] but how to enter this

:Payment done, Return data: Array
(

}
Nick
  • 138,499
  • 22
  • 57
  • 95
fikimk
  • 3
  • 1
  • 2
    Possible duplicate of [How can I access an array/object?](https://stackoverflow.com/questions/30680938/how-can-i-access-an-array-object) – Cid Jan 25 '19 at 08:10

2 Answers2

2

Edit my answer. Your comment_content is a string not an array. So you don't have to access it at all you have another nested array below that.

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

Also your approaches are used for objects and not for arrays.

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
  • This won't work because `comment_content` is a string, not an array – Nick Jan 25 '19 at 08:19
  • You are right i edit my answer. there are 2 nested fields in there. i constructed the array locally to see the right structure of it. I got bugged on how can an array field have a string and an array value nest at the same time. Thanks for pointing. – pr1nc3 Jan 25 '19 at 08:27
  • "how can an array field have a string and an array value nest at the same time"? it can't. the entire field is a string. that's why the output of `print_r` displays the way it does in the question. – Nick Jan 25 '19 at 08:31
  • Yes i already edited my answer. I got stuck there thinking for a while :) – pr1nc3 Jan 25 '19 at 08:32
0

Based on the output you have shown us, comment_content is a string, not a structure. You can extract the value of txId from it using preg_match e.g.

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

Output:

edf3354c-af38-49f7-810a-e79234e6604d

You should probably look into how that element is being created so that it can be a structure rather than a string, it will make your life a lot easier in future.

Nick
  • 138,499
  • 22
  • 57
  • 95