Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/86396-array-syntax-question/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/86396-array-syntax-question/#findComment-441520
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/86396-array-syntax-question/#findComment-441565
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/86396-array-syntax-question/#findComment-441569
Share on other sites

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.