-1

I am a bigginner in php and stuck in the following:
I have an array with the following format and would like to implement the following logic in php.

If the attribute rz_price is present in this array I would multiply the value in [0]=500 and [1]=700 by a fixed amount else keep it as it is.

Array
(
    [0] => Array
        (
            [key] => rz_listing_type
            [value] => 380
        )

    [1] => Array
        (
            [key] => rz_listing_region
            [value] => Array
                (
                    [0] => 132
                    [1] => 134
                )

            [compare] => IN
        )

    [2] => Array
        (
            [key] => rz_price
            [value] => Array
                (
                    [0] => 500
                    [1] => 700
                )

            [type] => NUMERIC
            [compare] => BETWEEN
        )

)

Thanks for your help.

KLA
  • 31
  • 1
  • 8
  • Unclear. We do not have a [mcve]. You never say what your exact desired output is. We don't know how your input data may be slightly different because we only have one example of a row with `rz_price`. Please always present your array data as the copy-pasted text from `var_export()` so that volunteers can instantly use your data in their own script without needing to reformat it into a variable. – mickmackusa Feb 07 '23 at 21:33
  • It is okay to be a beginner, but we expect you to make an effort to self-solve and show proof of research. Stack Overflow is a community for programmers and programming enthusiasts. Please show your enthusiasm to become a programmer if you are not already a programmer. – mickmackusa Feb 07 '23 at 22:04

1 Answers1

0

First you need to reformat that array or you will get errors.

$arr = Array
(
    'rz_listing_type' => 380,
    'rz_listing_region' => Array (132, 134),
    'compare' => 'IN',
    'rz_price' => Array (500, 700),
    'type' => 'NUMERIC',
    'compare' => 'BETWEEN'
);

Now Set some variables.

// The fixed amount you will multiply by.
$fixed_amout = 100;

// the result from multiplying rz_price[0] by $fixed_amount
$new_price_1 = null;

// the result from multiplying rz_price[1] by $fixed_amount
$new_price_2 = null;

Now, check to see if the key 'rz_price' exists in your array.

if (array_key_exists('rz_price', $arr)) {

If it exists, put the value of 'rz_price' into a handy variable

$item = $arr['rz_price'];

And multiply each item in rz_price's array by $fixed_amout

$new_price_1 = $item[0] * $fixed_amout;
$new_price_2 = $item[1] * $fixed_amout;

}
HomeSlice
  • 596
  • 2
  • 14