Reputation: 166
I am new to woocomerce Rest API. I am accessing all data from wordpress. Now i want to print the meta data. Api return me data in this json :-
[meta_data] => Array
(
[0] => Array
(
[id] => 53380
[key] => slide_template
[value] => default
)
[1] => Array
(
[id] => 53381
[key] => payment_details
[value] => no data found
)
[2] => Array
(
[id] => 53382
[key] => wf_invoice_number
[value] => CNC04216
)
)
After decoding of json i want to print this meta data. I am trying to print like this
<?php echo $std[$k]['meta_data'][2]->wf_invoice_number; ?>
But may be its not a std Class object... How can i echo the meta data values.
Upvotes: 0
Views: 178
Reputation: 33813
I'm a little unclear what your problem is exactly - it seems that you have the values and the keys mixed up
The data from the question can be represented with this PHP snippet
$data=array(
'meta_data'=>array(
array('id'=>53380,'key'=>'slide_template','value'=>'default'),
array('id'=>53381,'key'=>'payment_details','value'=>'no data found'),
array('id'=>53382,'key'=>'wf_invoice_number','value'=>'CNC04216'),
)
);
To show details from all parts yo can iterate through the top level array like this and access the items within using standard array notation.
foreach( $data['meta_data'] as $arr ){
printf(
'<pre>
id: %s
key: %s
value: %s
</pre>',
$arr['id'],
$arr['key'],
$arr['value']
);
}
This will output:
<pre>
id: 53380
key: slide_template
value: default
</pre>
<pre>
id: 53381
key: payment_details
value: no data found
</pre>
<pre>
id: 53382
key: wf_invoice_number
value: CNC04216
</pre>
To access a specific record by index ( using the quoted index of 2 for instance )
#specific row
echo $data['meta_data'][2]['key']; // outputs: wf_invoice_number
To show the keys from a particular record, again at index 2:
#keys
printf('<pre>%s</pre>',print_r( array_keys( $data['meta_data'][2] ), true ) );
To show the corresponding values at the same index:
#values
printf('<pre>%s</pre>',print_r( array_values( $data['meta_data'][2] ), true ) );
Upvotes: 1