Link to home
Start Free TrialLog in
Avatar of MrBeedge
MrBeedgeFlag for United States of America

asked on

PHP array formatting

I have the following PHP array

JobNo: 15-001:001
TaskNo: 15-001:001
Hours: 2

JobNo: 15-002:002
TaskNo: 15-002:002
Hours: 3

and so on.  I want to convert the values in the JobNo field to only keep everything left of the colon (ie 15-001) and for the TaskNo field only keep everything right of the colon (ie 001).  The resulting array would be:

JobNo: 15-001
TaskNo: 001
Hours: 2

JobNo: 15-002
TaskNo: 002
Hours: 3

Current array is in PHP and would like the conversion to happen on the PHP side.  Thanks!
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Something like this.  The numbers represent the positions in the sub-arrays.
foreach($array as $val) {
$JobNo = explode(':',$val[0]);
echo $JobNo[0];
$TaskNo = explode(':',$val[1]);
echo $TaskNo[1];
echo $val[2];

Open in new window

More info: http://php.net/manual/en/control-structures.foreach.php
Don't you need an "&" to modify the array inside the loop?

foreach ($yourarray as &$row){
  $job = explode(":", $row["JobNo"]);
  $row["JobNo"] = $job[0];
  $task = explode(":", $row["TaskNo"]);
  $row["TaskNo"] = $task[0];
}
unset($row);

Open in new window

HTH,
Dan
SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial