0

What is the best way to extract the information from the database using get_post_meta?

field name = invoice_details

value = a:20:{s:15:"price_per_night";s:3:"150";s:12:"no_of_nights";s:1:"1";s:23:"price_calulating_nights";s:3:"150";s:22:"price_after_commission";s:3:"150";s:13:"discount_type";s:0:"";s:20:"price_after_discount";s:3:"150";s:16:"discounted_price";s:1:"0";s:19:"discount_percentage";s:0:"";s:17:"total_service_fee";s:2:"60";s:11:"total_price";s:3:"210";s:8:"currency";s:3:"GBP";s:7:"post_id";s:2:"21";s:8:"check_in";s:10:"02-07-2017";s:9:"check_out";s:10:"03-07-2017";s:6:"guests";s:1:"1";s:16:"security_deposit";s:3:"100";s:10:"secret_key";s:30:"FfmtXmaPOKbrS8B1XLugufiD3Hdjxl";s:10:"updated_at";s:19:"2017-06-25 17:04:33";s:18:"booking_request_by";i:2;s:17:"step_2_attributes";a:9:{s:9:"firstname";s:3:"Tom";s:8:"lastname";s:5:"Smith";s:14:"street_address";s:3:"229";s:11:"postal_code";s:8:"YW11 3DU";s:4:"city";s:6:"London";s:7:"country";s:14:"United Kingdom";s:5:"phone";s:10:"0799999999";s:11:"guest_fname";a:1:{i:1;s:5:"Bella";}s:11:"guest_lname";a:1:{i:1;s:4:"King";}}}

For example how could I retrieve the "firstname" field as "James"?

I am currently using:

$billing_firstname = get_post_meta( $post->ID, $invoice_details['firstname'] , true );

Any help will be much appreciated.

Thank you!

Unserialized value:

array (
  'price_per_night' => '150',
  'no_of_nights' => '1',
  'price_calulating_nights' => '150',
  'price_after_commission' => '150',
  'discount_type' => '',
  'price_after_discount' => '150',
  'discounted_price' => '0',
  'discount_percentage' => '',
  'total_service_fee' => '60',
  'total_price' => '210',
  'currency' => 'GBP',
  'post_id' => '21',
  'check_in' => '02-07-2017',
  'check_out' => '03-07-2017',
  'guests' => '1',
  'security_deposit' => '100',
  'secret_key' => 'FfmtXmaPOKbrS8B1XLugufiD3Hdjxl',
  'updated_at' => '2017-06-25 17:04:33',
  'booking_request_by' => 2,
  'step_2_attributes' => 
  array (
    'firstname' => 'Tom',
    'lastname' => 'Smith',
    'street_address' => '229',
    'postal_code' => 'YW11 3DU',
    'city' => 'London',
    'country' => 'United Kingdom',
    'phone' => '0799999999',
    'guest_fname' => 
    array (
      1 => 'Bella',
    ),
    'guest_lname' => 
    array (
      1 => 'King',
    ),
  ),
)

1 Answer 1

1

I tried online unserializer tool, but failed to interpret your value, because it's broken.

Anyway, the way to get value is like below:

// Get the value from db first
$meta_value = get_post_meta( $post->ID, 'invoice_details' , true ); //'invoice_details' is a string, not a variable

// Get to the desired value
$desired_value = $meta_value['array_key']; // value from first step of array

or:

$desired_value = $meta_value['array_key']['array_key_second']; // value from second step of array

You should var_dump($meta_value) after having $meta_value to get the idea how to proceed with the data.

Edit

I simply organized your array like below:

<?php
array (
    'price_per_night' => '150',
    'no_of_nights' => '1',
    'price_calulating_nights' => '150',
    'price_after_commission' => '150',
    'discount_type' => '',
    'price_after_discount' => '150',
    'discounted_price' => '0',
    'discount_percentage' => '',
    'total_service_fee' => '60',
    'total_price' => '210',
    'currency' => 'GBP',
    'post_id' => '21',
    'check_in' => '02-07-2017',
    'check_out' => '03-07-2017',
    'guests' => '1',
    'security_deposit' => '100',
    'secret_key' => 'FfmtXmaPOKbrS8B1XLugufiD3Hdjxl',
    'updated_at' => '2017-06-25 17:04:33',
    'booking_request_by' => 2,
    'step_2_attributes' => array (
        'firstname' => 'Tom',
        'lastname' => 'Smith',
        'street_address' => '229',
        'postal_code' => 'YW11 3DU',
        'city' => 'London',
        'country' => 'United Kingdom',
        'phone' => '0799999999',
        'guest_fname' => array (
            1 => 'Bella',
        ),
        'guest_lname' => array (
            1 => 'King',
        ),
    ),
);

And it itself giving you the answer. Yes, just get to the nested keys:

$firstname = $meta_value['step_2_attributes']['firstname'];

If the 'firstname' is set you will get the First Name.

For Guest First Name:

$guestFirstname = $meta_value['step_2_attributes']['guest_fname'][1];
3
  • Sorry to ask, how do I know what the array_key for firstname? I have used the online unserializer to interpret value, please see the original description
    – BSZ
    Jun 25, 2017 at 17:10
  • I have updated the value which can be interpretted when using online unserializer tool
    – BSZ
    Jun 25, 2017 at 17:18
  • Updated my answer Jun 27, 2017 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.