0

这是我的第一个函数的结果:

Array
(
[0] => Array
    (
        [MaidID] => 13
        [Stores] => Array
            (
                [0] => 14
                [1] => 5
            )
    )

  [1] => Array
     (
        [MaidID] => 3
        [Stores] => Array
            (
                [0] => 4
            )

    )

[2] => Array
    (
        [MaidID] => 41
        [Stores] => Array
            (
                [0] => 14
            )

    )
 )

然后,这是我的第二个函数的结果:

  Array
 (
 [1] => Array
    (
        [MaidID] => 14
        [Cash] => 10000
        [Debit] => 0
        [Credit] => 0
    )
  )

这应该是结果:

   Array ([0] => Array (

        [MaidID] => 14
        [Cash] => 10000.00
        [Debit] => 0
        [Credit] => 0   
        [MaidsID] => Array(
                       [0] => 13                          
                       [1] => 41
     )
   )
 )

有可能做到吗?我需要一个新的密钥名称 MaidsID 指向多个商店拥有的 MaidID 列表。请帮助我,请耐心等待我的问题,我只是一个初学者。非常感谢。

4

3 回答 3

0

此代码工作正常。$a 是你的第一个数组 $b 是第二个,$c 是结果

$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('女仆' => 4, '商店' => 数组 (1) ));

$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));

$MaidsID=array();

foreach ($a as $aa ){
    if (count($aa['Stores']>1)){
        array_push($MaidsID, $aa['Maid']);
    }
}
$MaidsID=array('MaidsID' => $MaidsID);

$c = array_merge($b, $MaidsID);`
于 2012-08-21T09:22:44.637 回答
0

我在这里测试过,没问题。(只需将 $a 替换为您的第一个数组,将 $b 替换为 seccond )。你确定你的数组结构和你上面写的完全一样吗?也许这有什么问题。您已将数组放入另一个数组中?(我认为不需要)

但是:对于此代码:

`$a = array (array('Maid' => 1, 'Stores' => array (1,5)), array('Maid' => 3, 'Stores' => array (4)), array( '女仆' => 4, '商店' => 数组 (1) )); $b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));

print_r($a);
echo "<br><br>================================================<br><br>";
print_r($b);
echo "<br><br>================================================<br><br>";

$MaidsID=array();

foreach ($a as $aa ){
    if (count($aa['Stores']>1)){
        array_push($MaidsID, $aa['Maid']);
    }
}
$MaidsID=array('MaidsID' => $MaidsID);

$c = array_merge($b, $MaidsID);

print_r($c);
echo "<br><br>================================================<br><br>";`

输出是:

Array ( [0] => Array ( [Maid] => 1 [Stores] => Array ( [0] => 1 [1] => 5 ) ) [1] => Array ( [Maid] => 3 [ Stores] => Array ( [0] => 4 ) ) [2] => Array ( [Maid] => 4 [Stores] => Array ( [0] => 1 ) ) )

=================================================

数组([0] => 数组([女佣] => 1 [现金] => 10000 [借方] => 0 [贷方] => 0))

=================================================

Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) [MaidsID] => Array ( [0] => 1 [1] => 3 [2] => 4))

=================================================

这不就是你想要的结果吗?

于 2012-08-21T10:18:36.470 回答
-1

看看这个。也许这可以帮助你。

$result = array_merge($array1, $array2);

于 2012-08-21T08:47:03.773 回答