Jump to content


Photo

multidimensional array problem


  • Please log in to reply
4 replies to this topic

#1 croakingtoad

croakingtoad
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts
  • LocationUSA

Posted 24 March 2006 - 07:07 PM

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--
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>';
            }
        }
    }
}

Here's the output--
  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;',
  ),


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 24 March 2006 - 08:19 PM

It may be better to write the output to a second array instead of trying to write back to the original.

$_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>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 croakingtoad

croakingtoad
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts
  • LocationUSA

Posted 27 March 2006 - 08:05 PM

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?

<?
// 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

?>


#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 27 March 2006 - 10:37 PM

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

<?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++;
}
?>

I am not entirly sure why you check to see if the key is not 0 in your script.
follow me on twitter @PHPsycho

#5 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 27 March 2006 - 10:45 PM

"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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users