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

}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

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.