sushilsth 0 Light Poster
array (size=4)
  0 => 
    array (size=7)
      'id' => string '75' (length=2)
      'room_name' => string 'Economy ' (length=8)
      'no_of_room' => string '0' (length=1)
      'price' => string '500' (length=3)
      'description' => string 'asdfasf' (length=7)
      'image' => string '' (length=0)
      'hotel_id' => string '1' (length=1)
  1 => 
    array (size=7)
      'id' => string '70' (length=2)
      'room_name' => string 'Couple Room' (length=11)
      'no_of_room' => string '0' (length=1)
      'price' => string '500' (length=3)
      'description' => string 'sadsadasdas' (length=11)
      'image' => string 'DSCF363.jpg' (length=11)
      'hotel_id' => string '1' (length=1)


      Hi  this is my array when i do var_dump. Now i want to make array push and modify array as below. I googled alot for this but cant find any soultion. Plz help me. I would be greatful.


array (size=4)
  0 => 
    array (size=7)
      'id' => string '75' (length=2)
      'room_name' => string 'Economy ' (length=8)
      'no_of_room' => string '0' (length=1)
      'price' => string '500' (length=3)
      'description' => string 'asdfasf' (length=7)
      'image' => string '' (length=0)
      'hotel_id' => string '1' (length=1)
      'check_in_date' => string '2014-1-1' (length=8)
      'check_out_date' => string '2014-1-3' (length=8)
  1 => 
    array (size=7)
      'id' => string '70' (length=2)
      'room_name' => string 'Couple Room' (length=11)
      'no_of_room' => string '0' (length=1)
      'price' => string '500' (length=3)
      'description' => string 'sadsadasdas' (length=11)
      'image' => string 'DSCF363.jpg' (length=11)
      'hotel_id' => string '1' (length=1)
      'check_in_date' => string '2014-1-1' (length=8)
      'check_out_date' => string '2014-1-3' (length=8)

Dani AI

Generated

— you have an array of associative arrays (one entry per room) and you want to add check_in_date and check_out_date to every inner array. Two simple, safe options follow.

// in-place modification using a reference
foreach ($rooms as &$room) {
    $room['check_in_date']  = '2014-01-01'; // prefer YYYY-MM-DD
    $room['check_out_date'] = '2014-01-03';
}
unset($room); // important: break the reference to avoid later bugs
// functional approach that returns a new array (no references)
$rooms = array_map(function($r){
    $r['check_in_date']  = '2014-01-01';
    $r['check_out_date'] = '2014-01-03';
    return $r;
}, $rooms);

If dates differ per room, build a lookup keyed by the room id and apply it inside the loop:

$datesById = [
    123 => ['in'=>'2014-01-01','out'=>'2014-01-03'], // replace 123 with actual ids
];

foreach ($rooms as &$room) {
    $id = (int) $room['id'];
    if (isset($datesById[$id])) {
        $room['check_in_date']  = $datesById[$id]['in'];
        $room['check_out_date'] = $datesById[$id]['out'];
    }
}
unset($room);

Troubleshooting notes: using foreach ($rooms as $room) (without &) will not change the original array. Always unset the reference variable after the loop. Use ISO date format (YYYY-MM-DD) or DateTime objects for reliable parsing and comparison. After changes, inspect the structure (e.g., print_r/var_dump) before persisting back to a database or JSON.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.