0

I have an array, and its large. very large. Its for a real estate company. I would like to split the array into sections based on the number of bedrooms each home has. So ideally all the entries (like the one below) that had 0 bedrooms would be set into one array, and with 1, into another. etc...

Is this possible?

object(SimpleXMLElement)#124 (1) {
  ["unit"]=>
  array(40) {
    [0]=>
    object(SimpleXMLElement)#246 (1) {
      ["@attributes"]=>          array(28) {
        ["mlsnumber"]=>            string(8) "A1837040"
        ["type"]=>            string(0) ""
        ["unit"]=>            string(3) "607"
        ["maintenancefee"]=>            string(4) "1609"
        ["price"]=>            string(6) "890000"
        ["salesprice"]=>            string(0) ""
        ["bath"]=>            string(1) "1"
        ["hbath"]=>            string(1) "0"
        ["beds"]=>            string(1) "0"
        ["sqft"]=>            string(3) "747"
        ["sqftm"]=>            string(5) "69.40"
        ["pricesqft"]=>            string(8) "1,191.43"
        ["lat"]=>            string(16) "25.7683201213024"
        ["lng"]=>            string(16) "-80.132474899292"
        ["pricemeters"]=>            string(9) "12,824.21"
        ["pricechange"]=>            string(5) "-6.32"
        ["dom"]=>            string(3) "181"
        ["reo"]=>            string(1) "N"
        ["shortsale"]=>            string(1) "N"
        ["dropprice"]=>            string(0) ""
        ["pets"]=>            string(3) "Yes"
        ["furnished"]=>            string(1) "U"
        ["FloorLevel"]=>            string(1) "6"
      }
    }
2
  • Is all the arrays inside unit? Commented Mar 4, 2014 at 4:43
  • Yes they are all inside unit Commented Mar 4, 2014 at 4:48

2 Answers 2

1

Loop through the array and push the current array into a new array whereas the index of the new array is the number of bedrooms of the current array. You'll need to convert your object into an array first, then something along the lines of this:

  foreach($all_units as $unit){
    if(!isset($new[$unit['beds']])){
          $new[$unit['beds']] = array();
    }
    array_push($new[$unit['beds']],$unit);
  }
  print_r($new);
Sign up to request clarification or add additional context in comments.

4 Comments

I have 6 sets of bed counts. 1,2,3,4,5,7. This code looks like it will only push out array for zero beds.
why? the !isset merely avoids the warning. If $unit[beds] contains the number of bedrooms You end up with a multi-dimensional array $new that is indexed by the number of bedrooms, ie $unit[2] contains arrays with 2 bedrooms.
I did try it, it returns Parse error: syntax error, unexpected ',', expecting ']'
I dont know, still not working. Id love for this code to work. says Warning: Illegal offset type
0

Suppose $arr holds your result, Try

$result = array();
foreach($arr['unit'] as $prop){
  $result[$prop["beds"]][] = $prop;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.