Jump to content

Archived

This topic is now archived and is closed to further replies.

croakingtoad

multidimensional array problem

Recommended Posts

Okay, so I have been working on this but am having problems. Basically what's happening is the loop is only exploding $_arr[1][136]. When it gets to $_arr[1][137] it doesn't explode it into an array and when it gets to any iteration beyond that it doesn't do either $_arr[2+][136] nor $_arr[2+][137]

What am I doing wrong here?

Here's the code I have--
[code]
for ($i = 0; $i <= count($_arr); $i++) {
    foreach($_arr[$i] as $key => $row) {
        if($key != 0) {
            if (strpos($row, '|') !== false) {
                $_arr[$i][$key] = explode('|', $_arr[$i][$key]);
                echo '<pre>' . var_export($_arr,true) . '</pre>';
            }
        }
    }
}
[/code]

Here's the output--
[code]
  1 =>
  array (
    0 => '20051230194116769413000000',
    1 => '20060310195836868674000000',
    2 => '00',
    3 => '500169',
    4 => 'Y',
    5 => '20060310190340772208000000',
    6 => '',
    7 => '19990816212109142258000000',
    8 => 'Residential',
    9 => 'Single Family',
    10 => '2005-01-04',
...
    135 => 'EUREKA LAND',
    136 =>
    array (
      0 => 'Bedroom 1:',
      1 => '',
      2 => '',
      3 => '',
      4 => 'Entry',
      5 => ';Bedroom 2:',
      6 => '',
      7 => '',
      8 => '',
      9 => 'Entry',
      10 => ';Kitchen:',
      11 => '',
      12 => '',
      13 => '',
      14 => 'Entry',
      15 => ';Living Room:',
      16 => '',
      17 => '',
      18 => '',
      19 => 'Entry',
      20 => ';',
    ),
    137 => 'Lot Description|Up Slope|Yes;Construction|Vinyl|Yes;Miscellaneous Info|Single Wide|No;Miscellaneous Info|Double Wide|No;Heating|Heat Pump Electric|Yes;Cooling|Heat Pump Electric|Yes;Fireplace|# Fireplaces|0;Appliances|Refrigerator|Yes;Appliances|Range|Yes;Floors|Carpet|Yes;Floors|Vinyl|Yes;Windows|Tilt-In|Yes;Exterior Doors|Insulated|Yes;Porch|Front Porch|Yes;Water Description|Public Water|Yes;Sewer Description|Public Sewer|Yes;Bedrooms|Bedrooms Entry Level|2;Full Baths|Full Baths Entry Lvl|1;',
  ),
  2 =>
  array (
    0 => '20051230194116769413000000',
    1 => '20060310195730720323000000',
    2 => '00',
    3 => '410360',
    4 => 'Y',
    5 => '20060310190426562381000000',
    6 => '20060310190217691034000000',
    7 => '19990816212109142258000000',
    8 => 'Residential',
    9 => 'Single Family',
    10 => '2004-11-29',
...
    135 => '',
    136 => 'Breakfast Area:||||Entry|;Dining Room:||||Entry|;Family Room:||||Entry|;Living Room:||||Entry|;Bedroom 2:||||Upper|;Bedroom 3:||||Upper|;Bedroom 4:||||Upper|;Laundry:||||Upper|;Master Bedroom 1:||||Upper|;',
    137 => 'Lot Description|Gentle Slope|Yes;Construction|Brick|Yes;Construction|Vinyl|Yes;Miscellaneous Info|Single Wide|No;Miscellaneous Info|Double Wide|No;Heating|Forced Air Gas|Yes;Heating|Heat Pump Electric|Yes;Cooling|Central Cooling|Yes;Fireplace|# Fireplaces|1;Fireplace|Family Room|Yes;Interior Features|Breakfast Area|Yes;Interior Features|Gas Log Fireplace|Yes;Exterior Features|Deck|Yes;Exterior Features|Patio|Yes;Exterior Features|Paved Driveway|Yes;Appliances|Dishwasher|Yes;Appliances|Disposer|Yes;Appliances|Microwave Oven|Yes;Appliances|Garage Door Opener|Yes;Appliances|Oven|Yes;Appliances|Range|Yes;Misc Features|Cable TV|Yes;Misc Features|Maint-Free Exterior|Yes;Misc Features|Underground Util|Yes;Floors|Carpet|Yes;Floors|Vinyl|Yes;Floors|Wood|Yes;Windows|Insulated|Yes;Windows|Tilt-In|Yes;Exterior Doors|Insulated|Yes;Porch|Front Porch|Yes;Water Description|Public Water|Yes;Sewer Description|Public Sewer|Yes;Bedrooms|Bedrooms Upper Level|4;Full Baths|Full Baths Upper Lvl|2;Half Baths|Half Baths Entry Lvl|1;',
  ),
[/code]

Share this post


Link to post
Share on other sites
It may be better to write the output to a second array instead of trying to write back to the original.

[code]$_arr2 = array();

for ($i = 0; $i <= count($_arr); $i++) {
    foreach($_arr[$i] as $key => $row) {
        if($key != 0) {
            if (strpos($row, '|') !== false) {
                $_arr2[$i][$key] = explode('|', $row);
            }
            else {
                $_arr2[$i][$key] = $row;
            }
        }
    }
}

echo '<pre>', print_r($_arr2, true), '</pre>';[/code]

Share this post


Link to post
Share on other sites
I'm getting this error--

Warning: Invalid argument supplied for foreach() in /home/hunter/public_html/cron/csv2arrayManual.php on line 13

Any idea what's causing it?

[code]<?
// csv to array parser
include_once '../classes/class.csv.php';
$document = 'rva-1.20060313.1107.dat';

$csv = & new csv("$document", ',', '"' , '\\');
$_arr = $csv->csv2Array();
$_arr2 = array();
//echo '<pre>' . var_export($_arr,true) . '</pre>';


for ($i = 0; $i <= count($_arr); $i++) {
    foreach($_arr[$i] as $key => $row) {
        if($key != 0) {
            if (strpos($row, '|') !== false) {
                $_arr2[$i][$key] = explode('|', $row);
            } else {
                $_arr2[$i][$key] = $row;
            }
        }
    }
}

echo '<pre>', print_r($_arr2, true), '</pre>';
//print_r($_arr[5][137][1]);  // this one works

?>[/code]

Share this post


Link to post
Share on other sites
you have used count($arr) in your for loop!!! (For future refence that is very slow as count() will be evaluated each time you loop - set a counter = count before the loop and use that instead.)

Now that does not stop the script working but maybe there


try this

[code]
<?php
$i = 0;
foreach ($arr as $arr1) {
foreach ($arr1 as $key => $cont) {

   if (strpos($cont, '|') !== false  && !is_array($cont)) {
    $_arr2[$i][$key] = explode('|', $cont);
   } else {
    $_arr2[$i][$key] = $cont;
   }
  
}
$i++;
}
?>
[/code]

I am not entirly sure why you check to see if the key is not 0 in your script.

Share this post


Link to post
Share on other sites
"foreach($_arr[$i] as $key => $row) {"

The error you're getting happens when $_arr[$i] is not an array. It can also happen if $_arr[$i] has not been set. Try putting "print_r($_arr[$i]);" just before the foreach.

Share this post


Link to post
Share on other sites

×

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.