celtech Posted August 27, 2008 Share Posted August 27, 2008 Hello I'm fairly certain that this will be a simple problem to help with, I'm just so rusty with PHP and this is now starting to frustrate me... I have two arrays, one looks like: [0] => Array ( [itemID] => 110196213 [locationID] => 60008524 [typeID] => 18066 [quantity] => 37 [flag] => 4 [singleton] => 0 ) And the other like [0] => Array ( [18066] => 28 ) The end result I want to achieve is to loop through the first array, and use only 'typeID' & 'quantity'.I then wish to lookup the 'typeID' & price in the second array. The typeID is the key I believe (in my example 18066 is the typeID and 28 is the price)So, I look through the first array, take the typeID & quantity. I look these up in my prices array, and create a running total.Thanks for any help. Please ask questions if you like. Quote Link to comment https://forums.phpfreaks.com/topic/121534-array-trouble/ Share on other sites More sharing options...
JonnoTheDev Posted August 27, 2008 Share Posted August 27, 2008 // get 18066 $typeIdValue = $array1['typeID']; // get 28 from 18066 $valueFromTypeIdInArray2 = $array2[$typeIdValue]; Quote Link to comment https://forums.phpfreaks.com/topic/121534-array-trouble/#findComment-626765 Share on other sites More sharing options...
JasonLewis Posted August 27, 2008 Share Posted August 27, 2008 Wait, is the first array only going to look like that? Or will it get more items etc. If it is just like that they you could just do: $total = $array2[$array1['typeID']] * $array1['quantity']; If you need to loop then the first array would need to look like this: Array ( [0] => Array( [itemID] => 110196213 [locationID] => 60008524 [typeID] => 18066 [quantity] => 37 [flag] => 4 [singleton] => 0 ) [1] => Array( [itemID] => 110196213 [locationID] => 60008524 [typeID] => 18066 [quantity] => 37 [flag] => 4 [singleton] => 0 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/121534-array-trouble/#findComment-626766 Share on other sites More sharing options...
celtech Posted August 27, 2008 Author Share Posted August 27, 2008 Both arrays have hundreds / thousands of values. Thanks for the input so far, I will investigate on lunch break Quote Link to comment https://forums.phpfreaks.com/topic/121534-array-trouble/#findComment-626767 Share on other sites More sharing options...
celtech Posted August 27, 2008 Author Share Posted August 27, 2008 Not working so far. This is the logic I'm using to try and total up the prices. Any help appreciated! $countr = count($asset); $cost= 0; for($i=0;$i<=$countr;$i++) { $cost = $cost + ($pricelist[$asset]['typeID'] * $asset['quantity']); } echo ("cost: $cost<br><br>"); print("end"); Thanks Quote Link to comment https://forums.phpfreaks.com/topic/121534-array-trouble/#findComment-626778 Share on other sites More sharing options...
JasonLewis Posted August 27, 2008 Share Posted August 27, 2008 Try changing $asset to $i inside the $pricelist. $pricelist[$asset] becomes $pricelist[$i] Quote Link to comment https://forums.phpfreaks.com/topic/121534-array-trouble/#findComment-626799 Share on other sites More sharing options...
sasa Posted August 27, 2008 Share Posted August 27, 2008 try $countr = count($asset); $cost= 0; for($i=0;$i<=$countr;$i++) { $cost = $cost + ($pricelist[$asset[$i]['typeID']] * $asset[$i]['quantity']); } echo ("cost: $cost<br><br>"); print("end"); Quote Link to comment https://forums.phpfreaks.com/topic/121534-array-trouble/#findComment-626903 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.