将数组合并到另一个数组中

时间:2014-06-25 12:06:46

标签: php arrays

我一直在努力管理数组,但不知怎的,我似乎无法让它发挥作用。

这是我的问题: 我有一个产品阵列,如下所示:

    array (size=2)
  'count' => string '1' (length=1)
  'records' => 
    array (size=5)
      'care_Id' => string '11938' (length=5)
      'productName' => string 'The name of the product blabla' (length=35)
      'productPrice' => string '98.00' (length=5)
      'beginAt' => string '2010-09-17 09:00:00' (length=19)
      'endAt' => string '2010-09-27 23:59:00' (length=19)

和另一个关于产品的关联信息数组,如下所示:

array (size=5)
  'sellQuantity' => string '22' (length=2)
  'validatedQuantity' => string '22' (length=2)
  'lostQuantity' => string '0' (length=1)
  'remainQuantity' => string '0' (length=1)
  'turnover' => string '2177.00' (length=7)

我想要做的就是输出:

    array (size=2)
  'count' => string '1' (length=1)
  'records' => 
    array (size=5)
      'care_Id' => string '11938' (length=5)
      'productName' => string 'Journée Semaine Luxe et Bien-être' (length=35)
      'productPrice' => string '98.00' (length=5)
      'beginAt' => string '2010-09-17 09:00:00' (length=19)
      'endAt' => string '2010-09-27 23:59:00' (length=19)
      'sellQuantity' => string '22' (length=2)
      'validatedQuantity' => string '22' (length=2)
      'lostQuantity' => string '0' (length=1)
      'remainQuantity' => string '0' (length=1)
      'turnover' => string '2177.00' (length=7)

但我无法做到。我试过了:

$aTest = array_merge($aCares[$i]['records'], $aCaresInfo[$i]);

以下是使用的循环:

for($i=0;$i<count($aInSaleCares);$i++)
{
    $aFilter['careId'] = $aInSaleCares[$i]['careId'];
    $aCares[] = sthORM::get('Sales')->getInSaleCares($iOffset, $iLimit, array(), $aFilter);
    $aCaresInfo[] = sthORM::get('Sales')->getCaresInfo($aFilter);
    $aTest = array_merge($aCares[$i]['records'], $aCaresInfo[$i]);
}

但是当我var_dump $ aTest时,它会触发一个非常奇怪的错误:

Warning: array_merge(): Argument #1 is not an array in

但是var_dump(is_array($aCares[$i]['records']))会返回true吗?

所以我的问题是:我怎样才能得到我想要的输出?


编辑:


我也试过这个:

 $aTest[] = $aCares[$i]['records'] + $aCaresInfo[$i]; // inside of the loop

但是当我在循环之外打印它时,它会返回一个php致命错误:

Unsupported operand types

在循环中我正确得到这样的数据:

array (size=10)
  'care_Id' => string '11938' (length=5)
  'productName' => string 'Journée Semaine Luxe et Bien-être' (length=35)
  'productPrice' => string '98.00' (length=5)
  'beginAt' => string '2010-09-17 09:00:00' (length=19)
  'endAt' => string '2010-09-27 23:59:00' (length=19)
  'sellQuantity' => string '22' (length=2)
  'validatedQuantity' => string '22' (length=2)
  'lostQuantity' => string '0' (length=1)
  'remainQuantity' => string '0' (length=1)
  'turnover' => string '2177.00' (length=7)

1 个答案:

答案 0 :(得分:0)

循环外是否存在$ aTest?你是否将$ aTest的内容放入循环之外的其他内容中,而不是在你的示例中包含它?

$stuff = array();
for($i=0;$i<count($aInSaleCares);$i++)
{
    $aFilter['careId'] = $aInSaleCares[$i]['careId'];
    $aCares[] = sthORM::get('Sales')->getInSaleCares($iOffset, $iLimit, array(), $aFilter);
    $aCaresInfo[] = sthORM::get('Sales')->getCaresInfo($aFilter);
    $aTest = array_merge($aCares[$i]['records'], $aCaresInfo[$i]);
    $stuff[] = $aTest;
}

var_dump($stuff);

当其他所有方法都失败时,您也可以尝试:

for($i=0;$i<count($aInSaleCares);$i++)
{
    $tmp = array();
    $aFilter['careId'] = $aInSaleCares[$i]['careId'];
    $aCares[] = sthORM::get('Sales')->getInSaleCares($iOffset, $iLimit, array(), $aFilter);
    $aCaresInfo[] = sthORM::get('Sales')->getCaresInfo($aFilter);

    $tmp = $aCares[$i]['records'];
    $tmp = array_merge($tmp, $aCaresInfo[$i]);

    // overwrite the existing contents of 'records'
    $aCares[$i]['records'] = $tmp;

}