Jump to content

Recommended Posts

Hello. I have the following script:

 

<?php
$depinfo = "01~01~111,02~02~222,03~03~333";

$depdays = explode(",", $depinfo);
$nodays = count($depdays);

for ($a=0; $a <= ($nodays-2); $a++) {
$dayinfo = explode("~", $depdays[$a]);
$depmo[$a] = $dayinfo[$a];
$depdy[$a] = $dayinfo[$a+1];
$depam[$a] = $dayinfo[$a+2];

echo $depmo[$a]."-".$depdy[$a]."-".$depam[$a]."<br>";


}
?>

 

The output I am trying to achieve is:

01-01-111

02-02-222

03-03-333

 

The output I am getting is:

01-01-111

02-222-

 

Can I please just get an idea where I am going wrong? Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/104241-string-manipulation/
Share on other sites

<?php
$depinfo = "01~01~111,02~02~222,03~03~333";

$depdays = explode(",", $depinfo);
$nodays = count($depdays);

$depmo = array();
$depdy = array(); 
$depam = array(); 

foreach ($depdays as $day) {
    array_push($depmo, substr($day, 0, 2));
    array_push($depdy, substr($day, 3, 2));
    array_push($depam, substr($day, 6, 3));
    echo substr($day, 0, 2)."-".substr($day, 3, 2)."-".substr($day, 6, 3)."<br>"; 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533674
Share on other sites

array_push...really? people still use that?

 

<?php
$depinfo = "01~01~111,02~02~222,03~03~333";
$depmo = $depdy = $depam = array(); 

foreach (explode(",", $depinfo) as $day) {
    $depmo[] = substr($day, 0, 2);
    $depdy[] = substr($day, 3, 2);
    $depam[] = substr($day, 6, 3);
    echo substr($day, 0, 2)."-".substr($day, 3, 2)."-".substr($day, 6, 3)."<br>"; 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533679
Share on other sites

Another note...i personally would use an array of associative arrays so it's all in one variable:

 

<?php
  $depinfo = "01~01~111,02~02~222,03~03~333";
  $deparray = array(); 

  foreach (explode(",", $depinfo) as $day) {
    $item = array(
      'mo' => substr($day, 0, 2),
      'dy' => substr($day, 3, 2),
      'am' => substr($day, 6, 3),
    );
    echo "{$item['mo']}-{$item['dy']}-{$item['am']}<br>";
    $deparray[] = $item;
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533691
Share on other sites

this line:

for ($a=0; $a <= ($nodays-2); $a++) {

should have been

for ($a=0; $a < $nodays; $a++) {

 

and these:

$depmo[$a] = $dayinfo[$a];
$depdy[$a] = $dayinfo[$a+1];
$depam[$a] = $dayinfo[$a+2];

should have been

$depmo[$a] = $dayinfo[0];
$depdy[$a] = $dayinfo[1];
$depam[$a] = $dayinfo[2];

Link to comment
https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533737
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.