使用php获取基于对象id的Array值

时间:2018-06-06 15:06:22

标签: php arrays wordpress

我有这个数组,但我不知道如何根据[object_ids]得到[discount_amount]。

例如,如果我的[object_ids]包含81,我想获得93值。

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] => 
    )
)

3 个答案:

答案 0 :(得分:1)

尝试使用此代码。

假设$ dataArray是您打印的数组。

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

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

echo $discount_amount

答案 1 :(得分:1)

您可以使用foreach并使用in_array检查数组object_ids是否包含81。

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

Demo

答案 2 :(得分:-1)

我主要这样做的方式如下:

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