PHP获取第一个数组元素

3

大家好,我正在尝试从电影API获取数据。格式如下:

page => 1
results =>
   0 =>
    adult =>
    backdrop_path => /gM3KKixSicG.jpg
    id => 603
    original_title => The Matrix
    release_date => 1999-03-30
    poster_path => /gynBNzwyaioNkjKgN.jpg
    popularity => 10.55
    title => The Matrix
    vote_average => 9
    vote_count => 328
  1 =>
    adult =>
    backdrop_path => /o6XxGMvqKx0.jpg
    id => 605
    original_title => The Matrix Revolutions
    release_date => 2003-10-26
    poster_path => /sKogjhfs5q3aEG8.jpg
    popularity => 5.11
    title => The Matrix Revolutions
    vote_average => 7.5
    vote_count => 98
 etc etc....

我应该如何获取仅第一个元素 [0] 的数据(比如背景图路径、原始标题等等)?我对 PHP 数组还不熟悉 :)

当然,这是我用来输出数组数据的代码:

 print_r($theMovie)

任何帮助都将是极好的!

3
...->results[0] 没问题吗? - Voitcus
你似乎在自问自答... $theMovie["results"][0] ? - cernunnos
获取数组的第一个元素。 - Steven Koch
6个回答

7

另一种解决方案:

$arr = reset($datas['results']);

返回第一个数组元素的值,如果数组为空则返回FALSE。
或者
$arr = current($datas['results']);

current() 函数只是返回当前由内部指针指向的数组元素的值。它不会移动指针。如果内部指针指向元素列表的末尾或数组为空,则 current() 返回 FALSE。


2
您可以使用这个 $theMovie['result'][0]['backdrop_path']; 来指向数组,或者您可以像这样循环遍历它。
foreach($theMovie['results'] as $movie){
   echo $movie['backdrop_path'];
}

我从上面的代码中没有得到任何数据,但是我使用 $theMovie['result'][0]['backdrop_path']; 却得到了数据? - StealthRT
是的,我漏掉了什么,请再检查一遍。 - believe me

2
你可以使用array_shift来弹出第一个元素,然后检查它是否有效(如果没有结果或项目不是数组,则array_shift将返回null)。
$data = array_shift($theMovie['results']);
if (null !== $data) {
    // process the first result
}

如果想要遍历所有结果,可以使用foreach循环或者while循环与array_shift结合使用。
foreach($theMovie['results'] as $result) {
    echo $result['backdrop_path'];
}

while ($data = array_shift($theMovie['results'])) {
    echo $data['backdrop_path'];
}

或者直接使用已经建议的$theMovie['result'][0]['backdrop_path'];,在检查$theMovie['result'][0]是否设置后使用。


给你点赞,感谢你的帮助,Daren C.! - StealthRT

1
假设所有这些代码都存储在一个变量$datas中:
$results = $datas['results'];
$theMovie = $results[0];

0

尝试

$yourArray['results'][0]

但请记住,当结果数组为空时,这会产生错误。


0

如果想开始学习一点编程,我建议你使用:array_slice

现在你的PHP代码看起来像这样:

$myVariable = [
    "page" => 1,
    "results" => [
        [
            'adult' => 1,
            'backdrop_path' => "/gM3KKixSicG.jpg",
            'id' => 603,
            'original_title' => "The Matrix",
            'release_date' => "1999-03-30",
            'poster_path' => "/gynBNzwyaioNkjKgN.jpg",
            'popularity' => 10.55,
            'title' => "The Matrix",
            'vote_average' => 9,
            'vote_count' => 328,
        ],
        [
            'adult' => 2,
            'backdrop_path' => "/gM3KKixSicG.jpg",
            'id' => 603,
            'original_title' => "The Matrix Revolutions",
            'release_date' => "1999-03-30",
            'poster_path' => "/gynBNzwyaioNkjKgN.jpg",
            'popularity' => 10.55,
            'title' => "The Matrix Revolutions",
            'vote_average' => 9,
            'vote_count' => 328,
        ],
    ],
];

然后在上面你使用:

$newArray = array_slice($myVariable['results'], 0, 1);

上述命令的输出结果如下:
[
  [
    "adult" => 1,
    "backdrop_path" => "/gM3KKixSicG.jpg",
    "id" => 603,
    "original_title" => "The Matrix",
    "release_date" => "1999-03-30",
    "poster_path" => "/gynBNzwyaioNkjKgN.jpg",
    "popularity" => 10.55,
    "title" => "The Matrix",
    "vote_average" => 9,
    "vote_count" => 328,
  ],
]

现在如果你想要得到一个扁平的数组,因为上面的数组仍然是多维的,你只需要使用:

$newArray[0]

输出将会是:

[
  "adult" => 1,
  "backdrop_path" => "/gM3KKixSicG.jpg",
  "id" => 603,
  "original_title" => "The Matrix",
  "release_date" => "1999-03-30",
  "poster_path" => "/gynBNzwyaioNkjKgN.jpg",
  "popularity" => 10.55,
  "title" => "The Matrix",
  "vote_average" => 9,
  "vote_count" => 328,
]

如果您想使用 array_slice 命令返回单行,请在命令的末尾添加索引0:

$newArray = array_slice($myVariable['results'], 0, 1)[0];

希望这可以帮到你:)。干杯。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接