PHP 中是否有内置方法可以从多维数组中获取数组?

发布于 2024-08-20 22:41:39 字数 220 浏览 3 评论 0原文

(查询数据库时非常有用)。
如果我有一个多暗数组

 [['id'=>1],['id'=>2],['id'=>34],['id'=>67]]

,并且我想要的是 [1,2,34,67]

我知道如何在代码中执行此操作,只需询问 PHP 中是否有内置方法(或可能在 PDO 中)来执行此操作。

(Very useful when querying DB).
If I have a multy dim array

 [['id'=>1],['id'=>2],['id'=>34],['id'=>67]]

and what I want is [1,2,34,67]

I know how to do it in code, just asking if there is a built in way in PHP (or may be in PDO) to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉栀 2024-08-27 22:41:39

我认为你需要 PDO::FETCH_COLUMN fetchAll 模式,返回仅将一列作为数组:

<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Fetch all of the values of the first column */
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
var_dump($result);
?>

来自手册

要返回由结果集中单个列的所有值组成的数组,请指定 PDO::FETCH_COLUMN。您可以使用列索引参数指定所需的列。

I think you need PDO::FETCH_COLUMN mode in fetchAll, which returns only a single column as array:

<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Fetch all of the values of the first column */
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
var_dump($result);
?>

From the manual:

To return an array consisting of all values of a single column from the result set, specify PDO::FETCH_COLUMN. You can specify which column you want with the column-index parameter.

剩一世无双 2024-08-27 22:41:39

一个非常简单的方法是展平多维数组

此代码是从 http: //php.net/manual/en/function.array-values.php

<?php 
  function array_flatten($array, $flat = false) 
  { 
    if (!is_array($array) || empty($array)) return ''; 
    if (empty($flat)) $flat = array(); 

    foreach ($array as $key => $val) { 
      if (is_array($val)) $flat = array_flatten($val, $flat); 
      else $flat[] = $val; 
    } 

    return $flat; 
  } 

  // will get your flattened array
  print_r( array_flatten( $vals ) );
?>

a very easy way is to flatten your multi dimensional array

this code was extracted from http://php.net/manual/en/function.array-values.php

<?php 
  function array_flatten($array, $flat = false) 
  { 
    if (!is_array($array) || empty($array)) return ''; 
    if (empty($flat)) $flat = array(); 

    foreach ($array as $key => $val) { 
      if (is_array($val)) $flat = array_flatten($val, $flat); 
      else $flat[] = $val; 
    } 

    return $flat; 
  } 

  // will get your flattened array
  print_r( array_flatten( $vals ) );
?>
小兔几 2024-08-27 22:41:39

你不能只制作你想要的数组吗?

$old = array( array( 'id'=>1 ), array( 'id'=>2 ), array( 'id'=>34 ), array( 'id'=>67 ) );
$arr = array();
foreach( $old as $item ) {
    $arr[] = $item['id'];
}

//$arr = [ 1, 2, 34, 67 ];

Could you not just make the array you want?

$old = array( array( 'id'=>1 ), array( 'id'=>2 ), array( 'id'=>34 ), array( 'id'=>67 ) );
$arr = array();
foreach( $old as $item ) {
    $arr[] = $item['id'];
}

//$arr = [ 1, 2, 34, 67 ];
柠檬心 2024-08-27 22:41:39

也许array_values(array_values($array));

Maybe array_values(array_values($array));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文