PHP根据匹配值划分数组

时间:2017-11-07 06:09:45

标签: php arrays divide

在PHP中,是否可以根据匹配值对数组进行分割或拆分?这些数组值被动态地推送到数组中。它们是来自单个mysql数据库查询的结果。长话短说,我设法通过将我的查询放在for循环中并完成一些jenky PHP编码来完成这项任务,但这种方法似乎非常低效且易于破解。下面的数组是我试图划分的数组的一个例子:

$returnedData = [
    ["1003", "correct"],
    ["1003", "incorrect"],
    ["1003", "correct"],
    ["1004", "correct"],
    ["1004", "incorrect"],
    ["1004", "incorrect"],
    ["1005", "incorrect"],
    ["1005", "correct"],
    ["1005", "incorrect"]
  ];

可以使用PHP将这个数组拆分成这样吗?

  $returnedData = [
    [
      ["1003", "correct"],
      ["1003", "incorrect"],
      ["1003", "correct"]
    ],
    [
      ["1004", "correct"],
      ["1004", "incorrect"],
      ["1004", "incorrect"]
    ],
    [
      ["1005", "incorrect"],
      ["1005", "correct"],
      ["1005", "incorrect"]
    ]
  ];

5 个答案:

答案 0 :(得分:0)

您可以通过遍历对数组进行分组。请尝试以下代码:

<?php

$returnedData = [
    ["1003", "correct"],
    ["1003", "incorrect"],
    ["1003", "correct"],
    ["1004", "correct"],
    ["1004", "incorrect"],
    ["1004", "incorrect"],
    ["1005", "incorrect"],
    ["1005", "correct"],
    ["1005", "incorrect"]
  ];

$grouped_array = array();
foreach($returnedData as $data)
{
    if(isset($grouped_array[$data[0]]))
        array_push($grouped_array[$data[0]],$data);
    else
        $grouped_array[$data[0]] = array($data);
}
$grouped_array = array_values($grouped_array);
print_r($grouped_array);

DEMO

答案 1 :(得分:0)

这可以使用array_reduce完成您的工作。

$data = [
    ["1003", "correct"],
    ["1003", "incorrect"],
    ["1003", "correct"],
    ["1004", "correct"],
    ["1004", "incorrect"],
    ["1004", "incorrect"],
    ["1005", "incorrect"],
    ["1005", "correct"],
    ["1005", "incorrect"]
];

$data = array_reduce($data, function ($data, $value) {
    if (! array_key_exists($value[0], $data)) {
        $data[$value[0]] = [];  
    }

    $data[$value[0]][] = $value;
    return $data;
}, []);

答案 2 :(得分:0)

使用此代码

$returnedData2=$log=array();

foreach($returnedData as $i=>$subAry){
    $ind=$subAry[0];
    if(isset($log[$ind])){
       $ind2= $log[$ind];
       array_push($returnedData2[$ind2], $subAry);

    }else{
       $log[$ind]=$i;
       $returnedData2[$i]=array();
       array_push($returnedData2[$i], $subAry);

    }

}
echo '<pre>';
print_r($returnedData2);

答案 3 :(得分:0)

试试这段代码

<?php
$returnedData = [
    ["1003", "correct"],
    ["1003", "incorrect"],
    ["1003", "correct"],
    ["1004", "correct"],
    ["1004", "incorrect"],
    ["1004", "incorrect"],
    ["1005", "incorrect"],
    ["1005", "correct"],
    ["1005", "incorrect"]
  ];

$output = array();

foreach($returnedData as $value)
{
    if(isset($output[$value[0]]))
        array_push($output[$value[0]],$value);
    else
        $output[$value[0]] = array($value);
}
$output = array_values($output);

foreach($output as $data){
    print_r(json_encode($data));echo "\n";
}

在此处查看输出:output

答案 4 :(得分:0)

您可以检查首先格式化代码然后返回匹配键数组。我将 json_encode 用于格式数组结果作为您的要求

<?php

$data = [
    ["1003", "correct"],
    ["1003", "incorrect"],
    ["1003", "correct"],
    ["1004", "correct"],
    ["1004", "incorrect"],
    ["1004", "incorrect"],
    ["1005", "incorrect"],
    ["1005", "correct"],
    ["1005", "incorrect"]
];

$format_data = array();

foreach ($data as $value) {
    if (isset($format_data[$value[0]])) {
        array_push($format_data[$value[0]], $value);
    } else {
        $format_data[$value[0]] = array($value);
    }
}
$format_array = array_values($format_data);

foreach ($format_array as $data) {
    $result[] = json_encode($data);
}
echo "<pre>";
print_r($result);
?>

,最终输出为:

enter image description here