Skip to content
Advertisement

How to change date format in multidimensional array and iterate all array with key

I have a multidimensional array with key and value and some key is empty also. Then I want to set a value for internal not empty array.

 $oldArray = array("Lexus LS600" => array(), 
                   "Toyota Alphard" => array(), 
                   "Benz S550" => array(0 => array(
                        "card_no" => "G2FPCBS3",
                        "travel_date" => "2020-09-10"
                        "travel_time" => "16:15:00",
                        "car_id" => 12,
                        "return_time" => "17:25")),
                   "BMW X6" => array());

I had this array but I want to set return_time 00:00 all over array. I tried foreach loop but foreach is remove empty array but I want empty array also.

I want this type array:-

$newArray = array("Lexus LS600" => array(), 
                  "Toyota Alphard" => array(), 
                  "Benz S550" => array(0 => array(
                       "card_no" => "G2FPCBS3",
                       "travel_date" => "2020-09-10"
                       "travel_time" => "16:15:00",
                       "car_id" => 12,
                       "return_time" => "00:00")),
                  "BMW X6" => array());

Advertisement

Answer

Try this foreach again, I think it will solve your problem if I understood you correctly.

foreach ($arrays as $key => $values) {
    if (is_array($values)) {
        if (count($values)) {
            foreach ($values as $index => $data) {
                $arrays[$key][$index]['return_time'] = "00:00";
            }
        } else {
            $arrays[$key] = $values;
        }
    }
}

It will change return_time to “00:00” and also retain the empty index to your array.

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