chrisale Posted August 12, 2008 Share Posted August 12, 2008 Hey yall, I've been banging my head on this one on and off for months and gone around in circles on the Internet looking for an answer. Please Help! I have a multidimensional Array that I'm using a foreach loop on to check all of its values according to its key. Example of what is working now... it picks out the proper keys and dumps the rest so that it can be sent to a specific function for processing. foreach ($weatherArray as $key => $val) { if (stristr($key, 'temp') || stristr($key, 'chill') || stristr($key, 'dew') || stristr($key, 'heat')){ // Searching for temperature, windchill, dewpoint, and heat values if (stristr($key, 'Time') === FALSE){// The Following Conditions get rid of the array keys that have to do with times, or are intervals, instead of bare temperatures if (stristr($key, 'date') === FALSE){ if (stristr($key, 'change') === FALSE){ if (stristr($key, 'avg') === FALSE){ if (stristr($key, 'unit') === FALSE){ $weatherArray = changeTempColor($weatherArray,$key,$val); } } } } } } //end of Temperature CSS Styling It is for weather data from my weather site, obviously. The above works fine... however... where it breaks down is when I try to enter into the nested array stored at $weatherArray [sqlData] if (stristr($key, 'SQLData')) { // Searching for SQLData { foreach ($val as $key2 => $val2) { if (stristr($key2, 'temp') || stristr($key2, 'chill') || stristr($key2, 'dew') || stristr($key2, 'heat')){ // Searching for temperature, windchill, dewpoint, and heat values if (stristr($key2, 'Time') === FALSE){// The Following Conditions get rid of the array keys that have to do with times, or are intervals, instead of bare temperatures if (stristr($key2, 'date') === FALSE){ if (stristr($key2, 'change') === FALSE){ if (stristr($key2, 'avg') === FALSE){ if (stristr($key2, 'unit') === FALSE){ echo $key2; echo $val2; $weatherArray = changeTempColor($weatherArray,$key2,$val2,$subArray); } } } } } } //end of SQL Temperature CSS Styling } //END if SQLDATA foreach }//End of SQL IF } //end of FOREACH loop EDIT: YOU KNOW WHAT... As I typed this... i just figured out why it was giving me the error it was.... so, uh, Thank you, Already. But a problem remains. It was spitting back "Array" for every "$val2". Well of course it was... As unlike the first-level of the array, which holds only singular, current data, the SQLData array holds arrays of data spanning 24hrs. But I only want to check the last record (#288) of that data. So my question becomes... I still need to march through each key in $weatherArray [sqlData], and the foreach accomplishes that... but can I pick out the last record in that sub array? if (stristr($key, 'SQLData')) { // Searching for SQLData { foreach ($val as $key2 => [b]$val2[288][/b]) { Doesn't work. Thank you so much for your help. Chris http://www.alberniweather.ca Quote Link to comment https://forums.phpfreaks.com/topic/119253-solved-foreach-in-multidimensional-array/ Share on other sites More sharing options...
chrisale Posted August 12, 2008 Author Share Posted August 12, 2008 You know what... never mind again... because the array has 288 values... so the last record is 287 so this worked fine: if (stristr($key, 'SQLData')) { // Searching for SQLData { foreach ($val as $key2 => $val2) { if (stristr($key2, 'temp') || stristr($key2, 'chill') || stristr($key2, 'dew') || stristr($key2, 'heat')){ // Searching for temperature, windchill, dewpoint, and heat values if (stristr($key2, 'Time') === FALSE){// The Following Conditions get rid of the array keys that have to do with times, or are intervals, instead of bare temperatures if (stristr($key2, 'date') === FALSE){ if (stristr($key2, 'change') === FALSE){ if (stristr($key2, 'avg') === FALSE){ if (stristr($key2, 'unit') === FALSE){ echo $key2; echo $val2[287]; $weatherArray = changeTempColor($weatherArray,$key2,$val2[287],$subArray); } } } } } } //end of SQL Temperature CSS Styling } //END if SQLDATA foreach Thank you for relieving my brain cramp. I guess sometimes just asking the question really does open your mind to the answer! Quote Link to comment https://forums.phpfreaks.com/topic/119253-solved-foreach-in-multidimensional-array/#findComment-614262 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.