0

I have the following array:

array(1) {
    [0]=> array(3) {
        ["s2member_level1"] => string(3) "400"
        ["s2member_level2"] => string(3) "400"
        ["administrator"] => string(3) "500"
    }
}

I want the values s2members_level1, s2members_level2, administratorand their values which are440, 400, 500 using a for loop so that I can update these values to their records.

0

2 Answers 2

1

Your data is 'hidden' in nested array. You can access it with $yourData[0]. To modify values, you can loop over the values and use reference:

foreach ($data[0] as $key => &$value) {
  $value = $value * 2;
}

In $key variable you will get s2member_level1, s2member_level2 and administrator.

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

Comments

0

well, you should use PHP's array function:

$myArray = array("anotherArray" => array("myDesiredValue"));

array_walk_recursive($myArray, function(&$value){
    if($value == "myDesiredValue")
    {
      $value = "ValueChanged";
    }
    });

// Now the $myArray is:
    $myArray = array("anotherArray" => array("ValueChanged"));

The above function loops through every individual member of all the arrays (no matter nested) and applys the changes you like. In our case, I said if the value is "myDesiredValue" then please do something. Note that I use & before $value in function to tell to PHP that directly change the real value of the array, so, after this function, you function is changed.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.