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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料