Jump to content

PHP Dynamic Associative Array


brm5017

Recommended Posts

I'm scraping a housing website with html source:

         <tr><td> </td><td> Bedrooms       </td><td> </td><td >3</td><td> </td></tr>
         <tr><td> </td><td> Full Baths     </td><td> </td><td >1</td><td> </td></tr>
         <tr><td> </td><td> Partial Baths  </td><td> </td><td >1</td><td> </td></tr>
         <tr><td> </td><td> Interior Sq Ft </td><td> </td><td >1,356</td><td> </td></tr>
         <tr><td> </td><td> Acres          </td><td> </td><td >0.16</td><td> </td></tr>

 

What i'm trying to do is create an associative array with only the details I  want from the above. I've already got a function that steps through the HTML and creates an array element for each row with the contents of each <td> statement as follows:

 

$repl = array(" ", "(", ")");
$repl_with = array("", "", "");
foreach ($rows_feat as $id2 => $row2){
	$features_raw = $scrape->fetchAllBetween('<td','</td>',$row2,true);
	//$features_data[str_replace($repl,$repl_with,strtolower(trim($features_raw[1])))] = trim($features_raw[3]);
	$countid=$countid+1;	

}

 

The array produced by this would look like:

 

features_raw[1] = Bedrooms

features_raw[3] = 1

 

 

 

Basically, I want to take data like this:

 

Beds      1

Baths      1

Garage    Yes

 

and create an array on the fly:

House[beds]=1

House[baths]=1

House[garage]="yes"

 

 

The problem is that the order of Beds, Baths and Garage changes. So on some house pages the order would be Baths, Garage, Beds.. etc.

 

Any pointers out there?

 

Thanks,

Brett

Link to comment
https://forums.phpfreaks.com/topic/216402-php-dynamic-associative-array/
Share on other sites

Crap I meant to include the following to my code before the foreach.

 

$data_features = trim($scrape->fetchBetween("<!-- Features Begin -->","<!-- Features End -->", $data_temp,false));
$rows_feat = $scrape->fetchAllBetween('<tr','</tr>',$data_features,true);

 

 

             

  
                $data_features = trim($scrape->fetchBetween("<!-- Features Begin -->","<!-- Features End -->", $data_temp,false));
$rows_feat = $scrape->fetchAllBetween('<tr','</tr>',$data_features,true);
$features_data = array();
$countid=0;
foreach ($rows_feat as $id2 => $row2){
	$features_raw = $scrape->fetchAllBetween('<td','</td>',$row2,true);
	$features_data[str_replace($repl_array,$repl_with,strtolower(trim($features_raw[1])))] = trim($features_raw[3]);
	echo $features_raw[1]. ": " . $features_data[$countid]."<br>";
	$countid=$countid+1;	

}

Instead of:

$features_raw = $scrape->fetchAllBetween('<td', '</td>', $row2, true);

Try:

while(list($key, $val) = array_splice($scrape->fetchAllBetween('<td', '</td>', $row2, true), 0, 2)) {
$features_new[$key] = $val;
}
echo 'Bedrooms: ' . $features_new['Bedrooms'] . '<br>';

Data source:        <td></td><td>Bedrooms</td><td></td><td>1</td><td></td>

 

 

	
            foreach ($rows_feat as $id2 => $row2){
	$features_raw = $scrape->fetchAllBetween('<td','</td>',$row2,true);
	$key=str_replace($repl_array,$repl_with,strtolower(trim($features_raw[1])));
	$features_data[$key] = trim($features_raw[3]);
	echo $key.": ". $features_data[$key]."<br>";              //This returns fine:    bedrooms: 1
	$countid=$countid+1;	

}

	$house[beds]=$features_data[bedrooms];

              echo '<br>beds: ' . $house[beds] ;              //This returns empty data                    beds:  "" 
              echo '<br>beds: ' . $features_data[bedrooms] ;    //This also returns empty data             beds:  "" 

 

     

 

I'm not sure why I can echo the correct values when within the foreach loop, but once I try to use the data located in "features_data[]" array outside of that loop, it returns null.

 

 

Archived

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

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