php 获取多维数组的键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8940825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:49:24  来源:igfitidea点击:

Get key of multidimensional array?

phparrays

提问by Jurijs Gu??ins

For example, I have multidimensional array as below:

例如,我有如下多维数组:

$array = array (
  0 => 
    array (
      'id' => '9',
      'gallery_id' => '2',
      'picture' => '56475832.jpg'
    ),
  1 => 
    array (
      'id' => '8',
      'gallery_id' => '2',
      'picture' => '20083622.jpg'
    ),
  2 => 
    array (
      'id' => '7',
      'gallery_id' => '2',
      'picture' => '89001465.jpg'
    ),
  3 => 
    array (
      'id' => '6',
      'gallery_id' => '2',
      'picture' => '47360232.jpg'
    ),
  4 => 
    array (
      'id' => '5',
      'gallery_id' => '2',
      'picture' => '4876713.jpg'
    ),
  5 => 
    array (
      'id' => '4',
      'gallery_id' => '2',
      'picture' => '5447392.jpg'
    ),
  6 => 
    array (
      'id' => '3',
      'gallery_id' => '2',
      'picture' => '95117187.jpg'
    )
);

How can I get key of array(0,1,2,3,4,5,6)?

我怎样才能得到 的钥匙array(0,1,2,3,4,5,6)

I have tried a lot of examples, but nothing has worked for me.

我尝试了很多例子,但没有一个对我有用。

回答by DaveRandom

This is quite simple, you just need to use array_keys():

这很简单,你只需要使用array_keys()

$keys = array_keys($array);

See it working

看到它工作

EDITFor your search task, this function should do the job:

编辑对于您的搜索任务,此功能应完成以下工作:

function array_search_inner ($array, $attr, $val, $strict = FALSE) {
  // Error is input array is not an array
  if (!is_array($array)) return FALSE;
  // Loop the array
  foreach ($array as $key => $inner) {
    // Error if inner item is not an array (you may want to remove this line)
    if (!is_array($inner)) return FALSE;
    // Skip entries where search key is not present
    if (!isset($inner[$attr])) continue;
    if ($strict) {
      // Strict typing
      if ($inner[$attr] === $val) return $key;
    } else {
      // Loose typing
      if ($inner[$attr] == $val) return $key;
    }
  }
  // We didn't find it
  return NULL;
}

// Example usage
$key = array_search_inner($array, 'id', 9);

The fourth parameter $strict, if TRUE, will use strict type comparisons. So 9will not work, you would have to pass '9', since the values are stored as strings. Returns the key of the first occurence of a match, NULLif the value is not found, or FALSEon error. make sure to use a strict comparison on the return value, since 0, NULLand FALSEare all possible return values and they will all evaluate to 0if using loose integer comparisons.

第四个参数$strictifTRUE将使用严格的类型比较。所以9不会工作,你必须通过'9',因为值存储为字符串。NULL如果找不到值或FALSE出错,则返回匹配项第一次出现的键。确保对返回值使用严格的比较,因为0,NULLFALSE都是可能的返回值,0如果使用松散整数比较,它们都会评估为。

回答by srbhbarot

Try this , I think it will help you.

试试这个,我想它会帮助你。

foreach ($array as $key=>$value)
{
    echo $key.'<br/>';

    echo $value['id'].'<br/>';
    echo $value['gallery_id'].'<br/>';
    echo $value['picture'].'<br/><br/>';
}

回答by xotix

Probably http://php.net/manual/en/function.array-keys.php?

可能是http://php.net/manual/en/function.array-keys.php

Convert your double dimensional array on your own:

自己转换你的二维数组:

$tmp = null
foreach($array as $key => $value) {
  $tmp[] = $key;
}

print_r($tmp);

回答by rauschen

sometimes it is to easy to find ;)

有时很容易找到;)

array_keys($array);

array_keys

数组键

回答by Sudhir Bastakoti

You mean something like this:

你的意思是这样的:


function getKeys($array)
{
    $resultArr = array();
    foreach($array as $subArr) {
        $resultArr = array_merge($resultArr, $subArr);
    }        
    return array_keys($resultArr);
}