doomie Posted July 4, 2010 Share Posted July 4, 2010 Hello Everyone, I got stuck. I have a headache already, please help. Problem is. Array example: $part[0][0] = "value1"; $part[0][1] = "1"; $part[1][0] = "value2"; $part[1][1] = "1"; $part[2][0] = "value3"; $part[2][1] = "2"; $part[2][0] = "value4"; $part[2][1] = "2"; $part[3][0] = "value5"; $part[3][1] = "3"; I am looping this array with for loop. In loop i need to call a function when $part[$i][1] changes. I tried to do something like this: $ch =""; for ($i=0;$i<count($part);$i++) { //making an array here based on $part[$i][0], let's call it $data. if($ch != $part[$i][1]) { function insert($data); $ch = $part[$i][1]; } } But then the function is inserting $data gathered for $part[0]. The question is: What should i to make that insert function to be called when $part[$i][1] changes ?? Quote Link to comment https://forums.phpfreaks.com/topic/206706-looping-array-issue/ Share on other sites More sharing options...
wildteen88 Posted July 4, 2010 Share Posted July 4, 2010 How you have setup your for loop is correct. However it can be cleaned up with a foreach loop function insert($data) { echo "inserting... $data<br />"; } $prev = ''; foreach($part as $data) { if($prev != $data[1]) insert($data[0]); $prev = $data[1]; } What are you planning on doing with the insert() function? Quote Link to comment https://forums.phpfreaks.com/topic/206706-looping-array-issue/#findComment-1081038 Share on other sites More sharing options...
doomie Posted July 4, 2010 Author Share Posted July 4, 2010 "Insert" function is just inserting $data into a csv file. The problem is that i want to insert just then when $part[$i][1] changes and after looping all values. Quote Link to comment https://forums.phpfreaks.com/topic/206706-looping-array-issue/#findComment-1081042 Share on other sites More sharing options...
doomie Posted July 4, 2010 Author Share Posted July 4, 2010 Your example does not work. Because before "if" i am making an array. And with function i do insert an array - not a value and not the array i am looping. To be more clearly i want him to make the array then to call insert function. Quote Link to comment https://forums.phpfreaks.com/topic/206706-looping-array-issue/#findComment-1081047 Share on other sites More sharing options...
doomie Posted July 4, 2010 Author Share Posted July 4, 2010 Help please! Quote Link to comment https://forums.phpfreaks.com/topic/206706-looping-array-issue/#findComment-1081076 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.