0

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] => 
    )
)
2
  • 1
    can you paste the array as JSON instead? That way we can play with code Commented Jun 6, 2018 at 15:09
  • If you are not expecting any Wordpress related answers, please remove that tag. Commented Jun 7, 2018 at 6:52

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

-1

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";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.