2017-07-26 11 views
1

私は、CRMデータベースから結果を取得するプロパティサイトで作業しています。引き出された結果は、より広い財産(すなわち、様々なアパートメントを収容する1つの大きなブロック)内の利用可能な単位に基づいている。私は、プロパティごとに画像の配列を押し入れることができる必要がありますが、ユニットごとではありませんが、結果配列は各プロパティと各ユニットを戻します。ここで私が何を意味するかだ:私は別のテーブルからフェッチされます共通のIDでオブジェクトのPHP配列をフィルタリングする

Array 
(
    [0] => stdClass Object 
     (
      [property_id] => 2 
      [unit_id] => 5 
      [property_name] => Morb House 
      [address] => 123 Test Street, Blahtown, BL1 AHH 
      [unit_name] => Apartment Number 1 
      [max_guests] => 2 
      [description] => This is a nice place 
      [bedrooms] => 1 
     ) 

    [1] => stdClass Object 
     (
      [property_id] => 2 
      [unit_id] => 6 
      [property_name] => Morb House 
      [address] => Some Road, Some Town, Anywhere 
      [unit_name] => Apartment Number 2 
      [max_guests] => 4 
      [description] => This is a slightly bigger place 
      [bedrooms] => 2 
     ) 

    [2] => stdClass Object 
     (
      [property_id] => 2 
      [unit_id] => 11 
      [property_name] => Morb House 
      [address] => 123 Test Street, Blahtown, BL1 AHH 
      [unit_name] => Cool Studio 
      [max_guests] => 1 
      [description] => This is a teeny weeny place 
      [bedrooms] => 1 
     ) 

    [3] => stdClass Object 
     (
      [property_id] => 4 
      [unit_id] => 12 
      [property_name] => Fake Buildings 
      [address] => 1 Fake Street, Anytown 
      [unit_name] => Fake Apartment One 
      [max_guests] => 1 
      [description] => Some description here 
      [bedrooms] => 1 
     ) 

) 

画像は、私がそのプロパティに関連オブジェクトに追加するプロパティIDによってフィルタリングされます。ただし、1つのプロパティ内で予約できる複数のユニットがあるため、最初の3つの結果でそのプロパティIDが複製されます。キーは、実際にプロパティIDであり、関連するユニットIDをキーとそのプロパティ内の各ユニットのサブオブジェクトが存在する

Array 
(
    [2] => stdClass Object 
     (
      [property_id] => 2 
      [property_name] => Morb House 
      [address] => 123 Test Street, Blahtown, BL1 AHH 
      [units] => Array 
      (
       [5] => stdClass Object 
        (
         [unit_name] => Apartment Number 1 
         [max_guests] => 2 
         [description] => This is a nice place 
         [bedrooms] => 1 
        ) 
       [6] => stdClass Object 
        (
         [unit_name] => Apartment Number 2 
         [max_guests] => 4 
         [description] => This is a slightly bigger place 
         [bedrooms] => 2 
        ) 
       [11] => stdClass Object 
        (
         [unit_name] => Cool Studio 
         [max_guests] => 1 
         [description] => This is a teeny weeny place 
         [bedrooms] => 1 
        ) 

      ) 
     ) 

    [4] => stdClass Object 
     (
      [property_id] => 4 
      [property_name] => Fake Buildings 
      [address] => 1 Fake Street, Anytown 
      [units] => Array 
      (
       [12] => stdClass Object 
        (
         [unit_name] => Fake Apartment One 
         [max_guests] => 1 
         [description] => Some description here 
         [bedrooms] => 1 
        ) 
      ) 

     ) 

) 

:理想的には、私は私の結果は次のようになりたいと思います。

これをPHPで単純かつきれいに行う方法については、ちょっとアドバイスが必要です。

答えて

2
$out=array(); 
foreach($array as $item) 
{ 
    $new = new StdClass; 
    foreach(array('unit_name','max_guests','description','bedrooms') as $add){ 
    $new->{$add} = $item->{$add}; 
    } 
    if(isset($item[$item->property_id])){ 
     $out[$item->property_id]->units[$item->unit_id] = $new; 
     continue; 
    } 
    $out[$item->property_id] = (object)array('property_id'=>$item->property_id,'property_name'=>$item->property_name,'address'=>$item->address,'units'=>array($item->unit_id=>$new)); 
} 
print_r($out); 
0

あなたが望むフォーマットを得るための唯一の方法は、プロパティとユニットテーブルの適切な関係設定でORMを使用している場合です。そうでない場合は、既存の結果を必要な形式に変換する必要があります。

<?php 

// your current result set goes here 
$units = $your_crm->database->get_units(); 

$properties = []; 

foreach ($units as $_unit) 
{ 
    $id = $_unit->property_id; 

    $unit = [ 
     'unit_name' => $_unit->unit_name, 
     'max_guests' => $_unit->max_guests, 
     'description' => $_unit->description, 
     'bedrooms' => $_unit->bedrooms, 
    ]; 

    if (isset($properties[$id])) { 
     $properties[$id]->units[] = $unit; 
    } else { 
     $properties[$id] = (object) [ 
      'property_id' => $_unit->property_id, 
      'property_name' => $_unit->property_name, 
      'address'  => $_unit->address, 
      'units'   => [$unit] 
     ]; 
    } 
} 

print_r($properties); 
関連する問題