简体   繁体   中英

foreach appending extra values in each iteration php?

I am having one array and i want to group by that array with particular id. eg $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
        )

)

getting above array and i want result like this :(id wise and all city wise inside that 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
        )

)

for this i am trying to get data by id in $result but not getting proper result. first array looping to get new array as i want : $fields = //user input;

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;
                }

inside $newArray1 i am getting appended data like this : wrong output :

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
        )

)

Please help to get expected output.

You are almost at the point, you just messing up with strings/numbers.

If you want to copy exactly the old value inside the new array => id,

just

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

you get a result array that contains all IDs as keys, and all the same values as values. Notice the commas. You need to set the ID value as a string, in the new key. As far as I know, you can't set the numeric value number 1000024 of an empty array. Imagine an array as tiles. It's like putting the tile #100024 without any tile below it!

Another thing.. you are using too much information in those arrays. You just need 4 values: id, city, cat, and search type. The id will be the key, so it's unuseful to repeat it in the values

So, it's better to clean the array, while copying it:

$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; 
    }
}

In this way you get an array of these types:

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

with just the minimal information needed, save memory, and make the app faster, and easier to manage... The ID will be the key, and the sub-keys are mapped as the old array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM