ChenXiu Posted September 4, 2021 Share Posted September 4, 2021 $matches is an array as follows: Array ( [0] => Array ( [0] => sku234567 the price equals $33.00 [1] => sku999888 the price equals $82.44 ) [1] => Array ( [0] => 234567 [1] => 999888 ) [2] => Array ( [0] => $33.00 [1] => $82.44 ) ) Using PHP, how do I end up with this: 234567 is $33.00 999888 is $82.44 I have tried just about everything, like echo "$matches[0] is $matches[2]" and $matches[1][1][2] and foreach loops..... I know I can access $matches[1] using a foreach loop, but then how do I access its corresponding value from $matches[2] ? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/313664-getting-2-array-values/ Share on other sites More sharing options...
Barand Posted September 4, 2021 Share Posted September 4, 2021 foreach ($array[1] as $k => $code) { $price = $array[2][$k]; echo "$code is $price<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/313664-getting-2-array-values/#findComment-1589651 Share on other sites More sharing options...
ChenXiu Posted September 4, 2021 Author Share Posted September 4, 2021 Thank you! That's perfect! I don't think I would have figured that one out... Whenever extracting values requires looping through an array using its own keys (e.g. "$array[$key_from_line_above]"), it always throws me for a loop (pardon the pun 😃 ). Quote Link to comment https://forums.phpfreaks.com/topic/313664-getting-2-array-values/#findComment-1589653 Share on other sites More sharing options...
ginerjm Posted September 5, 2021 Share Posted September 5, 2021 If you are creating these sets of data maybe you should re-think that process.... ? Quote Link to comment https://forums.phpfreaks.com/topic/313664-getting-2-array-values/#findComment-1589666 Share on other sites More sharing options...
cyberRobot Posted September 9, 2021 Share Posted September 9, 2021 In case you have control over how $matches is formatted, you could make this easier on yourself by changing the array to something like this: Array ( [0] => Array ( [sku] => 234567 [price] => $33.00 ) [1] => Array ( [sku] => 999888 [price] => $82.44 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/313664-getting-2-array-values/#findComment-1589769 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.