Jump to content

Help With preg_replace adding extra Gaps that arn't in the CSV file


sniesler08

Recommended Posts

 

Array(2) and others are just an empty array for some reason i do think it is to do with the preg_replace but don't have enough of an understanding about it to sort it out any help would be appreciated

 

i don't particularly know if i need preg replace but i have tried with out it or other options and nothing has worked so far.

 

Heres the code

 

<?php
$file = "widget.csv";
$delimiter = ",";
$enclosure = '"';

@$fp = fopen($file, "r") or die("Could not open file for reading");
while (!feof($fp))
{
    $tmpstr = fgets($fp, 100);
    $shop[] = preg_replace("//", "", $tmpstr);
/* $tmpstr = $shop; */
}
for ($i=0; $i < count($shop); $i++)
{
    $shop[$i] = explode($delimiter, $shop[$i]);
}
print_r ($shop);
echo "<h1>Manual access to each element</h1>";

echo $shop[0][0]." costs ".$shop[0][1]." and you get ".$shop[0][2]."<br />";
echo $shop[1][0]." costs ".$shop[1][1]." and you get ".$shop[1][2]."<br />";
echo $shop[2][0]." costs ".$shop[2][1]." and you get ".$shop[2][2]."<br />";

echo "<h1>Using loops to display array elements</h1>";

echo "<table>";
/* for ($row = 0; $row < 3; $row++) */
        for ($row=0; $row<count($shop); $row++)
{
    echo "<th colspan='3'><b>" . $shop[$row][0]."</b></th>";
    echo "<tr>";

    /* for ($col = 1; $col < 6; $col++) */
for ($col=1; $col < count($shop); $col++)
    {
        echo "<td>".$shop[$row][$col]."</td>";

    }

    echo "</tr>";
    /* echo "</li>"; */
}
echo "</table>";
?>

 

This is the CSV File widget.csv

 

A,Airlie Beach,Andergrove,Alexandra,Armstrong Beach,Alligator Creek,,,,,,,,,,,,,,

B,Bucasia,Blacks Beach,Beaconsfield,Bakers Creek,Balberra,Bloomsbury,Breadalbane,Ball Bay,Belmunda,,,,,,,,,,

C,Cannonvale,Calen,Crystal Brook,Cremorne,Chelona,Campwin Beach,Cape Hillsborough,Conway,,,,,,,,,,,

D,Dows Creek,Dumbleton,Dolphin Heads,,,,,,,,,,,,,,,,

E,Eimeo,Eton,Erakala,,,,,,,,,,,,,,,,

F,Foulden,Foxdale,Flametree,Farleigh,Freshwater Point,,,,,,,,,,,,,,

G,Glen Isla,Glenella,,,,,,,,,,,,,,,,,

H,Homebush,Hampden,,,,,,,,,,,,,,,,,

I,,,,,,,,,,,,,,,,,,,

J,Jubilee Pocket,,,,,,,,,,,,,,,,,,

K,Kinchant Dam,Kolijo,Koumala,Kuttabul,,,,,,,,,,,,,,,

L,Laguna Quays,,,,,,,,,,,,,,,,,,

M,McEwens Beach,Mackay,Mackay Harbour,Mandalay,Marian,Mia Mia,Middlemount,Midge Point,Mirani,Moranbah,Mount Charlton,Mount Jukes,Mount Julian,Mount Marlow,Mount Martin,Mount Ossa,Mount Pelion,Mount Pleasant,Mount Rooper

N,Narpi,Nebo,Nindaroo,North Eton,,,,,,,,,,,,,,,

O,Oakenden,Ooralea,,,,,,,,,,,,,,,,,

P,Palmyra,Paget,Pindi Pindi,Pinevale,Pioneer Valley,Pleystowe,Preston,Proserpine,,,,,,,,,,,

Q,,,,,,,,,,,,,,,,,,,

R,Racecourse,Richmond,Riordanvale,Rosella,Rural View,,,,,,,,,,,,,,

S,St Helens Beach,Sandiford,Sarina,Seaforth,Slade Point,Shoal Point,Shute Harbour,Shutehaven,Strathdickie,Sugarloaf,Sunnyside,,,,,,,,

T,Te Kowai,The Leap,,,,,,,,,,,,,,,,,

U,,,,,,,,,,,,,,,,,,,

V,Victoria Plains,,,,,,,,,,,,,,,,,,

W,Wagoora,Walkerston,Woodwark,,,,,,,,,,,,,,,,

X,,,,,,,,,,,,,,,,,,,

Y,Yalboroo,,,,,,,,,,,,,,,,,,

Z,,,,,,,,,,,,,,,,,,,

 

It is generating this as the output of the Array as this but the array(2) and others as you can see are empty C should of been the first one after B field had finished.

 

Array ( [0] => Array ( [0] => A [1] => Airlie Beach [2] => Andergrove [3] => Alexandra [4] => Armstrong Beach [5] => Alligator Creek [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [1] => Array ( [0] => B [1] => Bucasia [2] => Blacks Beach [3] => Beaconsfield [4] => Bakers Creek [5] => Balberra [6] => Bloomsbury [7] => Breadalbane [8] => Ball Bay [9] => Belmunda [10] => ) [2] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => ) [3] => Array ( [0] => C [1] => Cannonvale [2] => Calen [3] => Crystal Brook [4] => Cremorne [5] => Chelona [6] => Campwin Beach [7] => Cape Hillsborough [8] => Conway [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [4] => Array ( [0] => ) [5] => Array ( [0] => D [1] => Dows Creek [2] => Dumbleton [3] => Dolphin Heads [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [6] => Array ( [0] => E [1] => Eimeo [2] => Eton [3] => Erakala [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [7] => Array ( [0] => F [1] => Foulden [2] => Foxdale [3] => Flametree [4] => Farleigh [5] => Freshwater Point [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [8] => Array ( [0] => G [1] => Glen Isla [2] => Glenella [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [9] => Array ( [0] => H [1] => Homebush [2] => Hampden [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [10] => Array ( [0] => I [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [11] => Array ( [0] => J [1] => Jubilee Pocket [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [12] => Array ( [0] => K [1] => Kinchant Dam [2] => Kolijo [3] => Koumala [4] => Kuttabul [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [13] => Array ( [0] => L [1] => Laguna Quays [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [14] => Array ( [0] => M [1] => McEwens Beach [2] => Mackay [3] => Mackay Harbour [4] => Mandalay [5] => Marian [6] => Mia Mia [7] => Middlemount [8] => Midge Point [9] => Mirani [10] => Moranb ) [15] => Array ( [0] => ah [1] => Mount Charlton [2] => Mount Jukes [3] => Mount Julian [4] => Mount Marlow [5] => Mount Martin [6] => Mount Ossa [7] => Mount Pelion [8] => Mount ) [16] => Array ( [0] => Pleasant [1] => Mount Rooper ) [17] => Array ( [0] => N [1] => Narpi [2] => Nebo [3] => Nindaroo [4] => North Eton [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [18] => Array ( [0] => O [1] => Oakenden [2] => Ooralea [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [19] => Array ( [0] => P [1] => Palmyra [2] => Paget [3] => Pindi Pindi [4] => Pinevale [5] => Pioneer Valley [6] => Pleystowe [7] => Preston [8] => Proserpine [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [20] => Array ( [0] => Q [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [21] => Array ( [0] => R [1] => Racecourse [2] => Richmond [3] => Riordanvale [4] => Rosella [5] => Rural View [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [22] => Array ( [0] => S [1] => St Helens Beach [2] => Sandiford [3] => Sarina [4] => Seaforth [5] => Slade Point [6] => Shoal Point [7] => Shute Harbour [8] => Shutehaven [9] => Strath ) [23] => Array ( [0] => dickie [1] => Sugarloaf [2] => Sunnyside [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => ) [24] => Array ( [0] => T [1] => Te Kowai [2] => The Leap [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [25] => Array ( [0] => U [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [26] => Array ( [0] => V [1] => Victoria Plains [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [27] => Array ( [0] => W [1] => Wagoora [2] => Walkerston [3] => Woodwark [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [28] => Array ( [0] => X [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [29] => Array ( [0] => Y [1] => Yalboroo [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) [30] => Array ( [0] => Z [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => ) )
Link to comment
Share on other sites

I don't know why you're trying to use preg_replace() in there.

 

But moot point: most of that code can be replaced with a call to fgetcsv() and array_filter().

while (!feof($fp)) {
    $array = array_filter(fgetcsv($fp, 9999));
    $letter = array_shift($array);
    echo "Letter {$letter}: ";
    print_r($array);
}

Link to comment
Share on other sites

EDIT: I took a slightly different approach so I will post my code even though it is close to requinix's solution. I think mine has the added advantage of a single array in a logical format.

 

$file = "widget.csv";
@$fp = fopen($file, "r") or die("Could not open file for reading");
$outputArray = array();
while(($line = fgetcsv($fp, 1000, ",")) !== FALSE)
{
    $outputArray[array_shift($line)] =  array_filter($line);
}
print_r($outputArray);

 

Here is the first part of the array where the primary index is the associated letter for each group of records.

Array
(
    [A] => Array
        (
            [0] => Airlie Beach
            [1] => Andergrove
            [2] => Alexandra
            [3] => Armstrong Beach
            [4] => Alligator Creek
        )

    [b] => Array
        (
            [0] => Bucasia
            [1] => Blacks Beach
            [2] => Beaconsfield
            [3] => Bakers Creek
            [4] => Balberra
            [5] => Bloomsbury
            [6] => Breadalbane
            [7] => Ball Bay
            [8] => Belmunda
        )

    [C] => Array
        (
            [0] => Cannonvale
            [1] => Calen
            [2] => Crystal Brook
            [3] => Cremorne
            [4] => Chelona
            [5] => Campwin Beach
            [6] => Cape Hillsborough
            [7] => Conway
        )

Link to comment
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.