0

I have this array called $addon_array, that when i use var_dump shows up like this

array(10) { 
    ["ext_token"]=> string(5) "floor"  
    ["ext_token_child"]=> string(6) "carpet"  
    ["concrete"]=> string(1) "4"  
    ["cement"]=> string(1) "3"  
    ["sand"]=> string(1) "2"  
    ["wood_4_2"]=> string(1) "4"  
    ["wood_8_2"]=> string(1) "2"  
    [0]=> string(0) ""  
    ["brick"]=> string(3) "100"  
    ["carpet"]=> string(1) "3"
}

What I have bee trying to do most of the night is try and figure a way of cycling through all the different elements of the array (except ext_token and ext_token_child) and multiply each number via another variable called $qty (which can be anything, it doesn't matter).

Then I want them put in a new array again along with their corresponding columnNames (cement or sand etc...) I think it is jsut the simple case of just performing a foreach loop on each element, but I can't seem to be able to put them back into a new array.

Thanks for any and all help

1
  • 1
    What does this have to do with mysql or ajax? Commented Aug 26, 2012 at 9:54

4 Answers 4

2
$new_array = array();
foreach($addon_array as $key => $col) {
  if($key == 'ext_token' || $key == 'ext_token_child') {
    $new_array[$key] = $col;
  } else {
    $new_array[$key] = $col * $qty;
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

ok brilliant, that looks good, what i will do though, is only add the elements that have the calculation performed on them added to the array, so for example ext_token and ext_toke_child, i would not add to the array, so would i just remove the $new_array[$key] = $col? Also, i just noticed i have a blank element in my array with no variables, how would i not perform a calculation on this? Thanks
@Arken Yah, remove that line, then invert the meaning of the if statement so it only runs on valid values. For the blank one either check for blank: $col === '' (Note the three equals - this is VERY important). Or use is_numeric($col) and automatically check if it's a number - then you don't even need to check for ext_token since it's not a number.
Ok briliant, i just checked now and it works great, thanks for your help
2

This should work (using array_walk):

$new_array = array_walk($addon_array, function($v, $k) use ($qty) {
  if(in_array($k, array('ext_token', 'ext_token_child'))) return $v;
  return $v*$qty;
});

1 Comment

@Arken This version is a bit nicer since the array loop runs internally. But it's harder to understand for a newbie than mine.
1
$qty = 10;
$newArray = array();
foreach($addon_array as $key => $val){
    if($key != 'ext_token' && $key != 'ext_token_child'){
        $newArray[$key]    = $qty*(int)$val;
    }
}

$newArray['ext_token'] = $addon_array['ext_token'];
$newArray['ext_token_child'] = $addon_array['ext_token_child'];

var_dump($newArray);

Comments

0

Cycling through array in PHP is quite simple, use foreach.

$new = array();
foreach($addon_array as $key=>$value){

     if($key != "ext_token" OR $key != "ext_token_child"){
          $value = $value*$qty;
     }
     $new[$key] = $value;
}

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.