【问题标题】:Change array keys of nested array on multidimensional array from integer to alphabet将多维数组上嵌套数组的数组键从整数更改为字母
【发布时间】:2021-02-09 18:54:15
【问题描述】:
array (
  'A' => 
  array (
    'topic' => 'My topic name',
    'starttime' => '12/11/2018 16:30:00',
    'endtime' => '12/11/2018 18:00:00',
    'webinarid' => '1235',
    'webinarkey' => '123518238123',
    'orgkey' => '',
    'regurl' => 'https://attendee.gotowebinar.com/register/xxx',
    'description' => 'Long desc',
    'newsletteremailid' => '321',
    'ogimage' => 'https://xxx',
    'presenterurl' => 'https://xxx',
    'elite' => '1',
    'postid' => '699599',
  ),
  'B' => 
  array (
    'topic' => 'Topic title',
    'starttime' => '12/4/2018 16:30:00',
    'endtime' => '12/4/2018 18:00:00',
    'webinarid' => '123512',
    'webinarkey' => '8127317327132',
    'orgkey' => '',
    'regurl' => 'https://attendee.gotowebinar.com/register/xxx',
    'description' => 'Topic description',
    'newsletteremailid' => '316',
    'ogimage' => 'https://xxx',
    'presenterurl' => '',
    'elite' => '1',
    'postid' => '8235123'
    )
)

我试图将每个嵌套数组从 0..x 转换为字母表。所以例如'topic'是'A','orgkey'是'F'。

然后返回一个新的多维数组('A' => array('A' => 'My topic name', 'B' => '12/11/2018 16:30:00'))等等.

【问题讨论】:

    标签: php arrays multidimensional-array


    【解决方案1】:

    创建一个字母数组并循环遍历该数组,用相同索引处的字母替换键。

    https://paiza.io/projects/_ICIlEET4tieXtsN4MF4rw

    $alph = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
    
    $arr=array('A'=>array('topic'=>'My topic name','starttime'=>'12/11/2018 16:30:00','endtime'=>'12/11/2018 18:00:00','webinarid'=>'1235','webinarkey'=>'123518238123','orgkey'=>'','regurl'=>'https://attendee.gotowebinar.com/register/xxx','description'=>'Long desc','newsletteremailid'=>'321','ogimage'=>'https://xxx','presenterurl'=>'https://xxx','elite'=>'1','postid'=>'699599'),'B'=>array('topic'=>'Topic title','starttime'=>'12/4/2018 16:30:00','endtime'=>'12/4/2018 18:00:00','webinarid'=>'123512','webinarkey'=>'8127317327132','orgkey'=>'','regurl'=>'https://attendee.gotowebinar.com/register/xxx','description'=>'Topic description','newsletteremailid'=>'316','ogimage'=>'https://xxx','presenterurl'=>'','elite'=>'1','postid'=>'8235123'));
    
    $newArr = array();
    foreach($arr as $key1 => $val1){
        $i=0;
        foreach($val1 as $key2 => $val2){
            $newKey = "";
            //this if/else statement sets the key from A-Z, then AA, AB, etc.
            if($i > count($alph) - 1){
                $newKey = $alph[floor($i / count($alph)-1)].$alph[$i % count($alph)];
            }else{
                $newKey = $alph[$i];
            }
            $newArr[$key1][$newKey] = $val2;
            $i++;
        }
    }
    
    print_r($newArr);
    

    【讨论】:

    • 这是一种非常复杂的方法,可以通过 php 中的内置函数来完成。
    • @RyanH 我的代码说明了当数组元素的数量超过字母表中的字母数量时的实例。
    • @RyanH 非常复杂 ?我认为,在提出的解决方案中,它可能是最容易理解的。诚然,它有点冗长,但对于初学者和中级 PHP 开发人员来说,这个解决方案非常棒,因为与内置函数不同,它准确地显示了结果是如何构造的。 +1 符号链接。
    • 实际上,我最终使用了您的答案,因为它最容易根据我的需要进行修改。由于复杂性,我一开始就错误地对你投了反对票。那是我的错误。请进行编辑,以便我投票并将您标记为我的答案,谢谢。
    【解决方案2】:

    您没有任何整数/数字键;但假设它们已经按正确的顺序排列,只需创建一系列字母并将这些键与现有值组合。这只适用于Z。然后转至AA 等。会涉及更多,可能需要问另一个问题:

    foreach($array as &$values) {
        $values = array_combine(range(chr(65), chr(64+count($array))), $values);
    }
    

    如果它们不按顺序排列,那么您必须分配每个:

    foreach($array as $key => $values) {
        $result[$key] = ['A' => $values['topic'], 'B' => $values['starttime']]; // etc...
    }
    

    我真的想不出任何理由这样做,因此您可能需要重新考虑您的方法。

    【讨论】:

    • 谢谢。在range()上,如何使结束范围动态到数组的长度?
    • 使用 ascii 值编辑。这只适用于 Z。然后换成 AA 等。会涉及更多,可能需要问另一个问题。
    • 如果超过 26,这可能会导致意外行为,因为在 'Z' 之后,您会进入 ascii 表中的括号。
    • @RyanH 是的,这只适用于 Z
    【解决方案3】:
    array_walk($data, function(&$value){
        $keys = array_splice(range('A', 'Z', 1), 0, count($value));
        $value = array_combine($keys, $value);
    });
    

    这适用于任何长度的数据数组......最多 26 个项目

    【讨论】:

    • 如果您想查看完整的运行代码3v4l.org/JA9VS
    • 好一个!但是你不需要$value = array_values($value);
    • 非常好的点@AbraCadaver,更新了答案,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    相关资源
    最近更新 更多