简体   繁体   中英

How to Determine If array key equal to another array value in PHP

I have a array like this-array1:

Array
(
    [KAYSERİ EĞİTİM VE ARAŞTIRMA HASTANESİ] => Array
        (
            [27] => 1.31198374980837
            [28] => 1.31964897687573
            [29] => 1.30662343606958
            [30] => 1.36594872689222
            [31] => 1.32326024327785
            [32] => 1.34235680913653
            [33] => 1.3180530016225
        )

)

And this array keys exist in as another array values-array2:

Array
(
    [0] => Array
        (
            [HASTANEKODU] => 2784
            [HASTANEADI] => KAYSERİ EĞİTİM VE ARAŞTIRMA HASTANESİ
            [SEHIR] => Kayseri
            [DONEM] => HAZİRAN 2013
            [DONEMKODU] => 32

        )

    [1] => Array
        (
            [HASTANEKODU] => 2784
            [HASTANEADI] => KAYSERİ EĞİTİM VE ARAŞTIRMA HASTANESİ
            [SEHIR] => Kayseri
            [DONEM] => OCAK 2013
            [DONEMKODU] => 27

        )

    [2] => Array
        (
            [HASTANEKODU] => 2784
            [HASTANEADI] => KAYSERİ EĞİTİM VE ARAŞTIRMA HASTANESİ
            [SEHIR] => Kayseri
            [DONEM] => NİSAN 2013
            [DONEMKODU] => 30

        )

    [3] => Array
        (
            [HASTANEKODU] => 2784
            [HASTANEADI] => KAYSERİ EĞİTİM VE ARAŞTIRMA HASTANESİ
            [SEHIR] => Kayseri
            [DONEM] => TEMMUZ 2013
            [DONEMKODU] => 33

        )

    [4] => Array
        (
            [HASTANEKODU] => 2784
            [HASTANEADI] => KAYSERİ EĞİTİM VE ARAŞTIRMA HASTANESİ
            [SEHIR] => Kayseri
            [DONEM] => MAYIS 2013
            [DONEMKODU] => 31
            [YIL] => 2013
            [ROL] => AI
            [STATU] => DEVLET
            [TOPLAMBAGIL] => 9269.6
            [TOPLAMYATISGUNU] => 29602
            [YATISORTALAMAGUN] => 4.73783610755442
            [YATANHASTASAYISI] => 6248
            [VAKAKARMASI] => 1.32326024327785
        )

    [5] => Array
        (
            [HASTANEKODU] => 2784
            [HASTANEADI] => KAYSERİ EĞİTİM VE ARAŞTIRMA HASTANESİ
            [SEHIR] => Kayseri
            [DONEM] => ŞUBAT 2013
            [DONEMKODU] => 28

        )

    [6] => Array
        (
            [HASTANEKODU] => 2784
            [HASTANEADI] => KAYSERİ EĞİTİM VE ARAŞTIRMA HASTANESİ
            [SEHIR] => Kayseri
            [DONEM] => MART 2013
            [DONEMKODU] => 29

        )

) 

I want to get DONEM value from array-2 if array-1 key equal to array-2 DONEMKODU values.

I tried this code:

$donem=array();

foreach($array2 as $ar){

 foreach($array1 as $key => $value){
  if($key==$ar['DONEMKODU']){

   $donem[]=$ar['DONEM'];
}
}
}

But my code doesnt work.What should we do for gain this?

Thanks

You are iterating through $array1 keys, but it has a single key with value of KAYSERİ EĞİTİM VE ARAŞTIRMA HASTANESİ . So if you need a deeper iteration, change code to

$donem=array();

foreach ($array1 as $topValue){
    foreach ($topValue as $key => $value) {
        foreach ($array2 as $ar){
            if($key == $ar['DONEMKODU']){
                $donem[] = $ar['DONEM'];
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM