加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP:将数据与来自单个阵列的共享密钥组合到新阵列中

发布时间:2020-12-13 22:24:28 所属栏目: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" ["produc
我有这个数组:

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的组合值.我搜索了SO的例子,但似乎我发现的每个例子都基于多个输入数组.我知道我可以通过嵌套的foreach函数来强制使用它,但是我会喜欢更优雅的解决方案.

解决方法

我最后只是做了蛮力方法,如果有人感兴趣,这是我的解决方案:

$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数组,因为密钥永远不会改变.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读