簡體   English   中英

在PHP中對對象數組進行分組和重新格式化

[英]Group and reformat an array of objects in PHP

我在PHP代碼中遇到以下性能問題。 我無法編輯的外部API,將返回一個類似於以下內容的JSON數組:

[{"name": "Name 1", "code": "Code 1", "attribute1": "Black", "attribute2": "32", "price": "10"},
 {"name": "Name 2", "code": "Code 2", "attribute1": "Yellow", "attribute2": "", "price": "15"},
{"name": "Name 1", "code": "Code 3", "attribute1": "Yellow", "attribute2": "32", "price": "20"},....
]

我想按name分組並將其重新格式化為這樣的JSON數組:

[{
   "name": "Name 1",
   "available_attributes": [ "size", "color" ],
   "variations": [ 
       { "attributes": { "size": "32", "color": "Black" }, "price": "10", "code": "Code 1"},
       { "attributes": { "size": "32", "color": "Yellow" }, "price": "20", "code": "Code 3"}
   ]
}, {
   "name": "Name 2",
   "available_attributes": [  "color" ],
   "variations": [ { "attributes": { "color": "Yellow" }, "price": "15", "code": "Code 2"}]
}]

我的解決方案很丑陋而且很耗時,因為我使用了一種簡單的蠻力來迭代響應,然后每次在數組上每次都更新我已經存在的響應。

因此,我正在尋找專注於性能和速度的解決方案。

編輯。 這是我的代碼。 唯一的區別是,在兩個屬性均為空的情況下,它只包含價格和sku,而不是variants和available_attributes數組。

function cmp( $a, $b ) {
    if ( $a['name'] == $b['name'] ) {
        return 0;
    }
    return ( $a['name'] < $b['name'] ) ? - 1 : 1;
}

function format_products_array($products) {
    usort( $products, "cmp" );
    $formatted_products = array();
    $new = true;
    $obj = array();

    for ( $i = 0; $i < count( $products ); $i++ ) {

        if ( $new ) {
            $obj = array();
            $attr = array();
            $obj['available_attributes'] = array();
            $obj['variations'] = array();

            $obj['name'] = $products[$i]['name'];
            if ( $products[$i]['attribute1'] != '' ) {
                array_push( $obj['available_attributes'], 'color' );
                $attr['color'] = $products[$i]['attribute1'];
            }
            if ( $products[$i]['attribute2'] != '' ) {
                array_push( $obj['available_attributes'], 'size' );
                $attr['size'] = $products[$i]['attribute2'];
            }   
        }

        if ( $products[ $i ]['name'] == $products[ $i + 1 ]['name']) {
            $new = false;
            $attr['size'] = $products[$i]['attribute2'];            
            $attr['color'] = $products[$i]['attribute1'];
            if ( empty($obj['available_attributes']) ) {
                $obj['price'] = $products[$i]['price'];
            } else {
                $var = array();
                $var['price'] = $products[$i]['price'];
                $var['code'] = $products[$i]['code'];
                $var['attributes'] = $attr;
                array_push($obj['variations'], $var);
            }
        } else {
            $new = true;
            if ( empty($obj['available_attributes']) ) {
                $obj['price'] = $products[$i]['price'];
            }
            $attr['size'] = $products[$i]['attribute2'];            
            $attr['color'] = $products[$i]['attribute1'];
            $var['attributes'] = $attr;
            array_push($obj['variations'], $var);
            array_push($formatted_products, $obj);              
        }
    }
    return $formatted_products;
}

一種更快的解決方案是在生成用於存儲唯一標識或每個對象的數組時,例如,生成:

[
  "Name1":{
   "name": "Name 1",
   "code": "Code 1",
   "available_attributes": [ "size", "color" ],
   "variations": [ 
       { "attributes": { "size": "32", "color": "Black" }, "price": "10"},
       { "attributes": { "size": "32", "color": "Yellow" }, "price": "20"}
   ]
  },
  "Name2": {
   "name": "Name 2",
   "code": "Code 2",
   "available_attributes": [  "color" ],
   "variations": [ { "attributes": { "color": "Yellow" }, "price": "15"}]
}]

要么

[
  "Code 1":{
   "name": "Name 1",
   "code": "Code 1",
   "available_attributes": [ "size", "color" ],
   "variations": [ 
       { "attributes": { "size": "32", "color": "Black" }, "price": "10"},
       { "attributes": { "size": "32", "color": "Yellow" }, "price": "20"}
   ]
  },
  "Code 2": {
   "name": "Name 2",
   "code": "Code 2",
   "available_attributes": [  "color" ],
   "variations": [ { "attributes": { "color": "Yellow" }, "price": "15"}]
}]

之后(可選)刪除任何關聯。

之后,您可以將它們存儲在memcached / redis中,然后當您需要重新檢索相同的數據時,只需先查看redis / memcached。

因此,一開始可能很耗時,但是之后將准備好這樣做,因此他們只能由“不幸”的男/女來做同樣的事情。

萬一這是非常耗時的循環,則使用工作程序生成這些數據,然后將其存儲在基於文檔的存儲中(例如mongodb / couchdb),然后站點將查看現成的文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM