dousao6313 2018-08-21 10:23
浏览 104
已采纳

过滤多维数组中的字段,根据键将它们放在新数组中。 [PHP]

For a webstore script I am trying to create a filter for my two dimensional array. I have used a few guides and other answers here on Stackoverflow or w3Schools, but I keep running into the same problem;

The newly created array just has empty gaps where the filter takes out the data, rather than creating a 0 to 100 array full of relevant information.

Since I am still new to the whole multidimensional array thing, I was hoping somebody here could point me in the right direction and tell me what I`m doing wrong.

Array getting fully filled with stuff from my Database

                if ($result->num_rows > 0) {
                    while($row = $result->fetch_assoc()) {

                        $product[] = [
                            "ID" => $row["ID"],
                            "Name" => $row["Productname"],
                            "Price" => $row["Pricetag"],
                            "Supply" => $row["Productsupply"],
                            "Type" => $row["Producttype"],
                            "Tags" => $row["Tags"],
                            "Materials" => $row["Materials"],
                            "Date" => $row["Releasedate"],
                            "Description" => $row["Description"],
                            "isDigital" => $row["isDigital"],
                            "MainPhoto" => $row["MainPhoto"],
                            "Photo01" => $row["Productpic01"],
                            "Photo02" => $row["Productpic02"],
                            "Photo03" => $row["Productpic03"]
                        ];
                    }
                } else {
                    echo "0 results found. Please check database connection.";
                }

Script to filter this data and creating a new array based on the matches in the key:

    $Filterarray = array('Pluimstaart'); //Might filter on more keywords in future usage.
    $newArr = array();
    foreach ($product as $key => $value) {
        foreach ($value as $finalVal) {
            if(in_array($finalVal, $Filterarray)){ // check if available in filter array
                $newArr[$key]["ID"] = $value["ID"];
                $newArr[$key]["Name"] = $value["Name"];
                $newArr[$key]["Price"] = $value["Price"];
                $newArr[$key]["Supply"] = $value["Supply"];
                $newArr[$key]["Type"] = $value["Type"];
                //$newArr[$key][] = $finalVal;
            }       
        }   
    }

Output on website:

Array
(
    [1] => Array
        (
            [ID] => 1
            [Name] => Pluisje de Pluimstaart
            [Price] => 75
            [Supply] => 0
            [Type] => Pluimstaart
        )
    [5] => Array
        (
            [ID] => 69
            [Name] => Blizzard de Pluimstaart
            [Price] => 75
            [Supply] => 1
            [Type] => Pluimstaart
        )
)

The problem here being the skip from 1 to 5, I want the new array to just say

 Array
    (
        [0] => Array
            (
                [ID] => 1
                [Name] => Pluisje de Pluimstaart
                [Price] => 75
                [Supply] => 0
                [Type] => Pluimstaart
            )
        [1] => Array
            (
                [ID] => 69
                [Name] => Blizzard de Pluimstaart
                [Price] => 75
                [Supply] => 1
                [Type] => Pluimstaart
            )
    )
  • 写回答

3条回答 默认 最新

  • doumie6223 2018-08-21 10:40
    关注

    Please use array_values function like this:

      $newArr = array(1 => array("one", "two", "three"), 5 => array("horse", "cat", "monkey"));
    
    print_r($newArr); // test print
    
    $newArr = array_values($newArr);
    
    print_r($newArr); // test print
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?