ChenXiu Posted November 18, 2022 Share Posted November 18, 2022 my preg_match_all works perfectly. I just can't figure out how to loop through the "$matches" array it creates. preg_match_all( $pattern , $string , $matches); The code print_r($matches) yields the following super simple array. ( [0] => Array ( [0] => 123456wordsandwordsandmorestuff$40.97 [1] => 987654wordsandmorewords$26.79 ) [1] => Array ( [0] => 123456 <----------this goes with the [0] below [1] => 987654 <----------this goes with the [1] below ) [2] => Array ( [0] => $40.97 <-----------this goes with the [0] above [1] => $26.79 <-----------this goes with the [1] above ) ) The problem is I can't figure out how to do the foreach() loop to get this: SKU number 123456 costs $40.97 SKU number 978654 costs $26.79 I know the answer will be embarrasingly simple..... but I've tried at least 200 different foreach variations and I am close to giving up. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/315549-loop-multidimensional-array/ Share on other sites More sharing options...
Solution mac_gyver Posted November 18, 2022 Solution Share Posted November 18, 2022 foreach(array_keys($matches[1]) as $key) { echo "SKU number {$matches[1][$key]} costs {$matches[2][$key]}<br>"; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/315549-loop-multidimensional-array/#findComment-1602700 Share on other sites More sharing options...
ChenXiu Posted November 18, 2022 Author Share Posted November 18, 2022 @mac_gyver THANK YOU! The $matches[1][$key] <--- This is the one thing that I didn't try. That "$key" variable in that spot. Thank you again! Quote Link to comment https://forums.phpfreaks.com/topic/315549-loop-multidimensional-array/#findComment-1602701 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.