0

我正在调用一个函数,例如:

get(array('id_person' => $person, 'ot' => $ot  ));

在函数中如何访问键和值,因为它们是可变的?

function get($where=array()) {

  echo where[0];
  echo where[1];
}

如何在'id_person' => $person, 'ot' => $ot 不使用foreach的情况下提取,因为我知道函数内部有多少键值对?

4

4 回答 4

1

如果您知道它们将始终拥有这些密钥,则可以通过$where['id_person']/访问它们。$where['ot']

如果你想访问第一个和第二个元素,你可以这样做

reset($where)
$first = current($where);
$second = next($where);
于 2013-03-28T01:22:48.400 回答
1

几种方式。如果您知道期望什么键,您可以直接寻址$where['id_person'];或者您可以将它们提取为局部变量:

function get($where=array()) {
   extract($where);
   echo $id_person;
}

如果您不知道会发生什么,只需遍历它们:

foreach($where AS $key => $value) {
    echo "I found $key which is $value!";
}
于 2013-03-28T01:24:24.723 回答
0

就像在 JavaScript 中做的一样$where['id_person']$where['ot']

于 2013-03-28T01:23:40.950 回答
0

如果您不关心键并希望将数组用作有序数组,则可以对其进行移位。

function get($where=array()) {
$value1 = array_shift($where);
$value2 = array_shift($where);
}
于 2013-03-28T01:28:34.253 回答