Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP If array_key_exists, variable equals array value: How?

I combined two arrays to create the following array, named $group_wages_array:

Array ( [1] => 500 [4] => 44 [6] => 80 [3] => 11.25 )

I am trying to test if the array key matches X, set a variable as it's value. Here's what I have:

NOTE: This whole thing is executed in a while loop, so the value of $thegroup['group_id'] will change. I've set it's value as "6" for this example.

$thegroup['group_id'] = "6" // This particular group (for simplicity)

if (array_key_exists($thegroup['group_id'], $group_wages_array)) {

    $this_wages = // Need this to be 80... how do I do it?

}

So, how do I get $this_wages to equal the key value?

like image 215
Oseer Avatar asked Jul 06 '26 04:07

Oseer


2 Answers

You just use the key from the array to get it:

$thegroup['group_id'] = "6" // This particular group (for simplicity)

if (array_key_exists($thegroup['group_id'], $group_wages_array)) {
    $this_wages = $group_wages_array[$thegroup['group_id']];
}

Also, the array keys are not 0,1,2,etc because you explicitly set them in Array ( [1] => 500 [4] => 44 [6] => 80 [3] => 11.25 )

like image 128
swatkins Avatar answered Jul 08 '26 19:07

swatkins


You are trying to do:

$group_wages_array[6];

And

$thegroup['group_id'] = 6;

You can substitute this in as the key.

if (array_key_exists($thegroup['group_id'], $group_wages_array)) {
    $this_wages = $group_wages_array[$thegroup['group_id']];   
}
like image 29
Gazler Avatar answered Jul 08 '26 20:07

Gazler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!