是否可以使用键的值将数组拆分为多个块?

时间:2019-05-15 22:47:30

标签: php

假设我有一个像这样的数组:

$arr = [
    "question" => "What's your name?", "question_key" => "key1",
    "question" => "What's your job?", "question_key" => "key2",
    "question" => "Where do you live?", "question_key" => "key3", 

    "question" => "What's your name?", "question_key" => "key1",
    "question" => "What's your job?", "question_key" => "key2",
    "question" => "Where do you live?", "question_key" => "key3", 

    "question" => "What's your name?", "question_key" => "key1",
    "question" => "What's your job?", "question_key" => "key2",
    "question" => "Where do you live?", "question_key" => "key3", 
]

我是否可以使用array_chunk之类的东西并根据question_key拆分它们?就像每次到达键“ question_key” =>“ key3”一样,它将数组拆分成一个块,这样最终会像这样:

$newArr = [
    ["question" => "What's your name?", "question_key" => "key1",
    "question" => "What's your job?", "question_key" => "key2",
    "question" => "Where do you live?", "question_key" => "key3"], 

    ["question" => "What's your name?", "question_key" => "key1",
    "question" => "What's your job?", "question_key" => "key2",
    "question" => "Where do you live?", "question_key" => "key3"], 

    ["question" => "What's your name?", "question_key" => "key1",
    "question" => "What's your job?", "question_key" => "key2",
    "question" => "Where do you live?", "question_key" => "key3"], 
]

0 个答案:

没有答案