3

感谢您的帮助,我被困在这个问题上。让我解释一下,我有这种数组。

第一个数组
--------------
大批
(
    [2] => 价值7
    [3] => 值8
    [4] => 价值9
    [5] => 价值10
    [6] => 价值11
    [7] => 价值12
    [8] => 价值13
)
第二个数组
--------------
大批
(
    [0] => 数组
        (
            [0] => 5
            [1] => 3
            [2] => 6
        )

    [1] => 数组
        (
            [0] => 8
            [1] => 4
            [2] => 7
        )
)

和期望的结果:(如果第二个数组值==第一个数组键;更改第二个数组值)

大批
(
    [0] => 数组
        (
            [0] => 值10
            [1] => 值8
            [2] => 价值11
        )

    [1] => 数组
        (
            [0] => 值13
            [1] => 价值9
            [2] => 价值12
        )
)
4

2 回答 2

1

我认为您没有正确显示数组。这回答了你的问题了吗?

foreach($secondArray as $i => $v){
  if($firstArray[$v])$secondArray[$i] = $firstArray[$v];
}
于 2013-10-13T00:06:07.247 回答
1

如果我们假设您arrays是这样的:

$array1 = array(
     2 => 'value7',
     3 => 'value8',
     4 => 'value9',
     5 => 'value10',
     6 => 'value11',
     7 => 'value12',
); 
$array2 = array(
    array(
        10 => 5,
        1 => 7,
        2 => 4,
     ),
    array(
        0 => ''
    ),
    array(
        0 => 3
    )    
); 

然后这将完成这项工作:

$keys1 = array_keys($array1);
foreach($array2 as $innerKey => $innerArray) {
    foreach($innerArray as $k => $val) {
        if(in_array($val, $keys1)) {
            $array2[$innerKey][$k] = $array1[$val];
        }
    }
}
var_dump($array2);

输出

array (size=3)
  0 => 
    array (size=3)
      10 => string 'value10' (length=7)
      1 => string 'value12' (length=7)
      2 => string 'value9' (length=6)
  1 => 
    array (size=1)
      0 => string '' (length=0)
  2 => 
    array (size=1)
      0 => string 'value8' (length=6)
于 2013-10-13T00:43:17.987 回答