php - 最后一个值没有出现在动态 php 数组中

标签 php arrays json

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

来自数据库的结果数据:

[
  {"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"]
}

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

请多多指教,谢谢!!!

最佳答案

关于您的问题:

仅当遇到之前不存在的键时才分配 $dataArray[$key] = $specArray; //如果下一个属性与上一个属性不同 (然后重置数组并开始为新数组计数)

最后一个键,color在你的情况下永远不会到达那个if,因为他是最后一个,所以他永远不会与前一个键不同,所以你永远不会将他插入到结果数组中

我可能遗漏了一些东西,但你可以通过这样做来简化它:

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

我认为你最好使用这个而不是你拥有的复杂代码。

实例:3v4l

关于php - 最后一个值没有出现在动态 php 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57158622/

相关文章:

c# - 将 MSSQL GetUTCDate() 转换为 PHP/Unix/MySQL Ticks

string - 有没有办法在 swift 中从 utf16 数组创建字符串?

php - 买家在 magento 产品页面中填写的字段

php - 我正在尝试在 Laravel 数据库上使用多个过滤器

javascript - 在 Prop 中提取对象

javascript - 函数定义中使用的命名数组元素

java - 使用jackson将Json对象转换为java对象

php - 当我包含特殊字符串时,Wordpress 不会解析 JSON

java - JSONArray 类型的 add(String) 方法未定义

php - 显示数据库中的多个图像