内爆值数组及其键

发布于 2024-09-08 20:45:20 字数 1308 浏览 6 评论 0原文

我正在尝试破坏其键和值的数组。我可以轻松地通过内爆获得钥匙,但发现我必须重复自己才能获得钥匙。

目前我正在这样做:

$values = array(
  'id'                    =>  $sel['id'],
  'creator_id'            =>  $sel['creator_id'],
  'campaign_id'           =>  $sel['campaign_id'],
  'save_results'          =>  $sel['save_results'],
  'send_results_url'      =>  $sel['send_results_url'],
  'reply_txt'             =>  $sel['reply_txt'],
  'allow_multiple_votes'  =>  $sel['allow_multiple_votes']
    );
    $cols = '';
    $vals = '';
    $first = true;
    foreach($values as $col => $val) {
        if(!$first) {
            $cols .= ', ';
            $vals .= ', ';
        }
        $cols .= $col;
        $vals .= $val;
        $first = false;
    }

困扰我的部分是:

foreach($values as $col => $val) {
  if(!$first) {
    $cols .= ', ';
    $vals .= ', ';
  }
  $cols .= $col;
  $vals .= $val;
  $first = false;
}

有没有办法使数组键内爆?

例如,我可以

$vals = implode(', ', $values);

对值进行内爆,但我也需要对键执行此操作。

我也可以使用,

$keys = array();
    foreach($values as $col => $val)
        $keys[] = $col;
    $cols = implode(', ', $keys);
    $rows = implode(', ', $values);

但它仍然需要我循环创建另一个数组,当然有更好的方法,只获取密钥吗?

I'm trying to implode an array of both its keys and values. I can easily get the keys with the implode, but find I have to repeat myself for the keys.

Currently I am doing this:

$values = array(
  'id'                    =>  $sel['id'],
  'creator_id'            =>  $sel['creator_id'],
  'campaign_id'           =>  $sel['campaign_id'],
  'save_results'          =>  $sel['save_results'],
  'send_results_url'      =>  $sel['send_results_url'],
  'reply_txt'             =>  $sel['reply_txt'],
  'allow_multiple_votes'  =>  $sel['allow_multiple_votes']
    );
    $cols = '';
    $vals = '';
    $first = true;
    foreach($values as $col => $val) {
        if(!$first) {
            $cols .= ', ';
            $vals .= ', ';
        }
        $cols .= $col;
        $vals .= $val;
        $first = false;
    }

The part that bothers me is this:

foreach($values as $col => $val) {
  if(!$first) {
    $cols .= ', ';
    $vals .= ', ';
  }
  $cols .= $col;
  $vals .= $val;
  $first = false;
}

Is there a way to implode the array keys?

For example, I could do

$vals = implode(', ', $values);

to implode the values, but I need to do this as well for the keys.

I could also use

$keys = array();
    foreach($values as $col => $val)
        $keys[] = $col;
    $cols = implode(', ', $keys);
    $rows = implode(', ', $values);

but it still requires me to loop over it creating another array, surely there is a better way, do just get the keys?

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

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

发布评论

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

评论(3

苍景流年 2024-09-15 20:45:20
$cols = implode(', ',array_keys($values));
$cols = implode(', ',array_keys($values));
不喜欢何必死缠烂打 2024-09-15 20:45:20

该函数将从多维数组中提取键

<?php 
function multiarray_keys($ar) { 

    foreach($ar as $k => $v) { 
        $keys[] = $k; 
        if (is_array($ar[$k])) 
            $keys = array_merge($keys, multiarray_keys($ar[$k])); 
    } 
    return $keys; 
} 
?> 

This function will extract keys from a multidimensional array

<?php 
function multiarray_keys($ar) { 

    foreach($ar as $k => $v) { 
        $keys[] = $k; 
        if (is_array($ar[$k])) 
            $keys = array_merge($keys, multiarray_keys($ar[$k])); 
    } 
    return $keys; 
} 
?> 
烈酒灼喉 2024-09-15 20:45:20

print_r($values,true);

然后从结果中删除前两行和最后一行:

Array
(
    [foo] => bar
    [baz] => boom
)

print_r($values,true);

Then removing the first two lines and the last line from the result:

Array
(
    [foo] => bar
    [baz] => boom
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文