0

Lets say i have an array like this here and want to get the parent key while iterating through it, how can i acomplish that?

Array
(
    [rigs] => Array
        (
            [45334a] => Array
                (
                    [condition] => mining
                    [version] => 1.2.7
                    [driver] => amdgpu
                    [miner] => claymore-xmr
                    [gpus] => 6
                    [miner_instance] => 6
                    [miner_hashes] => 807.00 807.10 806.76 807.05 806.57 806.81
                    [bioses] => 113-C9403100_101 113-C9403100_101 113-C9403100_101 113-C9403100_101 113-C9403100_101 113-C9403100_101
                    [meminfo] => GPU0:01.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
GPU1:02.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
GPU2:03.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
GPU3:04.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
GPU4:05.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
GPU5:06.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
                    [vramsize] => 8 8 8 8 8 8
                    [drive_name] => KINGSTON SA400S37120G 50026B76760083DD
                    [mobo] => H81 Pro BTC R2.0
                    [lan_chip] => Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 11)
                    [connected_displays] => 
                    [ram] => 7
                    [rack_loc] => devilrig
                    [ip] => 192.168.179.22
                    [server_time] => 1512868103
                    [uptime] => 522
                    [miner_secs] => 457
                    [rx_kbps] => 0.00
                    [tx_kbps] => 0.00
                    [load] => 0.28
                    [cpu_temp] => 33
                    [freespace] => 3.1
                    [hash] => 4841.29
                    [pool] => etn-eu1.nanopool.org:13333
                    [temp] => 70.00 72.00 71.00 70.00 67.00 51.00
                    [powertune] => 7 7 7 7 7 7
                    [voltage] => 1.150 1.150 1.150 1.150 1.150 1.150
                    [watts] => 
                    [fanrpm] => 1518 1659 1518 1518 1518 1518
                    [core] => 1380 1380 1380 1380 1380 1380
                    [mem] => 2150 2150 2150 2150 2150 2150
                )

            [469119] => Array
                (
                    [condition] => mining
                    [version] => 1.2.7
                    [driver] => amdgpu
                    [miner] => claymore
                    [gpus] => 3
                    [miner_instance] => 3
                    [miner_hashes] => 22.19 27.56 27.23
                    [bioses] => 113-C9403100_101 113-C9403100_101 113-C9403100_101
                    [meminfo] => GPU0:01.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
GPU1:03.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
GPU2:04.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
                    [vramsize] => 8 8 8
                    [drive_name] => Hitachi HTS545050B9A302 111002PBL400P7GKD6DV
                    [mobo] => H81 Pro BTC R2.0
                    [lan_chip] => Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 11)
                    [connected_displays] => 
                    [ram] => 4
                    [rack_loc] => dragonri
                    [ip] => 192.168.179.27
                    [server_time] => 1512867984
                    [uptime] => 76137
                    [miner_secs] => 76073
                    [rx_kbps] => 0.09
                    [tx_kbps] => 0.09
                    [load] => 0.17
                    [cpu_temp] => 19
                    [freespace] => 3.1
                    [hash] => 76.98
                    [pool] => pirl.minerpool.net:8002
                    [temp] => 69.00 56.00 69.00
                    [powertune] => 3 3 3
                    [voltage] => 0.862 0.887 0.868
                    [watts] => 
                    [fanrpm] => 1518 1518 1659
                    [core] => 1075 1075 1075
                    [mem] => 2000 2000 2000
                )

            [4699d6] => Array
                (
                    [condition] => mining
                    [version] => 1.2.7
                    [driver] => fglrx
                    [miner] => claymore-xmr
                    [gpus] => 5
                    [miner_instance] => 5
                    [miner_hashes] => 831.00 833.95 801.36 825.64 814.59
                    [bioses] => 113-2E3243U-X4H 115-C671GPI-200 113-2E3243U-X4J xxx-xxx-xxx 113-2E3240U-X4A
                    [meminfo] => GPU0:01.00.0:Radeon R9 390:113-2E3243U-X4H:SK Hynix H5GC4H24AJR:GDDR5:Hawaii
GPU1:02.00.0:Radeon R9 390:115-C671GPI-200:SK Hynix H5GC4H24AJR:GDDR5:Hawaii
GPU2:03.00.0:Radeon R9 390:113-2E3243U-X4J:Elpida EDW4032BABG:GDDR5:Hawaii
GPU3:04.00.0:Radeon R9 390:xxx-xxx-xxx:SK Hynix H5GC4H24AJR:GDDR5:Hawaii
GPU4:05.00.0:Radeon R9 390:113-2E3240U-X4A:SK Hynix H5GC4H24AJR:GDDR5:Hawaii
                    [vramsize] => 8 8 8 8 8
                    [drive_name] => WDC WD10EZEX-08WN4A0 WD-WCC6Y3FS9RVS
                    [mobo] => H81 Pro BTC R2.0
                    [lan_chip] => Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 11)
                    [connected_displays] => 640x480
                    [ram] => 4
                    [rack_loc] => r9-rig
                    [ip] => 192.168.179.23
                    [server_time] => 1512868147
                    [uptime] => 79995
                    [miner_secs] => 79943
                    [rx_kbps] => 0.06
                    [tx_kbps] => 0.04
                    [load] => 0.42
                    [cpu_temp] => 43
                    [freespace] => 2.9
                    [hash] => 4106.54
                    [pool] => etn-eu1.nanopool.org:13333
                    [temp] => 72.00 59.00 62.00 74.00 55.00
                    [powertune] => 50 50 50 50 50
                    [voltage] => 0 0 0 0 0
                    [watts] => 
                    [fanrpm] => 2184 0 1917 2118 1557
                    [core] => 1040 1050 1040 1024 1009
                    [mem] => 1500 1500 1500 1500 1500
                )

            [46a259] => Array
                (
                    [condition] => stuck_miners
                    [version] => 1.2.7
                    [driver] => amdgpu
                    [miner] => claymore
                    [gpus] => 6
                    [miner_instance] => 5
                    [miner_hashes] => 24.30 28.12 24.35 00.00 24.38 24.94
                    [bioses] => 115-D000PIL-100 115-D000PIL-100 113-2E3470U.S5X 113-D00034-S07 115-D000PIL-100 113-C9403100_101
                    [meminfo] => GPU0:01.00.0:Radeon RX 480:115-D000PIL-100:Samsung K4G80325FB:GDDR5:Polaris10
GPU1:02.00.0:Radeon RX 480:115-D000PIL-100:Samsung K4G80325FB:GDDR5:Polaris10
GPU2:03.00.0:Radeon RX 480:113-2E3470U.S5X:Samsung K4G80325FB:GDDR5:Polaris10
GPU3:04.00.0:Radeon RX 570:113-D00034-S07:SK Hynix H5GC4H24AJR:GDDR5:Polaris10
GPU4:05.00.0:Radeon RX 480:115-D000PIL-100:Samsung K4G80325FB:GDDR5:Polaris10
GPU5:06.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
                    [vramsize] => 8 8 8 4 8 8
                    [drive_name] => KINGSTON SA400S37120G 50026B767600864A
                    [mobo] => H81 Pro BTC R2.0
                    [lan_chip] => Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 11)
                    [connected_displays] => 
                    [ram] => 4
                    [rack_loc] => smallrig
                    [ip] => 192.168.179.26
                    [server_time] => 1512868110
                    [uptime] => 48011
                    [miner_secs] => 47949
                    [rx_kbps] => 0.09
                    [tx_kbps] => 0.09
                    [load] => 0.23
                    [cpu_temp] => 45
                    [freespace] => 3.2
                    [hash] => 126.09
                    [pool] => pirl.minerpool.net:8002
                    [temp] => 64.00 64.00 74.00 34.00 64.00 56.00
                    [powertune] => 5 5 5 5 5 5
                    [voltage] => 1.143 0.956 1.031 1.131 1.150 1.150
                    [watts] => 
                    [fanrpm] => 2294 1659 988 1659 1888 1518
                    [core] => 1232 1125 1191 1168 1232 1266
                    [mem] => 2000 2050 2000 300 2000 2000
                )

        )

    [total_hash] => 9150.9
    [alive_gpus] => 19
    [total_gpus] => 20
    [alive_rigs] => 4
    [total_rigs] => 4
    [current_version] => 1.2.7
    [avg_temp] => 63.8075
    [capacity] => 95.0
    [per_info] => Array
        (
            [claymore-xmr] => Array
                (
                    [hash] => 8948
                    [per_alive_gpus] => 11
                    [per_total_gpus] => 11
                    [per_alive_rigs] => 2
                    [per_total_rigs] => 2
                    [per_hash-gpu] => 813.5
                    [per_hash-rig] => 4474.0
                    [current_time] => 1512868156
                )

            [claymore] => Array
                (
                    [hash] => 203
                    [per_alive_gpus] => 8
                    [per_total_gpus] => 9
                    [per_alive_rigs] => 2
                    [per_total_rigs] => 2
                    [per_hash-gpu] => 25.4
                    [per_hash-rig] => 101.5
                    [current_time] => 1512868156
                )

        )

)

And i try to to get the key of the rigy while im iterating through the rigs (i want to get the 45334a, 469119 etc to save it to a variable in my iteration. where for example $loc_id should be the parent key like 45334a or 469119.

like

foreach($obj[rigs] as $rig) {

        $livegpus   = $rig['miner_instance'];
        $gpus       = $rig['gpus'];
        $loc        = $rig['rack_loc'];
        $loc_id     = key($obj[rigs]);


      // print_r($rig);
       print "LIVE GPUs von " .$loc. "(" .$loc_id ."): " . $livegpus . " of ". $gpus . "\n";

       //echo key($rig);

        if($livegpus < $gpus) {
           // echo  key($rig);
        }


        }

any idea how i can acomplish this ? any help would be quiet nice.

Tdotcom
  • 145
  • 12
  • 1
    In the [manual](http://php.net/manual/en/control-structures.foreach.php), there is an example of getting the `key` in a `foreach`. This is the syntax is pretty much this: `foreach (array_expression as $key => $value)`. – FirstOne Dec 10 '17 at 01:33
  • If you change your foreach to `foreach($obj[rigs] as $key => $rig) {`, you can access the values for rigs keys (`$key` will be `45334a`, `469119` and so on) – FirstOne Dec 10 '17 at 01:34
  • Possible duplicate of [PHP: Get the key from an array in a foreach loop](https://stackoverflow.com/questions/10992005/php-get-the-key-from-an-array-in-a-foreach-loop) – FirstOne Dec 10 '17 at 01:40

0 Answers0