Skip to content

PHP: Modify an associative array based on value of a key

I have an array like this

Array
(
    [id] => 3
    [type] => default
    [place] => 1
)
Array
(
    [id] => 3
    [type] => default
    [place] => 2
)
Array
(
    [id] => 3
    [type] => default
    [place] => 3
)

This array is created from this php

for($count=1;$count <= 3;$count++){
$places_array = array(
    "id" => "3",
    "type" => "default",
    "place" => $count,
);
}

Now I want to change the result of this array if the place is found by the php mysql data. For example I have this array.

Array
(
    [id] => 7
    [type] => 1
    [place] => 2
    [description] => This is item place 2
    [image] => this_is_the_image.png
)

As you can see that the second array is in “place 2”. Now I want the result be like this

Array
(
    [id] => 3
    [type] => default
    [place] => 1
)
Array
(
    [id] => 7
    [type] => 1
    [place] => 2
    [description] => This is item place 2
    [image] => this_is_the_image.png
)
Array
(
    [id] => 3
    [type] => default
    [place] => 3
)

How to achieve this? I have already done with array_search function but no luck. anyone please help me

=======================EDIT FULL CODE================================ Here is the code, I’m showing the data from database and call it in while loop function

for($count=1;$count <= $max_places;$count++){
    $array[] = array(
        "id" => $res['id'],
        "type" => "default",
        "place" => $count
    );
    while($arr = $stmts->fetch()){
        $key = array_search($arr['place'], array_column($array, 'place'));
        if($key && array_key_exists($key, $array)) {
            $array[$key] = [
                "id" => $arr['id'],
                "type" => $arr['type'],
                "place" => $arr['place'],
                "url" => $arr['url'],
                "image" => $arr['image']
            ];
        }
    }
}

=========================SWAPPED CODE==============================

while($arr = $stmts->fetch()){
        $array[] = [
                "id" => $arr['id'],
                "type" => $arr['type'],
                "place" => $arr['place'],
                "url" => $arr['url'],
                "image" => $arr['image']
        ];
    for($count=1;$count <= $max_places;$count++){
    $key = array_search($arr['place'], array_column($array, 'place'));
    if($key && array_key_exists($key, $array)) {
        $array[] = array(
            "id" => $res['id'],
            "type" => "default",
            "place" => $count
        );
        }
    }  
}

Advertisement

Answer

Found the answer, thanks for all help.

Here is the trick

first we need to determine the main array like this

for($count=1;$count <= $max_places;$count++){
        $array[$count] = array(
            "id" => $res['id'],
            "type" => "default",
            "place" => $count
        );
} 

Then we need to find which place is not available and show the matches.

while($arr = $stmts->fetch()){
    $key = array_search($arr['place'], array_column($array, 'place'));
    $key+=1;
    if($key && array_key_exists($key, $array)) {
        $array[$arr['place']] = [
            "id" => $arr['id'],
            "type" => $res['type'],
            "place" => $arr['place'],
            "url" => $arr['url'],
            "image" => $arr['image']
        ];
    }
}

The trick is in the

$key+=1;

Because the default key in array is 0. Hope this can help others

User contributions licensed under: CC BY-SA
1 People found this is helpful