【问题标题】:foreach appending extra values in each iteration php?foreach 在每次迭代 php 中附加额外的值?
【发布时间】:2022-06-10 17:05:40
【问题描述】:

我有一个数组,我想按具有特定 ID 的数组分组。 例如 $newArr =

Array
(
    [0] => Array
        (
            [0] => palanpur
            [city] => palanpur
            [1] => category_1
            [cat_name] => category_1
            [2] => 10000024
            [id] => 10000024
            [3] => 0
            [search_type] => 0
        )

    [1] => Array
        (
            [0] => palanpur
            [city] => palanpur
            [1] => category_2
            [cat_name] => category_2
            [2] => 10000086
            [id] => 10000086
            [3] => 2
            [search_type] => 2
        )

    [2] => Array
        (
            [0] => pattukottai
            [city] => pattukottai
            [1] => category_1
            [cat_name] => category_1
            [2] => 10000024
            [id] => 10000024
            [3] => 0
            [search_type] => 0
        )

    [3] => Array
        (
            [0] => pattukottai
            [city] => pattukottai
            [1] => category_2
            [cat_name] => category_2
            [2] => 10000086
            [id] => 10000086
            [3] => 2
            [search_type] => 2
        )

    [4] => Array
        (
            [0] => puttur
            [city] => puttur
            [1] => category_1
            [cat_name] => category_1
            [2] => 10000024
            [id] => 10000024
            [3] => 0
            [search_type] => 0
        )

    [5] => Array
        (
            [0] => puttur
            [city] => puttur
            [1] => category_2
            [cat_name] => category_2
            [2] => 10000086
            [id] => 10000086
            [3] => 2
            [search_type] => 2
        )

    [6] => Array
        (
            [0] => category_3
            [cat_name] => category_3
            [1] => 10000059
            [id] => 10000059
            [2] => 0
            [search_type] => 0
        )

    [7] => Array
        (
            [0] => category_4
            [cat_name] => category_4
            [1] => 10000060
            [id] => 10000060
            [2] => 0
            [search_type] => 0
        )

)

得到上面的数组,我想要这样的结果:(id 明智和该 id 内的所有城市明智):

Array
(
    [10000024] => Array
        (
            [id] => 10000024
            [cat_name] => category_1
            [field] => search_type
            [palanpur] => 0
            [pattukottai] => 0
            [puttur] => 0
        )

    [10000086] => Array
        (
            [id] => 10000086
            [cat_name] => category_2
            [field] => search_type
            [palanpur] => 2
            [pattukottai] => 2
            [puttur] => 2
        )

    [10000059] => Array
        (
            [id] => 10000059
            [cat_name] => category_3
            [field] => search_type
            [palanpur] => 0
            [pattukottai] => 0
            [puttur] => 0
            [universal] => 0
        )

    [10000060] => Array
        (
            [id] => 10000060
            [cat_name] => category_4
            [field] => search_type
            [palanpur] => 0
            [pattukottai] => 0
            [puttur] => 0
            [universal] => 0
        )

)

为此,我试图通过 $result 中的 id 获取数据,但没有得到正确的结果。 第一个数组循环以获取我想要的新数组: $fields = //用户输入;

foreach($newArr as $k1 =>  $val2){ 
                    $newArray['id'] = $val2['id'];
                    $newArray['cat_name'] = $val2['cat_name'];
                    $newArray['field'] = $fields;
                    if (array_key_exists('city', $val2)){ 
                        $city1 = $val2['city'];
                    } 
                    else { 
                        $city1 = "universal";
                    }
                    $newArray[$city1] = $val2[$fields];
                    $newArray1[] =  $newArray;
                }

在 $newArray1 中,我得到了这样的附加数据:错误的输出:

Array
(
    [0] => Array
        (
            [id] => 10000024
            [cat_name] => category_1
            [field] => search_type
            [palanpur] => 0
        )

    [1] => Array
        (
            [id] => 10000086
            [cat_name] => category_2
            [field] => search_type
            [palanpur] => 2
        )

    [2] => Array
        (
            [id] => 10000024
            [cat_name] => category_1
            [field] => search_type
            [palanpur] => 2
            [pattukottai] => 0
        )

    [3] => Array
        (
            [id] => 10000086
            [cat_name] => category_2
            [field] => search_type
            [palanpur] => 2
            [pattukottai] => 2
        )

    [4] => Array
        (
            [id] => 10000024
            [cat_name] => category_1
            [field] => search_type
            [palanpur] => 2
            [pattukottai] => 2
            [puttur] => 0
        )

    [5] => Array
        (
            [id] => 10000086
            [cat_name] => category_2
            [field] => search_type
            [palanpur] => 2
            [pattukottai] => 2
            [puttur] => 2
        )

    [6] => Array
        (
            [id] => 10000059
            [cat_name] => category_3
            [field] => search_type
            [palanpur] => 2
            [pattukottai] => 2
            [puttur] => 2
            [universal] => 0
        )

    [7] => Array
        (
            [id] => 10000060
            [cat_name] => category_4
            [field] => search_type
            [palanpur] => 2
            [pattukottai] => 2
            [puttur] => 2
            [universal] => 0
        )

)

请帮助获得预期的输出。

【问题讨论】:

    标签: php foreach


    【解决方案1】:

    你快到了,你只是在搞乱字符串/数字。

    如果你想完全复制新数组中的旧值 => id,

    只是

    $newA = array();
    foreach ($oldA as $k => $v) {
        $code = $v["id"]; 
        $newA["$code"] = $v;
    }
    

    您会得到一个结果数组,其中包含所有 ID 作为键,以及所有相同的值作为值。

    无论如何,您在这些数组中使用了太多信息。 您只需要 4 个值: id、city、cat 和搜索类型 id 将是一个键,因此在值中重复它是没有用的

    所以,最好在复制时清理数组:

    $newA = array();
    foreach ($oldA as $k => $v) {
        // get the ID
        $id = $v["id"]; 
    
        // Remove the ID, because it will be the first level key of the new array. 
        // If you want you can remove this next line, to keep the ID between the values too
        unset($v["id"]);
    
        foreach ($v as $k1 => $v1) {
    
            // Remove all numeric keys, because those are duplicated infos
            if (is_int($k1)) continue;
    
            // Here it is. Set the array => $id => $k1 at value $v1
            // It means that, for example, $newA['10000055']['city'] = "palanpur"
            // Notice that you need to use commas, to convert id in string. 
            // As far as I know, You can't set the numeric value of the array at position 10000025... 
            // so you need to pass it as STRING! 
            $newA["$id"][$k1] = $v1; 
        }
    }
    

    这样你就得到了这些类型的数组:

    array {
      [10000024]=>
        array(3) {
            ["city"]=> "palanpur"
            ["cat_name"]=> "cat"
            ["search_tipe"]=> 0
      }
    }
    

    只需要最少的信息,节省内存,让应用程序更快,更易于管理... ID 将是键,子键映射为旧数组。

    【讨论】:

    • 天哪,你的反应比我还快。非常优雅的解决方案!
    猜你喜欢
    • 2018-02-21
    • 1970-01-01
    • 2018-09-01
    • 2022-01-20
    • 2017-05-01
    • 1970-01-01
    • 2017-01-15
    • 2011-09-21
    • 1970-01-01
    相关资源
    最近更新 更多