【问题标题】:Accessing the array of Multidimensional array with indexed array使用索引数组访问多维数组的数组
【发布时间】:2019-10-26 04:22:13
【问题描述】:

我想访问这个数组中的 id。我怎样才能访问这个数组索引。

Array
(
    [Magnets] => Array
        (
            [id] => 3
            [product] => Magnets
            [qty] => 2
            [price] => 250
        )

    [Lawn Suits , Summer Collection] => Array
        (
            [id] => 2
            [product] => Lawn Suits , Summer Collection
            [qty] => 1
            [price] => 1000
        )

【问题讨论】:

  • 请指定是否要访问任何特定的 id 或所有的 id。
  • 我要访问数组中的所有记录
  • 使用foreach循环

标签: php arrays multidimensional-array


【解决方案1】:

假设您的数组命名为$results。您可以使用foreach 循环所有值。

foreach( $results as $key => $result) {
    echo $result['id']; //This will give you id
    echo "\n";
}

【讨论】:

    【解决方案2】:

    你可以使用array_column

    $aa = array_column($a, 'id');
    

    演示:https://3v4l.org/biuRr

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 2021-08-19
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      相关资源
      最近更新 更多