最后的值未出现在动态php数组内

最高

我尝试重新排列从db获取的结果到新数组中,以后将在javascript中使用它。这里的问题是当我将结果数据重新排列到新数组中时,我创建的数组中缺少结果的最后2行。我尝试了很多方法,但没有找到解决方案。

来自db的结果数据:

[
  {"attribute":"Paper Material","specification":"Gloss Art Card 250 gsm"},
  {"attribute":"Paper Material","specification":"Gloss Art Card 310 gsm"},
  {"attribute":"Paper Material","specification":"Gloss Art Card 360 gsm"}, 
  {"attribute":"Paper Material","specification":"Briliant White 220 gsm"}, 
  {"attribute":"Paper Material","specification":"Linen 240 gsm"}, 
  {"attribute":"Paper Material","specification":"Metal Ice 250 gsm"}, 
  {"attribute":"Paper Material","specification":"Synthetic Paper 180 micron"},
  {"attribute":"Paper Material","specification":"Super White 250 gsm"}, 
  {"attribute":"Paper Material","specification":"Suwen 240 gsm"}, 
  {"attribute":"Paper Material","specification":"Vellum 220 gsm"}, 
  {"attribute":"Paper Material","specification":"Frosted Plastic Card 0.4mm"},
  {"attribute":"Type","specification":"Standard Business Card"}, 
  {"attribute":"Type","specification":"Folded Business Card"}, 
  {"attribute":"Type","specification":"Custom Die-Cut Business Card"}, 
  {"attribute":"Size","specification":"54mm x 89mm"}, 
  {"attribute":"Size","specification":"52mm x 86mm"}, 
  {"attribute":"Size","specification":"50mm x 89mm"}, 
  {"attribute":"Size","specification":"54mm x 86mm"}, 
  {"attribute":"Orientation","specification":"Portrait"}, 
  {"attribute":"Orientation","specification":"Landscape"}, 
  {"attribute":"Lamination","specification":"Not Required"}, 
  {"attribute":"Lamination","specification":"Matte 2 Side"}, 
  {"attribute":"Lamination","specification":"Matte 1 Side"}, 
  {"attribute":"Lamination","specification":"Gloss 2 Side"}, 
  {"attribute":"Lamination","specification":"Gloss 1 Side"}, 
  {"attribute":"Lamination","specification":"Gloss Water Based Varnish 2 Side"},
  {"attribute":"Lamination","specification":"Matte 2 Side + Spot UV 2 Side"},
  {"attribute":"Lamination","specification":"Matte 2 Side + Spot UV 1 Side"},
  {"attribute":"Hot Stamping","specification":"Not Required"}, 
  {"attribute":"Round Corner","specification":"Not Required"}, 
  {"attribute":"Hole Punching","specification":"Not Required"}, 
  {"attribute":"Color","specification":"4C 1 Side"}, 
  {"attribute":"Color","specification":"4C 2 Side"}]

然后将结果数据重新排列到新的动态数组中:

    $attrArray = array(); //temp attr array as key data for data array
    $specArray = array(); //temp spec array as value data for data array
    $dataArray = array(); //array that consist key:[value] pair after rearrange data complete

    //check result data from db length
    for($x = 0; $x < count($result); $x++){
        //if attr array is empty push attribute into attr array
        //push spec data into spec array
        if($attrArray == []){
            array_push($attrArray,$result[$x]['attribute']);
            array_push($specArray,$result[$x]['specification']);
        }
        //check if attr array is not empty
        elseif($attrArray !== []){
            //count attr array length
            foreach($attrArray as $key){
                //push all spec data into spec array if have same next row attribute
                if($key == $result[$x]['attribute']){
                    array_push($specArray,$result[$x]['specification']);
                }
                //if next attribute is not same from previous attribute
                //push attr array into data array as key and spec array as value
                //empty attr and spec array and push new attr and spec data into array
                elseif($key !== $result[$x]['attribute']){
                    $dataArray[$key] = $specArray;
                    $attrArray = [];
                    $specArray = [];
                    array_push($attrArray,$result[$x]['attribute']);
                    array_push($specArray,$result[$x]['specification']);
                    //unset($key);
                }
            }
        }
    }

    print_r(json_encode($dataArray));

结果:

{
    "Paper Material":["Gloss Art Card 250 gsm","Gloss Art Card 310 gsm","Gloss Art Card 360 gsm","Briliant White 220 gsm","Linen 240 gsm","Metal Ice 250 gsm","Synthetic Paper 180 micron","Super White 250 gsm","Suwen 240 gsm","Vellum 220 gsm","Frosted Plastic Card 0.4mm"],
    "Type":["Standard Business Card","Folded Business Card","Custom Die-Cut Business Card"],
    "Size":["54mm x 89mm","52mm x 86mm","50mm x 89mm","54mm x 86mm"], 
    "Orientation":["Portrait","Landscape"],
    "Lamination":["Not Required","Matte 2 Side","Matte 1 Side","Gloss 2 Side","Gloss 1 Side","Gloss Water Based Varnish 2 Side","Matte 2 Side + Spot UV 2 Side","Matte 2 Side + Spot UV 1 Side"],
    "Hot Stamping":["Not Required"],
    "Round Corner":["Not Required"],
    "Hole Punching":["Not Required"]
}

我已经创建的新数组中缺少最后一个属性的颜色。

请指教谢谢!!!

dWinder

关于您的问题:

$dataArray[$key] = $specArray;遇到注释中提到的密钥之前,您将分配唯一的密钥//if next attribute is not same from previous attribute(然后重置阵列并开始计算新的密钥

color在您的情况下,最后一个键永远都不会到达那个键,if因为他是最后一个键,所以他与上一个键没有区别,因此您永远不会将他插入结果数组

我可能会丢失一些东西,但是您可以通过执行以下操作将其简化很多:

foreach($result as $e) {
    $dataArray[$e["attribute"]][] = $e["specification"];
}

我认为您最好使用此代码,而不要使用复杂的代码。

直播示例:3v4l

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章