0

I am using laravel 5. I queried the list of dates from this sql statement:

//query tarikh tamat kursus                    
$datatarikhTamat = DB::table('itemregistrationdetail')
                  ->select('itemregistrationdetail.daytamat_kursus', 'itemregistrationdetail.monthtamat_kursus', 'itemregistrationdetail.yeartamat_kursus')
                  ->where('itemregistrationdetail.itemregistrationid', $id)
                  ->get();
$data = json_decode($datatarikhTamat, true); 

The decoded query will show this output:

      array:4 [▼
      0 => array:3 [▼
      "daytamat_kursus" => 28
      "monthtamat_kursus" => "July"
      "yeartamat_kursus" => 2011
    ]
     1 => array:3 [▼
     "daytamat_kursus" => 27
     "monthtamat_kursus" => "0"
     "yeartamat_kursus" => 2013
    ]
    2 => array:3 [▼
     "daytamat_kursus" => 14
     "monthtamat_kursus" => "0"
     "yeartamat_kursus" => 2013
    ]
    3 => array:3 [▼
     "daytamat_kursus" => 11
     "monthtamat_kursus" => "April"
     "yeartamat_kursus" => 2014
     ]
    ]

day, month and year of each date are from different columns of the table. I want to have those date to grouped together and displayed in a table. For example: The muldtidimensiarray of :

    array:12 [▼
   0 => "28 July 1022"
   1 => "27 0 2013"
   2 => "14 0 2013"
   3 => "11 April 2014"
    ]

can be grouped using implode to turn into a string "11 April 2014".

I had using this code to implode the array but it results differently.

//tarikh tamat kursus
    //**************** format the value of the date************************  
    $data = json_decode($datatarikhTamat, true); 

    $val_arr = array();

    foreach($data  as $val)
    {
        foreach($val as $key => $value)
        {
             $val_arr[] = $value;
        }
    }

    $dataTarikhTamat = implode("-", $val_arr);
    dd($val_arr);

This will produce this output:

 array:12 [▼
   0 => 28
   1 => "July"
   2 => 2011
   3 => 27
   4 => "0"
   5 => 2013
   6 => 14
   7 => "0"
   8 => 2013
   9 => 11
   10 => "April"
   11 => 2014
   ]

How can I modify the codes to get an indexed array of the dates?

1 Answer 1

1

I believe you have gone to far down in your foreach() loops. You can implode() the data on the first level of the array:

$dates = json_decode($datatarikhTamat, true); 

$date_arr = array();

foreach($dates as $date)
{
    $date_arr[] = implode(' ', array_values($date));
}

This should result in the expected format. Using array_values() will only return the values of the $date array.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, you are right. Your solution help me to get out of the puzzle...thank you so much @thisiskelvin for helping me...it works as it should be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.