Jump to content


Photo

problem with array


  • Please log in to reply
15 replies to this topic

#1 croakingtoad

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

Posted 17 March 2006 - 03:02 PM

Okay, so I have this multidimensional array that looks something like--
[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 [11] => [12] => [13] => [14] => [15] => Active [16] => [17] => [18] => [19] => [20] => [21] => [22] => 69950.00 [23] => [24] => [25] => [26] => [27] => [28] => [29] => 5 - Northwest Roanoke City [30] => [31] => 1303 [32] => [33] => [34] => HANOVER [35] => [36] => NW [37] => AVE [38] => [39] => Roanoke [40] => VA [41] => Roanoke City [42] => [43] => 24017 [44] => [45] => [46] => [47] => [48] => 768.00 [49] => 768.00 [50] => 0.00 [51] => 0.00 [52] => 0.00 [53] => 1949 [54] => Cape Cod [55] => [56] => 50 X 117 [57] => 0.00 [58] => [59] => [60] => [61] => [62] => [63] => [64] => [65] => 0 [66] => 2 [67] => [68] => 1 [69] => [70] => [71] => [72] => [73] => N/A [74] => [75] => 394.00 [76] => [77] => EUREKA LAND [78] => COMPLETELY REMODELED-NEW VINYL SIDING,NEW WINDOWS,NEW CARPET & VINYL NEW KITCHEN,NEW HEAT PUMP,NEW PAINT,WALKUP ATTIC COULD BE FINISHED INTO MORE ROOMS [79] => [80] => 2220506 [81] => [82] => 10TH ST TO RIGHT ON HANOVER HOUSE ON RIGHT [83] => [84] => [85] => [86] => Off Street Parking [87] => 2006-03-11 13:08:33.650667 [88] => Exclusive Right to Represent Seller [89] => No [90] => Crawl Space [91] => FRPK [92] => William Ruffner [93] => Fleming [94] => [95] => [96] => [97] => No [98] => 20060311031233373503000000 [99] => [100] => [101] => [102] => [103] => [104] => Y [105] => 20060310190150596090000000 [106] => [107] => [108] => 1303 [109] => -28655552 [110] => -16110225 [111] => [112] => Completed [113] => [114] => [115] => PTS 1&2 [116] => 2 [117] => 0 [118] => N/A [119] => [120] => [121] => 0.00 [122] => 2.00 [123] => 0.00 [124] => [125] => [126] => [127] => [128] => [129] => 0.00 [130] => [131] => [132] => [133] => [134] => [135] => EUREKA LAND [136] => Bedroom 1:||||Entry|;Bedroom 2:||||Entry|;Kitchen:||||Entry|;Living Room:||||Entry|; [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; )

Now if you look at [137] you will see it also contains an array with '|' as the delimiter. I have two questions. How do I get the value of [1] and how do I get the values of the array items within [137]?

As for how to get the value of [1] I have something like this-- $array[$key][1] but all that is giving me when I print_r it is "array[1]".

And I haven't the slightest idea where to begin on my second question.

Thanks for the help!

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 17 March 2006 - 03:06 PM

well, as far as PHP is concerned, the value of [137] is still a string. you've got to change it to an array to be able to access it as an array. try something like this:
$array[137] = explode("|", $array[137]); // break it into an array
echo $array[137][1]; // now, you can access individual pieces.

hope this helps!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 March 2006 - 03:09 PM

No, index 137 of your array contains a string. To convert this into an array, do:
<?php 
$tmp_ary = explode('|',$array[137]);
echo '<pre>' . print_r($tmp_ary,true) . '</pre>';
?>

Ken

#4 croakingtoad

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

Posted 17 March 2006 - 03:22 PM

Okay, that makes sense but what about $array[1] ?

When I use this code
foreach($array as $key => $row) {
    print_r("$array[$key][0]<br />");
}

It's just printing--

Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
etc....



#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 March 2006 - 05:49 PM

You don't need to put "print_r" in a loop, just use:
<?php echo '<pre>' . print_r($array,true) . '</pre>'; ?>

Ken

#6 croakingtoad

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

Posted 17 March 2006 - 05:53 PM

[!--quoteo(post=355950:date=Mar 17 2006, 12:49 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 17 2006, 12:49 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You don't need to put "print_r" in a loop, just use:
<?php echo '<pre>' . print_r($array,true) . '</pre>'; ?>

Ken
[/quote]

Good point.

Okay, back to my original problem that Obsidian and kenrbnsn dealt with.

I now have this code-
foreach($_arr as $key => $row) {
    if($key != 0) {
        $_arr[$key][137] = explode("|", $_arr[$key][137]);
        echo "$_arr[$key][137][0]<br />";
    }
}

All this is giving me though is-
Array[137][0]
Array[137][0]
Array[137][0]

repeated over and over until the end of the loop. What am I doing wrong, why isn't it giving me the value of the array item?

#7 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 17 March 2006 - 05:55 PM

[!--quoteo(post=355945:date=Mar 17 2006, 03:22 PM:name=croakingtoad)--][div class=\'quotetop\']QUOTE(croakingtoad @ Mar 17 2006, 03:22 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Okay, that makes sense but what about $array[1] ?

When I use this code
foreach($array as $key => $row) {
    print_r("$array[$key][0]<br />");
}

It's just printing--

Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
Array[0]
etc....
[/quote]

Dude.. you don't have a multi dimensional array, at all.. You have a single dimension array.

Stop trying to [key][value].. its just Array[0] -> Array[137]

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#8 croakingtoad

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

Posted 17 March 2006 - 05:58 PM

[!--quoteo(post=355952:date=Mar 17 2006, 12:55 PM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 17 2006, 12:55 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Dude.. you don't have a multi dimensional array, at all.. You have a single dimension array.

Stop trying to [key][value].. its just Array[0] -> Array[137]
[/quote]

If you look at key [136] in my original post, you will see the multidimensional array csv'd with pipes |

This would be [$key][136][$whatever]
[136] => Bedroom 1:||||Entry|;Bedroom 2:||||Entry|;Kitchen:||||Entry|;Living Room:||||Entry|; [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; )


#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 March 2006 - 06:17 PM

As I said before, the entries with the "|" are just plain strings, you need to use explode() to turn them into an array.

This code
<?php
foreach($_arr as $key => $row) {
    if($key != 0) {
        $_arr[$key][137] = explode("|", $_arr[$key][137]);
        echo "$_arr[$key][137][0]<br />";
    }
}?>
is not going to do what you want, which I think is to find all the values in the array that contain a "|" and replace them with an array. Try this:
<?php
for ($i=0;$$ < count($_arr); $i++)
     if (strpos($_arr[$i] !== false) $_arr[$i] = explode('|',$_arr[$i]);
echo '<pre>' . print_r($_arr,true) . '</pre>';   // for debugging only
?>

Ken

#10 croakingtoad

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

Posted 17 March 2006 - 07:27 PM

Ken, I'm not sure I understand what you're doing but that's my fault!

I made a breakthrough (I think) a bit ago, let me show you where I am--

foreach($_arr as $key => $row) {
    if($key != 0) {
        $_arr[$key][137] = explode("|", $_arr[$key][137]);
        print_r("$_arr[$key][137][1]<br />");   // this one doesn't...
    }
}

print_r($_arr[5][137][1]);  // this one works

The print_r in the loop just prints "Array[137][1]" down the page, BUT, the print_r at the bottom actually prints the value of the array, which in this case is "Sloped".

What gives?

#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 March 2006 - 07:38 PM

Can you post the results of this code:
<?php echo '<pre>' . var_export($_arr,true) . '</pre>'; ?>
Put this line after the array is populated and before you do anything to it.

Ken

#12 croakingtoad

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

Posted 17 March 2006 - 07:42 PM

Okay, here it is, I've cut out some of the middle because it's a really long array
1 => 
  array (
    0 => '20051230194116769413000000',
    1 => '20060310195836868674000000',
    2 => '00',
    3 => '500169',
    4 => 'Y',
    5 => '20060310190340772208000000',
.....
    135 => 'EUREKA LAND',
    136 => 'Bedroom 1:||||Entry|;Bedroom 2:||||Entry|;Kitchen:||||Entry|;Living Room:||||Entry|;',
    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;',
  ),


#13 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 March 2006 - 07:59 PM

Thanks for posting the output.

What I missed (and I think everyone else missed) was the initial "1" pointing to an array, so all the code I posted couldn't work.

I took the output, cut & pasted it into a small script and came up with:
<?php
$_arr = array(1 =>
  array (
    0 => '20051230194116769413000000',
    1 => '20060310195836868674000000',
    2 => '00',
    3 => '500169',
    4 => 'Y',
    5 => '20060310190340772208000000',
    135 => 'EUREKA LAND',
    136 => 'Bedroom 1:||||Entry|;Bedroom 2:||||Entry|;Kitchen:||||Entry|;Living Room:||||Entry|;',
    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;',
  ),);
  
  echo '<pre>' . var_export($_arr,true) . '</pre>';  // before exploding the strings containing "|"
  foreach ($_arr[1] as $k => $v)
          if (strpos($v,'|') !== false) $_arr[1][$k] = explode('|',$_arr[1][$k]);
  echo '<pre>' . var_export($_arr,true) . '</pre>';  // after 
  ?>
The output is:
array (
  1 => 
  array (
    0 => '20051230194116769413000000',
    1 => '20060310195836868674000000',
    2 => '00',
    3 => '500169',
    4 => 'Y',
    5 => '20060310190340772208000000',
    135 => 'EUREKA LAND',
    136 => 'Bedroom 1:||||Entry|;Bedroom 2:||||Entry|;Kitchen:||||Entry|;Living Room:||||Entry|;',
    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;',
  ),
)

array (
  1 => 
  array (
    0 => '20051230194116769413000000',
    1 => '20060310195836868674000000',
    2 => '00',
    3 => '500169',
    4 => 'Y',
    5 => '20060310190340772208000000',
    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 => 
    array (
      0 => 'Lot Description',
      1 => 'Up Slope',
      2 => 'Yes;Construction',
      3 => 'Vinyl',
      4 => 'Yes;Miscellaneous Info',
      5 => 'Single Wide',
      6 => 'No;Miscellaneous Info',
      7 => 'Double Wide',
      8 => 'No;Heating',
      9 => 'Heat Pump Electric',
      10 => 'Yes;Cooling',
      11 => 'Heat Pump Electric',
      12 => 'Yes;Fireplace',
      13 => '# Fireplaces',
      14 => '0;Appliances',
      15 => 'Refrigerator',
      16 => 'Yes;Appliances',
      17 => 'Range',
      18 => 'Yes;Floors',
      19 => 'Carpet',
      20 => 'Yes;
Floors',
      21 => 'Vinyl',
      22 => 'Yes;Windows',
      23 => 'Tilt-In',
      24 => 'Yes;Exterior Doors',
      25 => 'Insulated',
      26 => 'Yes;Porch',
      27 => 'Front Porch',
      28 => 'Yes;Water Description',
      29 => 'Public Water',
      30 => 'Yes;Sewer Description',
      31 => 'Public Sewer',
      32 => 'Yes;Bedrooms',
      33 => 'Bedrooms Entry Level',
      34 => '2;Full Baths',
      35 => 'Full Baths Entry Lvl',
      36 => '1;',
    ),
  ),
)

Is this what you want?

Ken

#14 croakingtoad

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

Posted 17 March 2006 - 08:01 PM

Yup...exactly. And then how would I access each subitem in [136] and/or [137]..

#15 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 March 2006 - 08:14 PM

$_arr[1][136 or 137][$i] .... for $i from 0 to count($_arr[1][136 or 137]) or in code:
<?php
for ($j = 136; $j<138; $j++)
     for ($i = 0; $i < count($_arr[1][$i]; $i++) {
           echo "we're now looking at: " . $_arr[$j][$i] . "<br>\n";
?>

Ken

#16 croakingtoad

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

Posted 23 March 2006 - 05:08 PM

Okay, so I have been working on this but am still 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;',
  ),





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users