相关疑难解决方法(0)

PHP多维数组按值搜索

我有一个数组,我想搜索uid并获取数组的键.

例子

假设我们有以下二维数组:

$userdb = array(
    array(
        'uid' => '100',
        'name' => 'Sandra Shush',
        'pic_square' => 'urlof100'
    ),
    array(
        'uid' => '5465',
        'name' => 'Stefanie Mcmohn',
        'pic_square' => 'urlof100'
    ),
    array(
        'uid' => '40489',
        'name' => 'Michael',
        'pic_square' => 'urlof40489'
    )
);
Run Code Online (Sandbox Code Playgroud)

函数调用search_by_uid(100)(第一个用户的uid)应该返回0.

函数调用search_by_uid(40489)应该返回2.

我尝试制作循环,但我想要更快的执行代码.

php arrays multidimensional-array

298
推荐指数
9
解决办法
42万
查看次数

php - 获取关联数组的数字索引

我有一个关联数组,我需要找到一个键的数字位置.我可以手动遍历数组来找到它,但有没有更好的方法构建到PHP?

$a = array(
  'blue'   => 'nice',
  'car'    => 'fast',
  'number' => 'none'
);

// echo (find numeric index of $a['car']); // output: 1
Run Code Online (Sandbox Code Playgroud)

php arrays

144
推荐指数
3
解决办法
11万
查看次数

PHP - 获取数组值的键名

我有一个数组如下:

function example() {
    /* some stuff here that pushes items with
        dynamically created key strings into an array */

    return array( // now lets pretend it returns the created array
        'firstStringName' => $whatEver,
        'secondStringName' => $somethingElse
    );
}

$arr = example();

// now I know that $arr contains $arr['firstStringName'];
Run Code Online (Sandbox Code Playgroud)

我需要找出索引,$arr['firstStringName']以便我能够循环array_keys($arr)'firstStringName'通过其索引返回键字符串.我怎样才能做到这一点?

php arrays array-key

139
推荐指数
9
解决办法
33万
查看次数

标签 统计

arrays ×3

php ×3

array-key ×1

multidimensional-array ×1