0

提前感谢您的帮助!我是 php 的绝对新手,但正在努力相处。

我有一个数组 $result ,其中包含以下内容:

Array
(
    [0] => Array
        (
            [id] => xyz
            [from] => Array
                (
                    [name] => xyz
                    [id] => xyz
                )

            [link] => xyz
            [name] => xyz
            [picture] => xyz
            [created_time] => xyz
        )
[1] => Array
        (
            [id] => xyz
            [from] => Array
                (
                    [name] => xyz
                    [id] => xyz
                )

            [link] => xyz
            [name] => xyz
            [picture] => xyz
            [created_time] => xyz
        )

等等... 。

我想要实现的是一个适用于其中一些信息的 foreach 循环。现在我更改了代码以简单地输出信息,所以我可以看到问题......我想。我的代码是:

foreach($result AS $buildresult) {
  $resobjid = array_column($buildresult, 'id');
  $cardpicurl = "https://graph.facebook.com/$resobjid/picture";
  $heading = array_column($buildresult ['from'], 'name');
  $para = array_column($buildresult, 'name');
  $link = array_column($buildresult, 'link');
  echo '<pre>' . print_r( $resobjid, 1 ) . '</pre><br>';
  echo '<pre>' . print_r( $cardpicurl, 1 ) . '</pre><br>';
  echo '<pre>' . print_r( $heading, 1 ) . '</pre><br>';
  echo '<pre>' . print_r( $para, 1 ) . '</pre><br>';
  echo '<pre>' . print_r( $link, 1 ) . '</pre><br>';
  echo '<pre>' . print_r( $cardpicurl, 1 ) . '</pre><br>';
  echo '<pre>' . print_r( $buildresult, 1 ) . '</pre><br>';
}

我期待它做类似的事情:

The ID

https://graph.facebook.com/someidfromthearray/picture

Array
(
    [0] => Some Name from the Array
)
Array
(
    [0] => Some Name from the above array
)

但我得到的是:

Array
(
    [0] => 387639951329150
)

https://graph.facebook.com/Array/picture

Array
(
)

Array
(
    [0] => Some.Name
)

Array
(
)

https://graph.facebook.com/Array/picture

所以我确实从“来自”数组中获取了 ID 和名称。其余的似乎是空的。尽管在其中正确显示,但也https://graph.facebook.com/$resobjid/picture显示“阵列”echo '<pre>' . print_r( $resobjid, 1 ) . '</pre><br>';

非常感谢任何帮助!

非常感谢!:)

4

1 回答 1

0

你不需要 array_column 来寻址数组,你可以直接做。考虑这段代码:

    $result = array(
                    array('id' => 'id0',
                          'from' => array('name' => 'from_name0','id' => 'from_id0'),
                          'link' => 'link0',
                          'name' => 'name0',
                          'picture' => 'picture0',
                          'created_time' => 'created_time0'
                         ),
                    array('id' => 'id1',
                          'from' => array('name' => 'from_name1','id' => 'from_id1'),
                          'link' => 'link1',
                          'name' => 'name1',
                          'picture' => 'picture1',
                          'created_time' => 'created_time1'
                         )
                   );
    $resobjid = 'SOMETHING';
    foreach($result AS $buildresult)
    {
        $cardpicurl = "https://graph.facebook.com/$resobjid/picture";

        echo "<pre>\n";
        echo 'The ' . $buildresult['id'] . "\n";
        echo $cardpicurl . "\n";
        echo $buildresult['from']['name'] . "\n";
        echo $buildresult['name'] . "\n";
        echo $buildresult['link'] . "\n";
        echo $cardpicurl . "\n";
        echo "</pre>\n<hr>\n";
    }

我更改了数组值以使输出更易于阅读和调试,但我没有更改结构。

我明白了:

在此处输入图像描述

你没有为 $resobjid 指定一个值,所以我设置了一些东西来避免错误。此外, print_r 的第二个参数仅在要将结果分配给某个变量时使用,如下所示:

$output = print_r($array,1);

你在这里不需要它。我希望这会有所帮助,尼克。

于 2017-10-19T16:04:36.233 回答