弹出钥匙 & PHP 中关联数组的值

发布于 2024-11-19 00:49:08 字数 309 浏览 2 评论 0原文

假设 S 是 PHP 中的关联数组,我需要从中检索并提取第一个元素,包括值和键。

我会使用

value1=array_pop(S);

,但它只给我带来价值。

我可以使用

K=array_keys(S);
key1=array_pop(K);
value1=array_pop(S);

,但它很复杂,因为它需要相同数据的两个副本。这是一个令人困惑的问题,因为数组本身就是数组数组中的一个元素。必须有一种更优雅的方法来在提取键/值时读取它。

Let S be an associative array in PHP, I need to retrieve and extract from it the first element, both the value and the key.

I would use

value1=array_pop(S);

but it only gives me the value.

I can use

K=array_keys(S);
key1=array_pop(K);
value1=array_pop(S);

but it is complicated because it requires to have two copies of the same data. WHich is a confusing since the array is itself an element in an array of arrays. There must be a more elegant way to just read the couple key/value while extracting it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

疾风者 2024-11-26 00:49:10
$value = reset($array);
$key = key($array);

编辑:哈克莱比我先一步:-)

$value = reset($array);
$key = key($array);

Edit: Hakre just beat me to it :-)

歌枕肩 2024-11-26 00:49:10
list($value, $key) = array(reset($s), key($s));
array_shift($s); // or just unset($s[$key]);

当然,您可以将第一个语句分成两个单独的语句。

list($value, $key) = array(reset($s), key($s));
array_shift($s); // or just unset($s[$key]);

Of course you can split the first statement into two separate.

笙痞 2024-11-26 00:49:10

这是我在类似情况下所做的:

$last_index = count($test_array)-1;
if($last_index >= 0) {
    $last_key = array_keys($test_array)[$last_index];
    foreach($test_array as $k => $v) {
        if($k == $last_key)
            echo "last: ";
        echo $k . '=' . $v . PHP_EOL;
    }
}

尽管我最终使用的解决方案是一个 while 循环(我实际上不需要保留密钥)。

我基本上是试图通过将多个元素放在同一行上来将数组的元素适合到 pdf 的某个区域。我使用 array_chunk() 将数组分成每个不超过 5 个的子数组,然后使用 while(count($chunks) > 0) {} 移动生成的数组。本文的一些解决方案可以类似地在 while 循环中移动/取消设置数组的元素,直到数组为空。

Here's what I did in a similar situation:

$last_index = count($test_array)-1;
if($last_index >= 0) {
    $last_key = array_keys($test_array)[$last_index];
    foreach($test_array as $k => $v) {
        if($k == $last_key)
            echo "last: ";
        echo $k . '=' . $v . PHP_EOL;
    }
}

although the solution I eventually ended up using was a while loop (I didn't really need to retain the keys).

I was basically trying to fit the elements of an array onto an area of a pdf by putting more than one on the same line. I used array_chunk() to break the array up into sub-arrays of 5-or-less each, then shifted the resultant array with a while(count($chunks) > 0) {}. Some of the solutions to this post could similarly shift/unset the elements of an array in a while loop until the array was empty.

梦魇绽荼蘼 2024-11-26 00:49:09

array_slice

$arr = array('k1' => 'v1', 'k2' => 'v2', 'k3' => 'v3');

$a = array_slice($arr, 0, 1);
var_dump($a);

$arr = array_slice($arr, 1);
var_dump($arr);


array(1) {
  ["k1"]=>
  string(2) "v1"
}
array(2) {
  ["k2"]=>
  string(2) "v2"
  ["k3"]=>
  string(2) "v3"
}

array_slice

$arr = array('k1' => 'v1', 'k2' => 'v2', 'k3' => 'v3');

$a = array_slice($arr, 0, 1);
var_dump($a);

$arr = array_slice($arr, 1);
var_dump($arr);


array(1) {
  ["k1"]=>
  string(2) "v1"
}
array(2) {
  ["k2"]=>
  string(2) "v2"
  ["k3"]=>
  string(2) "v3"
}
不疑不惑不回忆 2024-11-26 00:49:08
// first
$value = reset($arr);
$key = key($arr);

(按顺序)

参见 reset()< strong>PHP 手册,
key()PHP 手册

unset($arr[$key]); # in case you want to remove it.

但是 array_pop()PHP手册正在处理最后一个元素:

// last
$value = end($arr);
$key = key($arr);
unset($arr[$key]); # in case you want to remove it.

请参阅end()PHP 手册

为了好玩:

[$value, $key] = [reset($arr), key($arr)]; // first
[$value, $key] = [end($arr), key($arr)]; // last

(PHP 7.1+)

list($value, $key) = array(reset($arr), key($arr)); // first
list($value, $key) = array(end($arr), key($arr)); // last

(PHP 4.3+)

extract(array('value' => reset($arr), 'key' => key($arr))); // first
extract(array('value' => end($arr), 'key' => key($arr))); // last

(PHP 4.3+;警告:extract() 正在使用!)

// first
reset($arr);
list($key, $value) = each($arr);


// last
end($arr);
list($key, $value) = each($arr);

(注意:each() 函数自 PHP 7.2.0 起已弃用,自 PHP 起已消失8.0.0)

或任何你喜欢的游戏风格;)

处理空数组

到目前为止,还缺少处理空数组的功能。因此需要检查是否存在最后一个(第一个)元素,如果没有,则将 $key 设置为 null (因为 null 可以不是数组键):

// first
for ($key = null, $value = null; false !== $_ = reset($arr);)
{
    $value = $_;
    unset($arr[$key = key($arr)]);
    break;
}
unset($_);

// last
for ($key = null, $value = null; false !== $_ = end($arr);)
{
    $value = $_;
    unset($arr[$key = key($arr)]);
    break;
}
unset($_);

这将给出一个填充数组,如 $arr = array('first' => '1st', 'last' => '2nd.');

string(4) "2nd." # value
string(4) "last" # key
array(1) { # leftover array
  ["first"]=>
  string(3) "1st"
}

和一个空数组:

bool(false) # value
NULL # key
array(0) { # leftover array
}

害怕使用未设置?

如果您不相信 unset() 具有您需要的性能(我认为这不是一个真正的问题,尽管我没有运行任何指标),您可以使用本机array_pop() 也实现了(但我真的认为 unset() 作为语言构造可能会更快):

// first
reset($arr);
$key = key($arr);
$value = array_pop($arr);


// last
end($arr);
$key = key($arr);
$value = array_pop($arr);
// first
$value = reset($arr);
$key = key($arr);

(in that order)

See reset()PHP Manual,
key()PHP Manual.

unset($arr[$key]); # in case you want to remove it.

However array_pop()PHP Manual is working with the last element:

// last
$value = end($arr);
$key = key($arr);
unset($arr[$key]); # in case you want to remove it.

See end()PHP Manual.

For the fun:

[$value, $key] = [reset($arr), key($arr)]; // first
[$value, $key] = [end($arr), key($arr)]; // last

(PHP 7.1+)

or

list($value, $key) = array(reset($arr), key($arr)); // first
list($value, $key) = array(end($arr), key($arr)); // last

(PHP 4.3+)

or

extract(array('value' => reset($arr), 'key' => key($arr))); // first
extract(array('value' => end($arr), 'key' => key($arr))); // last

(PHP 4.3+; Caution: extract() in use!)

or

// first
reset($arr);
list($key, $value) = each($arr);


// last
end($arr);
list($key, $value) = each($arr);

(Note: The each() function is deprecated since PHP 7.2.0 and gone since PHP 8.0.0)

or whatever style of play you like ;)

Dealing with empty arrays

It was missing so far to deal with empty arrays. So it's a need to check if there is a last (first) element and if not, set the $key to null (as null can not be an array key):

// first
for ($key = null, $value = null; false !== $_ = reset($arr);)
{
    $value = $_;
    unset($arr[$key = key($arr)]);
    break;
}
unset($_);

// last
for ($key = null, $value = null; false !== $_ = end($arr);)
{
    $value = $_;
    unset($arr[$key = key($arr)]);
    break;
}
unset($_);

This will give for a filled array like $arr = array('first' => '1st', 'last' => '2nd.');:

string(4) "2nd." # value
string(4) "last" # key
array(1) { # leftover array
  ["first"]=>
  string(3) "1st"
}

And an empty array:

bool(false) # value
NULL # key
array(0) { # leftover array
}

Afraid of using unset?

In case you don't trust unset() having the performance you need (of which I don't think it's really an issue, albeit I haven't run any metrics), you can use the native array_pop() implementation as well (but I really think that unset() as a language construct might be even faster):

// first
reset($arr);
$key = key($arr);
$value = array_pop($arr);


// last
end($arr);
$key = key($arr);
$value = array_pop($arr);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文