【问题标题】:echo specific part of array in php在php中回显数组的特定部分
【发布时间】:2017-06-07 05:12:54
【问题描述】:

我还在学习 php 数组,我正在尝试回显一个

array(8) { [0]=> string(9) "site_name" [1]=> string(3) "url" [2]=> string(5) "title" [3]=> string(11) "description" [4]=> string(4) "type" [5]=> string(5) "image" [6]=> string(11) "image:width" [7]=> string(12) "image:height" } NULL site_name => Playbuzzurl => http://www.playbuzz.com/jordonr10/what-modern-societal-archetype-fits-your-personalitytitle => What Modern Societal Archetype Fits Your Personality?description => It's 2017, so it's about time we update the books!type => articleimage => http://cdn.playbuzz.com/cdn/aef166e1-1574-4d57-9823-3f38f30bcfc4/810f1d3e-0a97-4797-8235-b4c988562a1c.jpgimage:width => 1600image:height => 842

array(8) { [0]=> string(9) "site_name" [1]=> string(3) "url" [2]=> 字符串(5)“标题”[3]=>字符串(11)“描述”[4]=>字符串(4) “类型”[5]=> 字符串(5)“图像”[6]=> 字符串(11)“图像:宽度”[7]=> 字符串(12)“图像:高度”} NULL 站点名称 => Playbuzzurl => http://www.playbuzz.com/jordonr10/what-modern-societal-archetype-fits-your-personalitytitle => 什么样的现代社会原型适合你的个性?说明 => 现在是 2017 年,是时候更新书籍了!type => articleimage => http://cdn.playbuzz.com/cdn/aef166e1-1574-4d57-9823-3f38f30bcfc4/810f1d3e-0a97-4797-8235-b4c988562a1c.jpgimage:width => 1600 图像:高度 => 842

我试图回显description 的值,即It's 2017, so it's about time we update the books!

我尝试了几种不同的方法,但没有任何效果。非常感谢任何帮助。

echo $array[0]['description'];      // returns nothing
var_dump($key);                     // returns string(12) "image:height"

当前代码

<?php
require_once('OpenGraph.php');

$graph = OpenGraph::fetch('http://www.playbuzz.com/jordonr10/what-modern-societal-archetype-fits-your-personality');
var_dump($graph->keys());
var_dump($graph->schema);

foreach ($graph as $key => $value) {
    echo "$key => $value<br><hr>";
}
echo "Value Below<hr>";
echo $array['description'];

?>

【问题讨论】:

  • 描述的值在你的数组之外。您需要先将其放入数组中,然后才能获取它。
  • 显示您在此处使用的代码。

标签: php arrays multidimensional-array echo


【解决方案1】:

从您的代码看来,如果密钥已经存在,您可以直接从$graph 获取描述值。 你也想在这里显示$array['description']; 我可以看到 $array 是在任何地方定义的

尝试

 echo $graph['description']; // if $graph is array

echo $graph->description; // If its an object

【讨论】:

  • 它是一个对象。这就解释了为什么我尝试的一切都失败了。您如何区分上面的对象和数组之间的区别?再次感谢!
  • 使用var_dump($graph);结果会告诉你是对象还是数组
【解决方案2】:

只要打电话

echo $array[3];

或者使用可以调用的关联数组

echo $array['description'];

http://php.net/manual/en/language.types.array.php

【讨论】:

  • 您好 justyand,感谢您提供帮助。我也尝试过这两种方法,但都不起作用。还有其他想法吗?
  • 没问题。首先放置您的代码。然后我们可以谈谈故障。基本上先定义一个数组,然后调用 echo
  • @Mr.B 您的问题没有意义,请阅读您问题下的第一条评论。
  • 所以从代码中很明显 $array 没有定义你必须使用 $graph
猜你喜欢
  • 1970-01-01
  • 2015-01-16
  • 1970-01-01
  • 1970-01-01
  • 2012-09-05
  • 1970-01-01
  • 2019-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多