如何将for循环转换为foreach循环?

时间:2018-05-09 10:29:07

标签: php arrays

你好朋友我有两个数组,我想匹配它们的id id并在新数组中隐藏它们。

这是我的$dizi1;

$dizi1 = [
[
    'id' => 1,
    'name' => 'özkan',
    'surname' => 'özdemir',
],
[
    'id' => 2,
    'name' => 'çağrı',
    'surname' => 'uğurel',
],
[
    'id' => 3,
    'name' => 'can',
    'surname' => 'tokay'
],
[
    'id' => 4,
    'name' => 'lütfü',
    'surname' => 'uzun'
],
[
    'id' => 5,
    'name' => 'ahmet',
    'surname' => 'yeni',
],
[
    'id' => 6,
    'name' => 'rabia',
    'surname' => 'papatya',
],
[
    'id' => 7,
    'name' => 'canan',
    'surname' => 'tokatlı'
],
[
    'id' => 8,
    'name' => 'muhammet',
    'surname' => 'uzundal'
]
];

这是mu $dizi2;

$dizi2 = [
[
    'id' => 2,
    'birthday' => 1993
],
[
    'id' => 3,
    'birthday' => 1990
],
[
    'id' => 4,
    'birthday' => 1989
],
[
    'id' => 1,
    'birthday' => 1987
],
[
    'id' => 5,
    'birthday' => 1993
],
[
    'id' => 7,
    'birthday' => 1990
],
[
    'id' => 6,
    'birthday' => 1989
],
[
    'id' => 8,
    'birthday' => 1987
]
];

然后我尝试定义一个新数组

$newarray = $dizi1;

然后我尝试了

foreach($dizi1 as $k)
{
foreach ($newarray as $i)
{
    if ($newarray[$i]['id'] == $dizi2[$i]['id']) 
    {

        $newarray[$i]['birthday'] = $dizi2[$i]['birthday'];
    }
  }
 }

现在它无法正常工作。

我必须使用foreach,我也会将它用于excel文件。

有什么问题?

3 个答案:

答案 0 :(得分:2)

在我使用array_search

的内部循环中使用array_columnarray_map
$result = array_map (
  function ($item) use($dizi2) { 
    $index = array_search($item['id'], array_column($dizi2, 'id'));
    if($index >= 0){
      $item['birthday'] = $dizi2[$index]['birthday'];
    }
    return $item ;
  },$dizi1
);
print_r($result);

Live Demo

您将获得输出

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => özkan
            [surname] => özdemir
            [birthday] => 1987
        )

    [1] => Array
        (
            [id] => 2
            [name] => çağrı
            [surname] => uğurel
            [birthday] => 1993
        )

    [2] => Array
        (
            [id] => 3
            [name] => can
            [surname] => tokay
            [birthday] => 1990
        )

    [3] => Array
        (
            [id] => 4
            [name] => lütfü
            [surname] => uzun
            [birthday] => 1989
        )

    [4] => Array
        (
            [id] => 5
            [name] => ahmet
            [surname] => yeni
            [birthday] => 1993
        )

    [5] => Array
        (
            [id] => 6
            [name] => rabia
            [surname] => papatya
            [birthday] => 1989
        )

    [6] => Array
        (
            [id] => 7
            [name] => canan
            [surname] => tokatlı
            [birthday] => 1990
        )

    [7] => Array
        (
            [id] => 8
            [name] => muhammet
            [surname] => uzundal
            [birthday] => 1987
        )

)

答案 1 :(得分:0)

使用此代码:

foreach($dizi1 as $k)
{
foreach ($dizi2 as $i)
{
    if ($i['id']==$k['id']) 
    {

        $newarray[$i['id']]['birthday'] = $i['birthday'];
    }
  }
 }
echo "<pre>";
print_r($newarray);

输出:

Array
(
    [1] => Array
        (
            [birthday] => 1987
        )

    [2] => Array
        (
            [birthday] => 1993
        )

    [3] => Array
        (
            [birthday] => 1990
        )

    [4] => Array
        (
            [birthday] => 1989
        )

    [5] => Array
        (
            [birthday] => 1993
        )

    [6] => Array
        (
            [birthday] => 1989
        )

    [7] => Array
        (
            [birthday] => 1990
        )

    [8] => Array
        (
            [birthday] => 1987
        )

)

答案 2 :(得分:0)

试试这个:

$newarray = [];
foreach($dizi1 as $d1){
   foreach ($dizi2 as $d2){
      if ($d1['id'] == $d2['id']){
         $newarray[] = array_merge($d1, $d2);
         break;
      }else{
         $newarray[] = $d1;
         $newarray[] = $d2;
      }
   }
}

您将获得输出:

[
    [
            [id] => 1,
            [name] => özkan,
            [surname] => özdemir,
            [birthday] => 1987
    ],
    [
            [id] => 2,
            [name] => çağrı,
            [surname] => uğurel,
            [birthday] => 1993
    ],
    [
            [id] => 3,
            [name] => can,
            [surname] => tokay,
            [birthday] => 1990
    ],
    [
            [id] => 4,
            [name] => lütfü,
            [surname] => uzun,
            [birthday] => 1989
    ],
    [
            [id] => 5,
            [name] => ahmet,
            [surname] => yeni,
            [birthday] => 1993
    ],
    [
            [id] => 6,
            [name] => rabia,
            [surname] => papatya,
            [birthday] => 1989
    ],
    [
            [id] => 7,
            [name] => canan,
            [surname] => tokatlı,
            [birthday] => 1990
    ],
    [
            [id] => 8,
            [birthday] => 1987
    ]
]