1

I have an Array like this one:

Array ( [0] => a:39:{s:2:"id";s:6:"703981";s:4:"name";s:10:"Bilton Apt";s:7:"address";s:25:"Hart Blvd, Paradise Acres";s:3:"zip";s:2:"PO";s:10:"city_hotel";s:11:"Montego ....etc...

And i want to print in the page the "name" value so i wrote these two lines of code:

 $item = get_post_meta($post->ID, '_ihfc_hotel'); 
 echo $item['name'];

But when i load the page i receive this error:

Notice: Undefined index: name in /Applications/MAMP/htdocs/wp_test_csv/wp-content/themes/twenty....etc

i tried with other solutions like:

echo $item[0]['name']; or echo $item[0]->['name']:

But none works

Some one can help me?

2
  • 4
    You're going to need an unserialize somewhere. Commented Sep 13, 2016 at 15:53
  • 1
    Your array contains serialized values. Commented Sep 13, 2016 at 15:54

3 Answers 3

1

You need to unserialize the array, as told in the comments:

$ar = unserialize($item[0]);
echo $ar['name'];

You can put it in a loop to get all values in a multi dimensional array:

foreach($item as $key=>$value){
    $ar[$key] = unserialize($value);
}

and then access it:

echo $ar[0]['name'];
Sign up to request clarification or add additional context in comments.

Comments

1

As Jon Stirling and u_mulder said, your array contains a serialized value and the only one shown in your example is index 0. So due the fact that your example string is cut short with ....etc... I can only answer to what is known.

$data = unserialize($item[0]);
print_r($data);

echo $data['name'] // Bilton Apt

This should do that trick.

1 Comment

Thanks this is what i need.
0

Did you mean:

<?php 

 $name = get_post_meta($post->ID, '_ihfc_hotel', true); 
 echo $name;

Otherwise

<?php 

 $data = get_post_meta($post->ID, '_ihfc_hotel', true); 
 $data = unserialize($data);

 var_dump($data);
 var_dump($data['name']);

1 Comment

According to the wordpress manual of get_post_data() $single is set to false, as presumably there are more values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.