PHP根据键值生成不同的数组集
这是我得到的数组,请帮助我你得到相同键值的输出,为每个相同的类别创建不同的数组。我在下面添加所需的输出PHP根据键值生成不同的数组集,php,arrays,dynamic-arrays,Php,Arrays,Dynamic Arrays,这是我得到的数组,请帮助我你得到相同键值的输出,为每个相同的类别创建不同的数组。我在下面添加所需的输出 Array ( [2] => Array ( [ans_type] => single [ques] => Aşağıdakilerden hangisi Displayport'un özelliklerindendir [opt1] => Analog video konektörü [opt2
Array
(
[2] => Array
(
[ans_type] => single
[ques] => Aşağıdakilerden hangisi Displayport'un özelliklerindendir
[opt1] => Analog video konektörü
[opt2] => Sadece ses konektörü
[opt3] => (MNC)
[opt4] => Analog ses konektörü
[opt5] => Sadece video konektörü
[ans] => C
[cat] => Genel
[title] => Aşağıdakilerden hangisi Displayport'un özelliklerindendir
[quiz_name] => history
)
[3] => Array
(
[ans_type] => single
[ques] => Aşağıdaki konektör tiplerinden hangileri coaxial kablo ile kullanılır
[opt1] => ST
[opt2] => MT RJ
[opt3] => (BNC)
[opt4] => LC
[opt5] => F-connector
[ans] => E
[points] => 2
[cat] => Genel
[title] => Aşağıdaki konektör tiplerinden hangileri coaxial kablo ile kullanılır
[quiz_name] => geography
)
)
这是所需的输出
Array
(
[0] => Array
(
[0] => Array
(
[ans_type] => single
[ques] => Aşağıdakilerden hangisi Displayport'un özelliklerindendir
[opt1] => Analog video konektörü
[opt2] => Sadece ses konektörü
[opt3] => (MNC)
[opt4] => Analog ses konektörü
[opt5] => Sadece video konektörü
[ans] => C
[cat] => Genel
[title] => Aşağıdakilerden hangisi Displayport'un özelliklerindendir
[quiz_name] => history
)
[1] => Array
(
[ans_type] => single
[ques] => Aşağıdaki konektör tiplerinden hangileri coaxial kablo ile kullanılır
[opt1] => ST
[opt2] => MT RJ
[opt3] => (BNC)
[opt4] => LC
[opt5] => F-connector
[ans] => E
[points] => 2
[cat] => Genel
[title] => Aşağıdaki konektör tiplerinden hangileri coaxial kablo ile kullanılır
[quiz_name] => history
)
)
[1] => Array
(
[0] => Array
(
[ans_type] => single
[ques] => Aşağıdakilerden hangisi Displayport'un özelliklerindendir
[opt1] => Analog video konektörü
[opt2] => Sadece ses konektörü
[opt3] => (MNC)
[opt4] => Analog ses konektörü
[opt5] => Sadece video konektörü
[ans] => C
[cat] => Genel
[title] => Aşağıdakilerden hangisi Displayport'un özelliklerindendir
[quiz_name] => geography
)
)
)
如果可以将结果数组的索引作为类别名称,那么事情很简单。
让
$first
成为您的第一个数组
$first = Array
(
[2] => Array
(
[ans_type] => single
[ques] => Aşağıdakilerden hangisi Displayport'un özelliklerindendir
[opt1] => Analog video konektörü
.....
.....
然后在$first
数组上执行forach循环
$result = array();
foreach($first as $row){
$index = $row['quiz_name'];
$result[$index][] = $row;
}
var_dump($result);
输出结果如下:
Array
(
['history'] => Array
(
[0] => Array
(
[ans_type] => single
[ques] => Aşağıdakilerden hangisi Displayport'un özelliklerindendir
[opt1] => Analog video konektörü
[opt2] => Sadece ses konektörü
[opt3] => (MNC)
[opt4] => Analog ses konektörü
[opt5] => Sadece video konektörü
[ans] => C
[cat] => Genel
[title] => Aşağıdakilerden hangisi Displayport'un özelliklerindendir
[quiz_name] => history
)
[1] => Array
(
[ans_type] => single
[ques] => Aşağıdaki konektör tiplerinden hangileri coaxial kablo ile kullanılır
[opt1] => ST
[opt2] => MT RJ
[opt3] => (BNC)
[opt4] => LC
[opt5] => F-connector
[ans] => E
[points] => 2
[cat] => Genel
[title] => Aşağıdaki konektör tiplerinden hangileri coaxial kablo ile kullanılır
[quiz_name] => history
)
)
['geography'] => Array
(
[0] => Array
(
[ans_type] => single
[ques] => Aşağıdakilerden hangisi Displayport'un özelliklerindendir
[opt1] => Analog video konektörü
[opt2] => Sadece ses konektörü
[opt3] => (MNC)
[opt4] => Analog ses konektörü
[opt5] => Sadece video konektörü
[ans] => C
[cat] => Genel
[title] => Aşağıdakilerden hangisi Displayport'un özelliklerindendir
[quiz_name] => geography
)
)
)
请重新表述您的问题,并详细说明您正在寻找的数组合并的“规则”。谢谢您的回答,fi您可以给我一个简单的示例,这将有助于我需要[quick_name]=>历史记录此键值对,如果我们发现与此键值相似,则将得到不同的数组