Home > Mobile >  How to Remove Elements from array in php after some given character?
How to Remove Elements from array in php after some given character?

Time:08-19

I want to Remove all the Elements from my array which is after , but I am unable to do this.

I have an array like this => ["18-08-2022, 05:08:23pm","18-08-2022, 05:09:05pm"]

and I want to print array something like this => ["18-08-2022","18-08-2022"]

I want to remove Elements after the ,

This is what I tried

       <?php
         
   
         while ($row = mysqli_fetch_array($result)) {
                $Etime[] = $row["Etime"];


                $Etime1[]=$row["Etime"];
                $E2[]=substr($Etime1[],',',true);
              

         }
       
   
       

     
         $Etime = json_encode($Etime);
         $Etime1=json_encode($Etime1);
       
         echo $Etime1

        ?>

CodePudding user response:

Here's a simple php that generates the output you want. Since you only need date bits I have provided you with only the date bits. You can get general idea from here

$arr =  ["18-08-2022, 05:08:23pm","18-08-2022, 05:09:05pm"];

//
// sample output: [ "18-08-2022", "18-08-2022" ]
//
$dateMappedToYourFormat = array_map(function($dt) {
    return explode(",", $dt)[0]; // getting everything before comma
}, $arr);

echo implode(",", $dateMappedToYourFormat); //to view your result

CodePudding user response:

this is easy- steps to do so- 1.Parse your array as string. 2.store that in a variable. 3. then use a if loop and use php explode funtion. explode function will seperate that string elements by the seperator in this case the "," you want to remove.

 explode(string $separator, string $string)

where,
separator-The boundary string.
string-The input string.
limit

example=
$text = "hello,there";

//using explode-
var_dump( explode( ',', $input2 ) );

output=

array(2)
(
    [0] => string(5) "hello"
    [1] => string(5) "there"
)


more about explode() here- https://www.php.net/manual/en/function.explode.php
  •  Tags:  
  • php
  • Related