Jump to content

Archived

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

croakingtoad

problem with array

Recommended Posts

Okay, so I have this multidimensional array that looks something like--
[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 [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; )
[/code]

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!

Share this post


Link to post
Share on other sites
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:
[code]
$array[137] = explode("|", $array[137]); // break it into an array
echo $array[137][1]; // now, you can access individual pieces.
[/code]

hope this helps!

Share this post


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

Ken

Share this post


Link to post
Share on other sites
Okay, that makes sense but what about $array[1] ?

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

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

Share this post


Link to post
Share on other sites
You don't need to put "print_r" in a loop, just use:
[code]<?php echo '<pre>' . print_r($array,true) . '</pre>'; ?>[/code]

Ken

Share this post


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

Ken
[/quote]

Good point.

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

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

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?

Share this post


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

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

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]

Share this post


Link to post
Share on other sites
[!--quoteo(post=355952:date=Mar 17 2006, 12:55 PM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 17 2006, 12:55 PM) [snapback]355952[/snapback][/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]
[code]
[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; )
[/code]

Share this post


Link to post
Share on other sites
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
[code]<?php
foreach($_arr as $key => $row) {
    if($key != 0) {
        $_arr[$key][137] = explode("|", $_arr[$key][137]);
        echo "$_arr[$key][137][0]<br />";
    }
}?>[/code]
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:
[code]<?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
?>[/code]

Ken

Share this post


Link to post
Share on other sites
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--

[code]
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
[/code]

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?

Share this post


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

Ken

Share this post


Link to post
Share on other sites
Okay, here it is, I've cut out some of the middle because it's a really long array
[code]
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;',
  ),
[/code]

Share this post


Link to post
Share on other sites
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:
[code]<?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
  ?>[/code]
The output is:
[code]
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;',
    ),
  ),
)[/code]

Is this what you want?

Ken

Share this post


Link to post
Share on other sites
$_arr[1][136 or 137][$i] .... for $i from 0 to count($_arr[1][136 or 137]) or in code:
[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";
?>[/code]

Ken

Share this post


Link to post
Share on other sites
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--
[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

×

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.