简体   繁体   English

PHP如何从此数组打印单个值?

[英]PHP How to print a single value from this Array?

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... 数组([0] => a:39:{s:2:“ id”; s:6:“ 703981”; s:4:“名称”; s:10:“ Bilton Apt”; s:7:“地址”; s:25:“天堂大道哈特大道”; 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 注意:未定义的索引:/ Applications / MAMP / htdocs / wp_test_csv / wp-content / themes / twenty ....等中的名称

i tried with other solutions like: 我尝试了其他解决方案,例如:

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

But none works 但是没有办法

Some one can help me? 有人可以帮我吗?

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. 正如乔恩·斯特林(Jon Stirling)和u_mulder所说,您的数组包含一个序列化的值,并且示例中显示的唯一值是索引0。因此,由于示例字符串被....etc...剪短,所以我只能回答什么是已知的。

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

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

This should do that trick. 这应该做到这一点。

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'];

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']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM