Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
最后一个值未出现在动态php数组中_Php_Arrays_Json - Fatal编程技术网

最后一个值未出现在动态php数组中

最后一个值未出现在动态php数组中,php,arrays,json,Php,Arrays,Json,我尝试将从db获得的结果重新排列到新数组中,该数组稍后将在javascript中使用。这里的问题是,当我将结果数据重新排列到新数组中时,我创建的数组中缺少结果的最后2行。我尝试了很多方法,但没有找到解决办法 来自数据库的结果数据: [ {"attribute":"Paper Material","specification":"Gloss Art Card 250 gsm"}, {"attribute":"Paper Material","specification":"Gloss Ar

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

来自数据库的结果数据:

[
  {"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));
其中,我已经创建的新数组缺少最后一个属性颜色

请告知谢谢

关于您的问题:

分配
$dataArray[$key]=$specArray
仅当遇到一个键时,如您的注释所述,
//如果下一个属性与上一个属性不同
(然后重置数组并开始计算新属性)

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

我可能遗漏了一些东西,但您可以通过以下操作将其简化:

foreach($result as $e) {
    $dataArray[$e["attribute"]][] = $e["specification"];
}
我认为你最好使用这个,而不是你拥有的复杂代码


实时示例:

循环数组,并基于子数组中的两项构建关联数组

$arr = json_decode($json, true);

foreach($arr as $item){
    $new[$item["attribute"]][] = $item["specification"];
}

var_dump($new);

如果您使用

$data=array\u reduce($data,function($old,$new){
$old[$new->attribute]=isset($old[$new->attribute])?数组合并($old[$new->attribute],$new->specification]):[$new->specification];
返回$old;
}, []);
echo“”,json_编码($data);

正在工作。

正如其他用户所指出的,有更好的方法可以做到这一点。但是,如果您想专门修复代码,可以在for循环完成后和打印数组之前添加以下行

。
.
对于($x=0;$x
找到问题做得很好。我没有耐心去读一本书,也没有耐心去理解那些“简单”任务的代码。谢谢你的澄清和回答。它的帮助:)
$arr = json_decode($json, true);

foreach($arr as $item){
    $new[$item["attribute"]][] = $item["specification"];
}

var_dump($new);
$data = array_reduce($data, function ($old, $new) {
    $old[$new->attribute] = isset($old[$new->attribute]) ? array_merge($old[$new->attribute], [$new->specification]) : [$new->specification];
    return $old;
}, []);

echo '<pre>', json_encode($data);
.
.

for($x = 0; $x < count($result); $x++){
.
.
.
.
}//For Loop Finishes here

$dataArray[$key] = $specArray; //Add this Line. This will add your color keys to array.
print_r(json_encode($dataArray));