Can you help me with my problem? I have a problem in array. To make my problem short here it is. I created a code that can automatically replace and add a value in an array using an onclick. But javascript here is not an issue. The scenario is, after the user clicked the button or submit. The code will validate if it is existing in the array. By the way I have a serialized array. If the array is not existed in the list it will automatically added to the end of the array. If existed it will just replace the certain parts of array.
Here's my code:
$data = unserialize(file_get_contents('addresses.txt'));
//fn_print_r($data);
$data_update = array(
'restaurant_id' => $restaurant_id,
'new_lat' => $new_lat_entry,
'new_long' => $new_long_entry,
'date_updated' => date('Y-m-d H:i:s')
);
$iterate = -1;
foreach($data as $key => &$value){
$iterate++;
//echo $data[$iterate]['restaurant_id'];
if($data[$iterate]['restaurant_id'] == $data_update['restaurant_id']){
$data[] = $data_update;
}else{
array_push($data,$data_update);
break;
}
}
This is the the sample serialized array:
a:1:{i:0;{s:13:"id";s:4:"3";s:7:"name";s:8:"lmlm";}}
This is the example:
'BASE ARRAY'
Array(
[0] => Array(
[id] => 1,
[name] => popo
),
[1] => Array(
[id] => 3,
[name] => wewe
),
[2] => Array(
[id] => 2,
[name] => lolo
),
)
'NEW ARRAY'
Array(
[id] => 2,
[name] => fifi
)
If the id is found in the 'BASE' array. The output is:
Array(
[0] => Array(
[id] => 1,
[name] => popo
),
[1] => Array(
[id] => 3,
[name] => wewe
),
[2] => Array(
[id] => 2,
[name] => fifi
),
)
If not found. If we have a new array like this:
Array(
[id] => 6,
[name] => fifi
)
The result should be:
Array(
[0] => Array(
[id] => 1,
[name] => popo
),
[1] => Array(
[id] => 3,
[name] => wewe
),
[2] => Array(
[id] => 2,
[name] => lolo
),
[3] => Array(
[id] => 6,
[name] => fifi
),
)