2015-11-02 22 views
4

我有这样的阵列:PHP:与来自一个单一的阵列的共享密钥为新的阵列合成数据

array(5) { 
    [0]=> 
    array(4) { 
    ["productCode"]=> 
    string(4) "X001" 
    ["productUPC"]=> 
    string(3) "261" 
    ["productTextSeq"]=> 
    string(1) "1" 
    ["productTxtVal"]=> 
    string(5) "Text1" 
    } 
    [1]=> 
    array(4) { 
    ["productCode"]=> 
    string(4) "X001" 
    ["productUPC"]=> 
    string(3) "261" 
    ["productTextSeq"]=> 
    string(1) "2" 
    ["productTxtVal"]=> 
    string(5) "Text2" 
    } 
    [2]=> 
    array(4) { 
    ["productCode"]=> 
    string(4) "X001" 
    ["productUPC"]=> 
    string(3) "261" 
    ["productTextSeq"]=> 
    string(1) "3" 
    ["productTxtVal"]=> 
    string(5) "Text3" 
    } 
    [3]=> 
    array(4) { 
    ["productCode"]=> 
    string(4) "X002" 
    ["productUPC"]=> 
    string(3) "262" 
    ["productTextSeq"]=> 
    string(1) "1" 
    ["productTxtVal"]=> 
    string(5) "Text1" 
    } 
    [4]=> 
    array(4) { 
    ["productCode"]=> 
    string(4) "X002" 
    ["productUPC"]=> 
    string(3) "262" 
    ["productTextSeq"]=> 
    string(1) "2" 
    ["productTxtVal"]=> 
    string(5) "Text2" 
    } 
} 

利用上述输入,我想输出数组看起来像这样:

array(2) { 
    [0]=> 
    array(3) { 
    ["productCode"]=> 
    string(4) "X001" 
    ["productUPC"]=> 
    string(3) "261" 
    ["productTxtVal"]=> 
    string(17) "Text1 Text2 Text3" 
    } 
    [1]=> 
    array(3) { 
    ["productCode"]=> 
    string(4) "X002" 
    ["productUPC"]=> 
    string(3) "262" 
    ["productTxtVal"]=> 
    string(11) "Text1 Text2" 
    } 
} 

当productCode相同时,结果数组不需要productTextSeq键,只是productTextVal的组合值。我已经搜索过这个例子,但似乎我找到的每个例子都基于多个输入数组。我知道我可以用嵌套的foreach函数蛮力,但会喜欢更优雅的解决方案。

回答

0

最后我只是做的蛮力方法,这里是我的解决方案,如果任何人的兴趣:

$productData = array(); 
$sortedData = array(); 
$comments = ''; 
$saveKey = ''; 
$appendComment = false; 
$idx = 0; 

foreach ($data as $key=>$value) { 

    foreach ($value as $k=>$v) { 
     if ($k == 'productCode') { 
      if ($v == $saveKey) { 
       $appendComment = true; 
      } else { 
       $appendComment = false; 
       $saveKey = $v; 
       if ($idx !== 0) { // Don't write to array on first iteration! 
        $productData['productTxtVal'] = $comments; 
        $sortedData[] = $productData; 
       } 
      } 
     } 

     if ($k == 'productTxtVal') { 
      if ($appendComment == true) { 
       $comments .= ' ' . trim($v); 
      } else { 
       $comments = trim($v); 
      } 
     } 
    } 

    $productData = $value; 
    $idx++; 
} 

不“雅”,但它的工作原理。如果只有一个productCode在原始数组中,我也会在这个逻辑之后进行检查,因为它不会被写入到$ sortedData数组中,因为这个键永远不会改变。

0

以下代码假定您控制原始数据数组的内容(由于使用extract()函数注入的风险),并且没有两个具有相同productCode的项目具有相同的productTextSeq

$products = []; 
foreach ($data as $item) { 
    // extract contents of item array into variables 
    extract($item); 
    if (!isset($products[$productCode])) { 
     // create product array with code, upc, text as array 
     $products[$productCode] = compact('productCode', 'productUPC') + ['productTxtVal' => []]; 
    } 
    // add text value to array with sequence as index 
    $products[$productCode]['productTxtVal'][$productTextSeq] = $productTxtVal; 
} 

$products = array_values(// ignore array keys 
    array_map(function($product) { 
     ksort($product['productTxtVal']); // sort text as array by index/ sequence 
     $product['productTxtVal'] = implode(' ', $product['productTxtVal']); // implode into string 
     return $product; 
    }, $products) 
); 

你可以在这里运行代码:https://repl.it/BWQL

相关问题