PHP 获取第一个数组元素[重复]

问题描述 投票:0回答:6

嘿,我正在尝试从电影 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]的数据(如background_path、original_title等)?我是 PHP 数组新手:)。

当然,这就是我用来输出数组数据的方法:

 print_r($theMovie)

任何帮助都会很棒!

php arrays element
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'];
}

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]


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

希望这有帮助:)。干杯。

© www.soinside.com 2019 - 2024. All rights reserved.