php中的键和值数组访问

时间:2021-08-26 22:09:00

I am calling a function like:

我正在调用一个函数:

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

In function How can I access the key and value as they are variable?

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

function get($where=array()) {

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

How to extract 'id_person' => $person, 'ot' => $ot without using foreach as I know how many key-values pairs I have inside function?

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

4 个解决方案

#1


1  

You can access them via $where['id_person'] / $where['ot'] if you know that they will always have these keys.

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

If you want to access the first and second element, you can do it like this

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

reset($where)
$first = current($where);
$second = next($where);

#2


1  

Couple ways. If you know what keys to expect, you can directly address $where['id_person']; Or you can extract them as local variables:

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

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

If you don't know what to expect, just loop through them:

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

foreach($where AS $key => $value) {
    echo "I found $key which is $value!";
}

#3


0  

Just do $where['id_person'] and $where['ot'] like you do in JavaScript.

只需在JavaScript中执行$ where ['id_person']和$ where ['ot']。

#4


0  

If you do not care about keys and want to use array as ordered array you can shift it.

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

function get($where=array()) {
$value1 = array_shift($where);
$value2 = array_shift($where);
}

#1


1  

You can access them via $where['id_person'] / $where['ot'] if you know that they will always have these keys.

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

If you want to access the first and second element, you can do it like this

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

reset($where)
$first = current($where);
$second = next($where);

#2


1  

Couple ways. If you know what keys to expect, you can directly address $where['id_person']; Or you can extract them as local variables:

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

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

If you don't know what to expect, just loop through them:

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

foreach($where AS $key => $value) {
    echo "I found $key which is $value!";
}

#3


0  

Just do $where['id_person'] and $where['ot'] like you do in JavaScript.

只需在JavaScript中执行$ where ['id_person']和$ where ['ot']。

#4


0  

If you do not care about keys and want to use array as ordered array you can shift it.

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

function get($where=array()) {
$value1 = array_shift($where);
$value2 = array_shift($where);
}