Yung_Coco
Yung_Coco

Reputation: 23

Get Array value based on object ids with php

I have this Array but i don't know how to get the [discount_amount] based on the [object_ids].

For example i would like to get the 93 value if my [object_ids] contain 81.

Array
(
[0] => Array
    (
        [id] => rule_5b0d40cd1408a
        [membership_plan_id] => 106
        [active] => yes
        [rule_type] => purchasing_discount
        [content_type] => post_type
        [content_type_name] => product
        [object_ids] => Array
            (
                [0] => 81
            )

        [discount_type] => amount
        [discount_amount] => 93
        [access_type] => 
        [access_schedule] => immediate
        [access_schedule_exclude_trial] => 
    )

[1] => Array
    (
        [id] => rule_5b0d4e0f3b0b4
        [membership_plan_id] => 106
        [active] => yes
        [rule_type] => purchasing_discount
        [content_type] => post_type
        [content_type_name] => product
        [object_ids] => Array
            (
                [0] => 110
            )

        [discount_type] => amount
        [discount_amount] => 50
        [access_type] => 
        [access_schedule] => immediate
        [access_schedule_exclude_trial] => 
    )
)

Upvotes: 0

Views: 66

Answers (3)

Akshay Shah
Akshay Shah

Reputation: 3504

Try with this code .

Assuming $dataArray is the array you have printed.

foreach ($dataArray as $key => $value){

    if($value['object_ids'][0] == 83){
        $discount_amount = $value['discount_amount'];
    }
}

echo $discount_amount

Upvotes: 1

ageans
ageans

Reputation: 559

The way I mostly do this is as followed:

# Save retrieved data in array if you want to. 
$test = array();
foreach($array as $line){
# Count the row where discount_amount is found. 
$test[] = $line['9'];
echo "Result: ".$line['9']. "<br \>\n";
# OR another method is
$test[] = $line['discount_amount'];
echo "Result: ".$line['discount_amount']. "<br \>\n";
}

Upvotes: -1

The fourth bird
The fourth bird

Reputation: 163362

You could use a foreach and use in_array to check if the array object_ids contains 81.

foreach ($arrays as $array) {
    if (in_array(81, $array["object_ids"])) {
        echo $array["discount_amount"];
    }
}

Demo

Upvotes: 1

Related Questions