drbigfresh Posted January 17, 2008 Share Posted January 17, 2008 I am currently building an array as I loop through some data. Currently I am doing this (I've shortened the array for posting purposes): if(is_array($ListInformation)) { array_push ($ListInformation,array( listid => $result->fields['id'] )); //End Master Array } else { $ListInformation = array( array( listid => $result->fields['id'] ),); //End Master Array } // End We Have Info. Is there a better way to do this so I don't have to add the array info in 2 separate steps? Since right now I am checking to see if it is an array, and if so I am using array_push. If it isn't an array I create it. But it is a little cumbersome.... Quote Link to comment https://forums.phpfreaks.com/topic/86396-array-syntax-question/ Share on other sites More sharing options...
predator Posted January 17, 2008 Share Posted January 17, 2008 can u list all the code for this action so i can see how you are doing it. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/86396-array-syntax-question/#findComment-441515 Share on other sites More sharing options...
hitman6003 Posted January 17, 2008 Share Posted January 17, 2008 create $ListInformation as an array at the start so you always know it is one...and you don't need to use array_push.... This will give you the same result you currently have: $ListInformation = array(); while ... { $ListInformation[]['listid'] = $result->fields['id']; } Quote Link to comment https://forums.phpfreaks.com/topic/86396-array-syntax-question/#findComment-441520 Share on other sites More sharing options...
drbigfresh Posted January 17, 2008 Author Share Posted January 17, 2008 Hmmmm... I can't seem to get it to work. This is my code that I have: (it is inside a loop going though information from a database) if(is_array($ListInformation)) { array_push ($ListInformation,array( listid => $result->fields['id'], listname => $result->fields['listname'], listdescription => $result->fields['description'], thumbsup => $VoteUp, thumbsdown => $VoteDown, listcreated =>$result->fields['created'], totalitems => $result->fields['totalitems'], totalviews => $result->fields['totalviews'], sadellink => $sadellink, city => $result->fields['city'], state => $result->fields['state'], country => $result->fields['country'], published => $result->fields['published'], publisheddate => $result->fields['publisheddate'], zip => $result->fields['zipcode'], publicedit => $result->fields['publicedit'], url => $result->fields['url'], permalink => $PermaURL, keywords => $result->fields['keywords'], listrssfeed => $RSSFeed, recordsreturned => $RecordsReturned, creatorid => $result->fields['uid'] )); //End Master Array } else { $ListInformation = array( array( listid => $result->fields['id'], listname => $result->fields['listname'], listdescription => $result->fields['description'], listcreated =>$result->fields['created'], thumbsup => $VoteUp, thumbsdown => $VoteDown, totalitems => $result->fields['totalitems'], totalviews => $result->fields['totalviews'], sadellink => $sadellink, city => $result->fields['city'], state => $result->fields['state'], country => $result->fields['country'], published => $result->fields['published'], publisheddate => $result->fields['publisheddate'], zip => $result->fields['zipcode'], publicedit => $result->fields['publicedit'], url => $result->fields['url'], permalink => $PermaURL, keywords => $result->fields['keywords'], listrssfeed => $RSSFeed, recordsreturned => $RecordsReturned, creatorid => $result->fields['uid'] ),); //End Master Array } // End Is Array Quote Link to comment https://forums.phpfreaks.com/topic/86396-array-syntax-question/#findComment-441565 Share on other sites More sharing options...
hitman6003 Posted January 17, 2008 Share Posted January 17, 2008 $ListInformation = array(); while ... { $ListInformation[] = array( 'listid' => $result->fields['id'], 'listname' => $result->fields['listname'], 'listdescription' => $result->fields['description'], 'thumbsup' => $VoteUp, 'thumbsdown' => $VoteDown, 'listcreated' =>$result->fields['created'], 'totalitems' => $result->fields['totalitems'], 'totalviews' => $result->fields['totalviews'], 'sadellink' => $sadellink, 'city' => $result->fields['city'], 'state' => $result->fields['state'], 'country' => $result->fields['country'], 'published' => $result->fields['published'], 'publisheddate' => $result->fields['publisheddate'], 'zip' => $result->fields['zipcode'], 'publicedit' => $result->fields['publicedit'], 'url' => $result->fields['url'], 'permalink' => $PermaURL, 'keywords' => $result->fields['keywords'], 'listrssfeed' => $RSSFeed, 'recordsreturned' => $RecordsReturned, 'creatorid' => $result->fields['uid'] ); } Quote Link to comment https://forums.phpfreaks.com/topic/86396-array-syntax-question/#findComment-441569 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.