Solved

# array

Posted on 2006-04-21
225 Views
if ship_date is 0000-00-00, then it has to be empty.
if ship_date is 2006-04-21, then it has to be 04/21/2006.

How can i get those results based on the bottom code.

foreach(\$csvData as \$row) {

//\$row[] = ('0000-00-00' != \$row ) ? date('m/d/Y', strtotime(\$row)) : '';
print_r(\$row);
fputcsv(\$fp,\$row);
}

result looks like

Array
(
[0] => order_number
[1] => LNnumber
[2] => order_approval_status
[3] => ship_date
[4] => tracking_number
)
Array
(
[0] => M0101
[1] => KVTR
[2] => 1
[3] => 0000-00-00
[4] =>
)
Array
(
[0] => M0102
[1] => KVTR-6
[2] => 1
[3] => 0000-00-00
[4] =>
)
Array
(
[0] => M0103
[1] => KVTR-1
[2] => 4
[3] => 0000-00-00
[4] =>
)
0
Question by:bbvic

LVL 40

Expert Comment

foreach(\$csvData as \$row)
{
\$row[3] = ('0000-00-00' != \$row[3] ) ? date('m/d/Y', strtotime(\$row[3])) : '';
print_r(\$row);
fputcsv(\$fp,\$row);
}

Sorry. Didn't quite understand the other question.
0

Author Comment

if i use,
foreach(\$csvData as \$row)
{
\$row[3] = ('0000-00-00' != \$row[3] ) ? date('m/d/Y', strtotime(\$row[3])) : '';
print_r(\$row);
fputcsv(\$fp,\$row);
}

the result looks like
--------------------------
Array
(
[0] => jupiter_order_number
[1] => LNnumber
[2] => order_approval_status
[3] => 12/31/1969
[4] => tracking_number
)
Array
(
[0] => M^IND-IND0100~0101
[1] => KVTR-6NYLQF
[2] => 1
[3] =>
[4] =>
)
Array
(
[0] => M^IND-IND0100~0102
[1] => KVTR-6NYLQG
[2] => 1
[3] =>
[4] =>
)
Array
(
[0] => M^IND-IND0100~0103
[1] => KVTR-6NYLQH
[2] => 4
[3] =>
[4] =>
)

The result that I want

Array
(
[0] => jupiter_order_number
[1] => LNnumber
[2] => order_approval_status
[3] => ship_date
[4] => tracking_number
)
Array
(
[0] => M 0101
[1] => KVTR-6
[2] => 1
[3] => 04/21/2006
[4] =>
)
Array
(
[0] => M0102
[1] => KVTR-6NYLQG
[2] => 1
[3] =>
[4] =>
)
Array
(
[0] => M0103
[1] => KVTRH
[2] => 4
[3] =>
[4] =>
)

0

LVL 11

Expert Comment

The following code should work

foreach(\$csvData as \$row1) {
\$row = array();
foreach(\$row1 as \$values) {
if(\$values == '0000-00-00') {
\$values = "";
else {
\$values = preg_replace('/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/','\3/\4/\1\2',\$values);
}
\$row[] = \$values;
}
print_r(\$row);
fputcsv(\$fp,\$row);
}
0

LVL 11

Accepted Solution

oops missing a }
here is the correct one
foreach(\$csvData as \$row1) {
\$row = array();
foreach(\$row1 as \$values) {
if(\$values == '0000-00-00') {
\$values = "";
} else {
\$values = preg_replace('/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/','\3/\4/\1\2',\$values);
}
\$row[] = \$values;
}
print_r(\$row);
fputcsv(\$fp,\$row);
}
0

Author Comment

thanks
0

## Featured Post

This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.