Jump to content

[SOLVED] php array question


Jalz

Recommended Posts

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');
  } ?>

Link to comment
https://forums.phpfreaks.com/topic/165956-solved-php-array-question/
Share on other sites

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');
}

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.