MrBeedge
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!
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!
Don't you need an "&" to modify the array inside the loop?
Dan
foreach ($yourarray as &$row){
$job = explode(":", $row["JobNo"]);
$row["JobNo"] = $job[0];
$task = explode(":", $row["TaskNo"]);
$row["TaskNo"] = $task[0];
}
unset($row);
HTH,Dan
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
More info: http://php.net/manual/en/control-structures.foreach.php