502_Geek
502_Geek

Reputation: 2126

How do I regernate new format if key value is duplicate in current array?

I have a array that uses retrieving with cakephp model. This is the sample format that I retrieve.

{ 
    [0]=> { 
        ["User"]=>  { 
            ["id"]=>  "57ba6fcb-c214-410d-98dc-5b3c80c7bd6a"
            ["name"]=> "Alex" 
            ["username"]=>  "J111111M" } 
        ["FmRptGaTransaction"] =>  { 
            ["id"]=> "1542667" 
            ["organisation_id"]=> "20544" 
            ["txn_date"]=> "2017-12-01" 
            ["txn_type"]=> "school_fee" 
            ["txn_desc"]=> "Program Fee (December 2017)" 
        } 
     } 
    [1]=> { 
        ["User"]=>  { 
            ["id"]=>  "57ba6fcb-c214-410d-98dc-5b3c80c7bd6a"
            ["name"]=> "Alex" 
            ["username"]=>  "J111111M" } 
        ["FmRptGaTransaction"] =>  { 
            ["id"]=> "1542731" 
            ["organisation_id"]=> "20544" 
            ["txn_date"]=> "2017-11-01" 
            ["txn_type"]=> "school_fee" 
            ["txn_desc"]=> "Program Fee (November 2017)" 
        }
     } 
    [2]=> { 
        ["User"]=>  { 
           ["id"]=>  "57ba6fcb-c214-410d-98dc-5b3c80c7bd6a"
           ["name"]=> "Alex" 
           ["username"]=>  "J111111M" } 
        ["FmRptGaTransaction"] =>  { 
           ["id"]=> "1542734" 
           ["organisation_id"]=> "20544" 
           ["txn_date"]=> "2017-10-01" 
           ["txn_type"]=> "school_fee" 
           ["txn_desc"]=> "Program Fee (September 2017)" 
       }
    }
}

What I want is something like that;

{ 
    [0]=> { 
        ["User"]=>  { 
           ["id"]=>  "57ba6fcb-c214-410d-98dc-5b3c80c7bd6a"
           ["name"]=> "Alex" 
           ["username"]=>  "J111111M" 
        } 
        ["FmRptGaTransaction"] =>  {
           [0]=>{ 
              ["id"]=> "1542667" 
              ["organisation_id"]=> "20544" 
              ["txn_date"]=> "2017-12-01" 
              ["txn_type"]=> "school_fee" 
              ["txn_desc"]=> "Program Fee (December 2017)" 
           }[1]=>{
              ["id"]=> "1542731" 
              ["organisation_id"]=> "20544" 
              ["txn_date"]=> "2017-11-01" 
              ["txn_type"]=> "school_fee" 
              ["txn_desc"]=> "Program Fee (November 2017)" 
           }[2]=>{
              ["id"]=> "1542734" 
              ["organisation_id"]=> "20544" 
              ["txn_date"]=> "2017-10-01" 
              ["txn_type"]=> "school_fee" 
              ["txn_desc"]=> "Program Fee (September 2017)" 
          }
       } 
   }
}

The point is if user is already exist, I want to show once. Then, the rest of the transaction array want to create as another array.

What I did is..

$gaData = [];
$extUserId = [];
foreach($rptGaData as $data) {
     if(!in_array($data['User']['id'],$extUserId)){  //Want to create one time if User array is already exist
        $temp = [];
        $temp['User']['id'] = $data['User']['id'];
        $temp['User']['name'] = $data['User']['name'];
        $temp['User']['username'] = $data['User']['username'];
     }

     $transaction = [];
     $transaction['id'] = $data['FmRptGaTransaction']['id'];
     $transaction['organisation_id'] = $data['FmRptGaTransaction']['organisation_id'];
     $transaction['txn_date'] = $data['FmRptGaTransaction']['txn_date'];
     $transaction['txn_type'] = $data['FmRptGaTransaction']['txn_type'];
     $transaction['txn_desc'] = $data['FmRptGaTransaction']['txn_desc'];
     $temp['User']['FmRptGaTransaction'][] = $transaction;
     $gaData[] = $temp;
     $extUserId[] = $data['User']['id'];
}

But, sounds like something wrong with my final result.

Upvotes: 0

Views: 50

Answers (1)

imox
imox

Reputation: 1554

Try this (using user id as array key):

$gaData = [];
foreach($rptGaData as $data) {
    $temp = [];
     if(!isset($gaData[$data['User']['id']])){  //Want to create one time if User array is already exist
        $temp['User']['id'] = $data['User']['id'];
        $temp['User']['name'] = $data['User']['name'];
        $temp['User']['username'] = $data['User']['username'];
     }

     $transaction = [];
     $transaction['id'] = $data['FmRptGaTransaction']['id'];
     $transaction['organisation_id'] = $data['FmRptGaTransaction']['organisation_id'];
     $transaction['txn_date'] = $data['FmRptGaTransaction']['txn_date'];
     $transaction['txn_type'] = $data['FmRptGaTransaction']['txn_type'];
     $transaction['txn_desc'] = $data['FmRptGaTransaction']['txn_desc'];
     $temp['FmRptGaTransaction'][] = $transaction;
     if(isset($gaData[$data['User']['id']]))
        $gaData[$data['User']['id']]['FmRptGaTransaction'][] = $transaction;
     else
        $gaData[$data['User']['id']]=$temp;


}

echo "<pre>";
print_r($gaData);

Upvotes: 6

Related Questions