Jump to content


Photo

multidimensional array problem


  • Please log in to reply
7 replies to this topic

#1 croakingtoad

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

Posted 25 May 2006 - 06:15 PM

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

[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;
                )

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

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

Why isn't this working? All I get is what's in the first [ code ] block...

#2 Barand

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

Posted 25 May 2006 - 10:27 PM

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

Where does $i get its value;

You seem to be missing and inner foreach loop.

try

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

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 kenrbnsn

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

Posted 26 May 2006 - 01:02 AM

[!--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

#4 Barand

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

Posted 26 May 2006 - 09:27 AM

So there are. Sorry, I missed those.

Changed
explode('|', $v)
to
explode(';', $v)

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

        )

)

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

#5 croakingtoad

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

Posted 26 May 2006 - 01:42 PM

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--
[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] => 
                )

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-
$appliances = "appliance1, appliance2, appliance3, appliance4";

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!

#6 kenrbnsn

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

Posted 26 May 2006 - 02:34 PM

Try this 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";
}
?>

Note: this code has not been tested.

Ken

#7 croakingtoad

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

Posted 26 May 2006 - 04:00 PM

Okay, should i replace part of my code with that or append it somewhere? Thanks!

#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 26 May 2006 - 05:21 PM

just throw that on somewhere on the end. see if it works.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users