Jump to content

multidimensional array problem


croakingtoad

Recommended Posts

I'm trying to explode() this multidimensional array, in fact it was working at some point in the past but I've recently made some changes and it's stopped working. So here's what I've got...

$_arr = primary array below

[code]
[3364] => Array
                (
                    [0] => 20051230194116769413000000
                    [1] => 20060413190808359724000000
                    [2] => 00
                    [3] => 701429
                    ....
                    [137] => Construction|Vinyl|Yes;Heating|Baseboard Electric|Yes;Heating|Forced Air Gas|Yes;Cooling|Window A/C|Yes;Water Description|Public Water|Yes;Sewer Description|Public Sewer|Yes;Lease Type|Month-to-Month|Yes;Owner Expenses|Water|Yes;Tenant Expenses|Electricity|Yes;Tenant Expenses|Gas|Yes;
                )
[/code]

Okay, so here's the code I have, basically I want to brake apart any part of the primary array that has a pipe '|' in it into a new sub-array of the primary array...

[code]
foreach($_arr 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]

Why isn't this working? All I get is what's in the first [ code ] block...
Link to comment
Share on other sites

Why explode using ";" when they are separated by "|" ?

Where does $i get its value;

You seem to be missing and inner foreach loop.

try

[code]$arr = array (
    3364 => array
                (
                    0 => '20051230194116769413000000' ,
                    1 => '20060413190808359724000000',
                    2 => 00,
                    3 => 701429 ,
                    137 => "Construction|Vinyl|Yes;Heating|Baseboard Electric|Yes;Heating|Forced Air Gas|Yes;Cooling|Window A/C|Yes;Water Description|Public Water|Yes;Sewer Description|Public Sewer|Yes;Lease Type|Month-to-Month|Yes;Owner Expenses|Water|Yes;Tenant Expenses|Electricity|Yes;Tenant Expenses|Gas|Yes"
                )
);
foreach($arr as $key => $row) {
    foreach ($row as $k => $v)  {
        if($key != 0) {
            if (strpos($v, '|') !== false) {
                $_arr2[$key][$k] = explode('|', $v);
            } else {
                $_arr2[$key][$k] = $v;
            }
        }
       }
}

echo '<pre>', print_r($_arr2, true), '</pre>';[/code]
Link to comment
Share on other sites

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Why explode using ";" when they are separated by "|" ?[/quote]
If you look closely, there are groups of strings separated by "|" that are separated by ";".

What were the changes you made? Go back to a version where is was working (you do have a copy don't you?) and start adding the changes one at a time to see when it stops working. Then you can figure out why it stopped working.

Ken
Link to comment
Share on other sites

So there are. Sorry, I missed those.

Changed
[code]explode('|', $v) [/code]to
[code]explode(';', $v)[/code]

-->
[code]Array
(
    [3364] => Array
        (
            [0] => 20051230194116769413000000
            [1] => 20060413190808359724000000
            [2] => 0
            [3] => 701429
            [137] => Array
                (
                    [0] => Construction|Vinyl|Yes
                    [1] => Heating|Baseboard Electric|Yes
                    [2] => Heating|Forced Air Gas|Yes
                    [3] => Cooling|Window A/C|Yes
                    [4] => Water Description|Public Water|Yes
                    [5] => Sewer Description|Public Sewer|Yes
                    [6] => Lease Type|Month-to-Month|Yes
                    [7] => Owner Expenses|Water|Yes
                    [8] => Tenant Expenses|Electricity|Yes
                    [9] => Tenant Expenses|Gas|Yes
                )

        )

)[/code]
Link to comment
Share on other sites

Brilliant, I'm working again. I think I had deleted my outer/inner loop at some point because I was getting duplicate stuff, but nevermind, seems good now.

So on to the next question...

The array now looks like this--
[code]
[137] => Array
                (
                    [0] => Lot Description|Other - See Remarks|Yes
                    [1] => Lot Description|Views|Yes
                    [2] => Construction|Brick|Yes
                    [3] => Construction|Other - See Remarks|Yes
                    [4] => Miscellaneous Info|Single Wide|No
                    [5] => Miscellaneous Info|Double Wide|No
                    [6] => Heating|Heat Pump Electric|Yes
                    [7] => Cooling|Heat Pump Electric|Yes
                    [8] => Interior Features|Storage|Yes
                    [9] => Interior Features|Walk-in-Closet|Yes
                    [10] => Exterior Features|Deck|Yes
                    [11] => Exterior Features|Storage Shed|Yes
                    [12] => Appliances|Dishwasher|Yes
                    [13] => Appliances|Clothes Dryer|Yes
                    [14] => Appliances|Microwave Oven|Yes
                    [15] => Appliances|Oven|Yes
                    [16] => Appliances|Refrigerator|Yes
                    [17] => Appliances|Range|Yes
                    [18] => Appliances|Clothes Washer|Yes
                    [19] => Misc Features|New Construction|Yes
                    [20] => Floors|Carpet|Yes
                    [21] => Floors|Concrete|Yes
                    [22] => Windows|Tilt-In|Yes
                    [23] => Exterior Doors|Wood|Yes
                    [24] => Porch|Rear Porch|Yes
                    [25] => Water Description|Public Water|Yes
                    [26] => Sewer Description|Public Sewer|Yes
                    [27] => Bedrooms|Bedrooms Entry Level|1
                    [28] => Bedrooms|Bedrooms Upper Level|0
                    [29] => Bedrooms|Bedrooms Lower Level|0
                    [30] => Bedrooms|Bedrooms Other Level|0
                    [31] => Full Baths|Full Baths Entry Lvl|1
                    [32] => Full Baths|Full Baths Upper Lvl|0
                    [33] => Full Baths|Full Baths Lower Lvl|0
                    [34] => Full Baths|Full Baths Other Lvl|0
                    [35] => Half Baths|Half Baths Entry Lvl|0
                    [36] => Half Baths|Half Baths Upper Lvl|0
                    [37] => Half Baths|Half Baths Lower Lvl|0
                    [38] => Half Baths|Half Baths Other Lvl|0
                    [39] =>
                )
[/code]

So the question is, as you can see from the array there are numerous entries for "Appliances". What if I wanted to parse through there and pull them all out into a csv list?

Result that I'm looking for example-
[code]
$appliances = "appliance1, appliance2, appliance3, appliance4";
[/code]

The thing to understand here, is it could always be different and what I want to grab is in between the pipes. The actual names, not "Appliances" and not "Yes".

thanks!
Link to comment
Share on other sites

Try this code:
[code]<?php
foreach($arr[137] as $tmp) {
    list ($type, $desc, $dmy) = explode('|', $tmp);
    $new_array[$type][] = $desc;
}
foreach ($new_array as $what => $dmy) {
     $$what = implode(',',$new_array[$what]);
     echo $what . ' = ' . $$what . "<br>\n";
}
?>[/code]

Note: this code has not been tested.

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