Jalz Posted July 14, 2009 Share Posted July 14, 2009 Hello, I'm trying to get the contents of d_company into an array called cust_name. The code seems to be producing an error on line beginning with cust_name. The error is Parse error: syntax error, unexpected T_FOREACH, expecting ')' Any what the error could be as I've closed brackets correctly i think? <?php $cust_name = array(foreach($CustomerList_result->getRecords() as $CustomerList_row)){ echo $CustomerList_row->getField('d_company'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165956-solved-php-array-question/ Share on other sites More sharing options...
.josh Posted July 14, 2009 Share Posted July 14, 2009 <?php foreach($CustomerList_result->getRecords() as $CustomerList_row){ $cust_name[] = $CustomerList_row->getField('d_company'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165956-solved-php-array-question/#findComment-875276 Share on other sites More sharing options...
wildteen88 Posted July 14, 2009 Share Posted July 14, 2009 EDIT CV got there first Umm. That is completely the wrong syntax. This is the correct syntax $cust_name = array(); foreach($CustomerList_result->getRecords() as $CustomerList_row) { $cust_name[] = $CustomerList_row->getField('d_company'); } Quote Link to comment https://forums.phpfreaks.com/topic/165956-solved-php-array-question/#findComment-875277 Share on other sites More sharing options...
Jalz Posted July 14, 2009 Author Share Posted July 14, 2009 Thank you so much!! Thats working exactly as I want! Quote Link to comment https://forums.phpfreaks.com/topic/165956-solved-php-array-question/#findComment-875279 Share on other sites More sharing options...
Jalz Posted July 14, 2009 Author Share Posted July 14, 2009 Thanks Guys once again, Do I need the following line as you have it wildteen88, as it does seem to work without it? $cust_name = array(); Quote Link to comment https://forums.phpfreaks.com/topic/165956-solved-php-array-question/#findComment-875287 Share on other sites More sharing options...
.josh Posted July 14, 2009 Share Posted July 14, 2009 that line initializes/declares the array, before using it. PHP does not technically need it to work, however, depending on your error message settings, it may output a notice when using an array before initializing it. Apparently your setup is not, uh, setup to show notices, otherwise you would have uh, noticed it. So in other words, it's not necessary, but it's generally good practice. Quote Link to comment https://forums.phpfreaks.com/topic/165956-solved-php-array-question/#findComment-875291 Share on other sites More sharing options...
Jalz Posted July 14, 2009 Author Share Posted July 14, 2009 Kewl, Thanks once again guys. Quote Link to comment https://forums.phpfreaks.com/topic/165956-solved-php-array-question/#findComment-875292 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.