brm5017 Posted October 20, 2010 Share Posted October 20, 2010 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 Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 20, 2010 Share Posted October 20, 2010 $i = 0; $house = array(); $key = ''; foreach($features_raw as $v) : if ($i % 2) { $key = $v; } else { $house[$key] = $v; } $i++; endforeach; Quote Link to comment Share on other sites More sharing options...
brm5017 Posted October 20, 2010 Author Share Posted October 20, 2010 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; } Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 20, 2010 Share Posted October 20, 2010 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>'; Quote Link to comment Share on other sites More sharing options...
brm5017 Posted October 20, 2010 Author Share Posted October 20, 2010 Thanks for the help! i think this will work! Quote Link to comment Share on other sites More sharing options...
brm5017 Posted October 21, 2010 Author Share Posted October 21, 2010 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. Quote Link to comment Share on other sites More sharing options...
brm5017 Posted October 21, 2010 Author Share Posted October 21, 2010 Interesting. If I add echo $key . " has " . strlen($key). " characters <br>" it returns bedrooms has 17 characters fullbaths has 18 characters partialbaths has 21 characters ..etc Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.