首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PHP将数组的第一个键与数组的其余键分开?

如何使用PHP将数组的第一个键与数组的其余键分开?
EN

Stack Overflow用户
提问于 2020-07-15 21:33:48
回答 1查看 30关注 0票数 0

我有一个下面的数组,它是我从Rubrics表中得到的,我正在使用一个jquery扩展,它给了我下面的数组:

代码语言:javascript
复制
Array
(
    [1] => Array
        (
            [0] => Identifies Dilemma
            [1] => Has a vague idea of what the dilemma is and is uncertain what must be decided
            [2] => Identifies the dilemma, including pertinent facts, and ascertains what must be decided
            [3] => Describes the dilemma in detail having gathered pertinent facts. Ascertains exactly what must be decided
        )

    [2] => Array
        (
            [0] => Considers Stakeholders
            [1] => Is unsure as to who should be involved in the decision-making process
            [2] => Determines who should be involved in the decision-making process and accurately identifies all the stakeholders
            [3] => Determines who should be involved in the decision-making process and thoroughly reflects on the viewpoints of the stakeholders
        )

)

我尝试获取的输出是:-

代码语言:javascript
复制
[
'Identifies Dilemma' => [
            [1] => Has a vague idea of what the dilemma is and is uncertain what must be decided
            [2] => Identifies the dilemma, including pertinent facts, and ascertains what must be decided
            [3] => Describes the dilemma in detail having gathered pertinent facts. Ascertains exactly what must be decided
       
                        ]
],
[
'Considers Stakeholders' => [
         [1] => Is unsure as to who should be involved in the decision-making process
            [2] => Determines who should be involved in the decision-making process and accurately identifies all the stakeholders
            [3] => Determines who should be involved in the decision-making process and thoroughly reflects on the viewpoints of the stakeholders
      
       
                        ]
]

这是我到目前为止已经尝试过的,但如果一些php函数可以帮助我,我不想在很多情况下使用它。

代码语言:javascript
复制
    $rubics = json_decode($model->rubric_json);
            $originalPoints = json_decode($model->points_json)[0];
            $rubicsCols = json_decode($model->rubric_json)[0];
            unset($rubics[0]);
            if (!empty($rubics)) {
                foreach ($rubics as $key => $data) {
                    $rows[] =  $data[0];
                }
            }
            
            echo "<pre>";
            print_r($rubics);exit;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-15 21:40:02

代码语言:javascript
复制
$arr = [
   ['a','b','c'],
   ['a1','b1','c1']
];

$newArray = [];

foreach ($arr as $key => $data) {
    $newKey = $data[0];
    unset($data[0]);
    $newArray[$newKey] = $data;
}

print_r($newArray);

输出:

代码语言:javascript
复制
Array
(
    [a] => Array
    (
        [1] => b
        [2] => c
    )

    [a1] => Array
    (
        [1] => b1
        [2] => c1
    )

)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62916102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档