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

过滤多维数组中的字段,根据键将它们放在新数组中。 [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
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dtxox60220 2018-08-21 10:27

    Try the array function, array_values()

    Refer: http://php.net/manual/en/function.array-values.php

    评论
    解决 无用
    打赏 举报
  • dongyuan7981 2018-08-21 10:42

    You are setting a key for each product filtered in your $newArr, this means that you'll get the new array with the original indexes.

    To avoid it, just remove the $key index from the $newArr[] initialization.

    $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[]["ID"] = $value["ID"];
                    $newArr[]["Name"] = $value["Name"];
                    $newArr[]["Price"] = $value["Price"];
                    $newArr[]["Supply"] = $value["Supply"];
                    $newArr[]["Type"] = $value["Type"];
                    //$newArr[][] = $finalVal;
                }       
            }   
        }
    

    or, even better

    if(in_array($finalVal, $Filterarray)){
        array_push($newArr, array(
            "ID" => $value["ID"],
            "Name" => $value["Name"],
            "Price" => $value["Price"],
            "Supply" => $value["Supply"],
            "Type" => $value["Type"]
        );
    }
    

    If you need to keep track of the $key, just store it in an additional $newArr parameter.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题