sptrsn Posted February 25, 2012 Share Posted February 25, 2012 Instead of breaking down and actually learning how to use ajax, I'm thinking about trying to do something so I don't need a page refresh every time, and the filtered data is nearly instant. I just use the session array var, and build a new array from the rows that meet the conditions. I'm thinking of loading a two dimensional array into a session var, then use some input buttons, and use the post vars to filter the array based on a set of predefined conditions. ie status = 0, status = 1. It's a fairly small number of conditions applied to two different fields. So, I need to access specific fields within a row, and essentially create a new array either including or excluding that row depending on whether it met the condition. I'm struggling on several parts. 1. not sure how to build the array so that I can access specific fields, then not sure how to access those fields. Do I do this..... to build the array? .... while($row = mysql_fetch_assoc($result)){ $array[] = array($row['field1'], $row['field2']); } I was thinking about using a foreach and is_array() to get to each row... foreach($array as $key => $value){ if(is_array){ foreach($value as $k => $v){ if($v[0] == 1){ //stuck here $v[0] is not a field, it's the first char of the string. not sure how to access a field $new_array[] = $value; //stuck here. I need to put the whole row back into the array if the condition was true. seems like I would have to use the field selectors and rebuild the array. } } } } What would you do? Open to any ideas. Quote Link to comment https://forums.phpfreaks.com/topic/257744-helptrying-to-build-an-array-from-a-filtered-array-to-build-the-page-fast/ Share on other sites More sharing options...
freelance84 Posted February 25, 2012 Share Posted February 25, 2012 $array[] = array($row['field1'], $row['field2']); That should work fine, it will create a new numeric key with the value being the array. And as long as the $row is returning what you expecting... If the array called $array is only ever built via the method above then the $value in the first foreach will always be an array, so you don't need the is_array there. However you have not sent anything to the is_array, and could better look if you needed to use it: foreach($array as $key => $value) { if(is_array($value)) { foreach($value as $k => $v) { if($v[0] == 1) { //stuck here $v[0] is not a field, it's the first char of the string. not sure how to access a field $new_array[] = $value; //stuck here. I need to put the whole row back into the array if the condition was true. seems like I would have to use the field selectors and rebuild the array. } } } } After this you then have a nested foreach which is not really needed. Again if your array is only built in the fashion as stated at the beginning the array in $value will only ever have 2 parts to it so you don't need a foreach and could just use something like this: foreach($array as $key => $value) { if($value[0] == 1) { $new_array[] = $value; //stuck here. I need to put the whole row back into the array if the condition was true. seems like I would have to use the field selectors and rebuild the array. } } Now to deal with the value you want to keep, simply push into a new array that you wish to keep: //declaring your new array to keep $new_array = array(); //filtering through the $array foreach($array as $key => $value) { if($value[0] == 1) { $new_array[] = $value; } } And last but not least... the use of a foreach... Maniac Dan once posted on a thread of mine a while ago about the use of the function foreach and how is saps memory as it essentially make a duplicate copy before working: http://sldn.softlayer.com/blog/dmcaloon/PHP-Memory-Management-Foreach I don't know how big your $array will be but to optimize it further as the $array is simply a numeric one: for ( $p = 0; $p < count($array); $p++ ) { if($array[$p][0] == 1) { $new_array[] = $value; } } And if you change at a later date to using an associative array for the $array then use this method as illustrated by maniac dan to loop through an associative without the use of a foreach: http://www.phpfreaks.com/forums/index.php?topic=344015.msg1624048#msg1624048 Quote Link to comment https://forums.phpfreaks.com/topic/257744-helptrying-to-build-an-array-from-a-filtered-array-to-build-the-page-fast/#findComment-1321114 Share on other sites More sharing options...
freelance84 Posted February 25, 2012 Share Posted February 25, 2012 Oh and on your op the following: I was thinking about using a foreach and is_array() to get to each row... foreach($array as $key => $value){ if(is_array){ foreach($value as $k => $v){ if($v[0] == 1){ //stuck here $v[0] is not a field, it's the first char of the string. not sure how to access a field $new_array[] = $value; //stuck here. I need to put the whole row back into the array if the condition was true. seems like I would have to use the field selectors and rebuild the array. } } } } Didnt work because $v is not an array. $v is the value of the the nested array: foreach($array as $key => $value){ if(is_array){ foreach($value as $k => $v){ //you are now cycling through the array which is $value. The first time through $v is $row['field1'] and the second $v is $row['field2'] if($v[0] == 1){ $new_array[] = $value; } } } } However read the top thread as it solves all all of this i hope Quote Link to comment https://forums.phpfreaks.com/topic/257744-helptrying-to-build-an-array-from-a-filtered-array-to-build-the-page-fast/#findComment-1321117 Share on other sites More sharing options...
sptrsn Posted February 25, 2012 Author Share Posted February 25, 2012 This is great. Thank you for your input. I'm going to take another crack at this. Quote Link to comment https://forums.phpfreaks.com/topic/257744-helptrying-to-build-an-array-from-a-filtered-array-to-build-the-page-fast/#findComment-1321166 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.