Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.