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

ekqde3dh  于 2023-04-28  发布在  PHP
关注(0)|答案(7)|浏览(156)

此问题已在此处有答案

Get the first element of an array(39个答案)
昨天关门了。
嘿,所有我试图从一个电影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)

任何帮助将是伟大的!

3phpmpom

3phpmpom1#

另一个解决方案:

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

返回第一个数组元素的值,如果数组为空,则返回FALSE。

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

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

cfh9epnr

cfh9epnr2#

你可以指向这个$theMovie['result'][0]['backdrop_path'];的数组,也可以像这样循环遍历它,

foreach($theMovie['results'] as $movie){
   echo $movie['backdrop_path'];
}
gopyfrb3

gopyfrb33#

你可以使用array_shift弹出第一个元素,然后检查它是否有效(如果没有结果或者项目不是数组,array_shift将返回null)。

$data = array_shift($theMovie['results']);
if (null !== $data) {
    // process the first result
}

如果你想迭代所有结果,你可以用array_shift执行foreach循环while循环。

foreach($theMovie['results'] as $result) {
    echo $result['backdrop_path'];
}

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

或者在检查$theMovie['result'][0]实际设置后,按照建议使用$theMovie['result'][0]['backdrop_path'];

uemypmqf

uemypmqf4#

假设所有这些代码都存储在变量$datas中:

$results = $datas['results'];
$theMovie = $results[0];
6mzjoqzu

6mzjoqzu5#

试试看

$yourArray['results'][0]

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

1mrurvl1

1mrurvl16#

要开始学习一点,我建议你用途: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];

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

lyr7nygr

lyr7nygr7#

array_slice($yourArrayVariableName, 0, 1);

相关问题