By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,772 Members | 1,272 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,772 IT Pros & Developers. It's quick & easy.

Is there a more efficent way to doing this?

P: n/a
I need to make an associated array (all with the same key name) using values
from another array. Is there a more efficient way to doing this in one pass
(instead of looping)?

I'm always looking to learn something new. :-)

--------------------------------------------------------------------

$fields = array('username', 'passwd', 'firstname', 'lastname', 'email');

foreach ($fields as $field_entry)
{
$list[] = array( 'field_name' => $field_entry);
}

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Bosconian" <bo*******@planetx.com> wrote in message
news:up********************@comcast.com...
I need to make an associated array (all with the same key name) using values from another array. Is there a more efficient way to doing this in one pass (instead of looping)?

I'm always looking to learn something new. :-)

--------------------------------------------------------------------

$fields = array('username', 'passwd', 'firstname', 'lastname', 'email');

foreach ($fields as $field_entry)
{
$list[] = array( 'field_name' => $field_entry);
}


I should add that the array $fields is actually a list of items returned
from a object.
Jul 17 '05 #2

P: n/a

Bosconian wrote:
"Bosconian" <bo*******@planetx.com> wrote in message
news:up********************@comcast.com...
I need to make an associated array (all with the same key name) using
values
from another array. Is there a more efficient way to doing this in
one pass
(instead of looping)?

I'm always looking to learn something new. :-)
--------------------------------------------------------------------
$fields = array('username', 'passwd', 'firstname', 'lastname', 'email');
foreach ($fields as $field_entry)
{
$list[] = array( 'field_name' => $field_entry);
}


I should add that the array $fields is actually a list of items

returned from a object.


What, exactly, are you trying to do. The above code does not give you
what you said you wanted. Did you try it?

Does this give you what you want:
<?
$fields = array('username', 'passwd', 'firstname', 'lastname',
'email');
foreach ($fields as $field_entry)
$list['field_name'][] = $field_entry;
?>

Ken

Jul 17 '05 #3

P: n/a
Bosconian wrote:
I need to make an associated array (all with the same key name) using
values from another array. Is there a more efficient way to doing this in
one pass (instead of looping)?

I'm always looking to learn something new. :-)

--------------------------------------------------------------------

$fields = array('username', 'passwd', 'firstname', 'lastname', 'email');

foreach ($fields as $field_entry)
{
$list[] = array( 'field_name' => $field_entry);
}


There are a variety of interesting array functions, like array_merge, and
array_combine. Neither does quite what you are doing here, but they may
give you ideas.

But what are you trying to do here? Your original array is arguably much
easier to use than the new one you create, which buries the column names in
an inaccessible way w/o giving any advantage. What are you gaining by
this?

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #4

P: n/a
"Kenneth Downs" <kn**************@see.sigblock> wrote in message
news:ut************@pluto.downsfam.net...
Bosconian wrote:
I need to make an associated array (all with the same key name) using
values from another array. Is there a more efficient way to doing this in one pass (instead of looping)?

I'm always looking to learn something new. :-)

--------------------------------------------------------------------

$fields = array('username', 'passwd', 'firstname', 'lastname', 'email');

foreach ($fields as $field_entry)
{
$list[] = array( 'field_name' => $field_entry);
}
There are a variety of interesting array functions, like array_merge, and
array_combine. Neither does quite what you are doing here, but they may
give you ideas.

But what are you trying to do here? Your original array is arguably much
easier to use than the new one you create, which buries the column names

in an inaccessible way w/o giving any advantage. What are you gaining by
this?

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)


The first array is a list of field names returned by a class function. The
second array associates the same key name for output to the template page
(looping however many times the number of elements.)

I was simply wondering if there was a more efficient way of populating the
associated array with the contents of the first array. There won't ever be
more than a couple dozen items in the first array so using the foreach loop
is not expensive.
Jul 17 '05 #5

P: n/a
On Sat, 30 Apr 2005 19:18:50 -0700, Bosconian wrote:
I need to make an associated array (all with the same key name) using values
from another array. Is there a more efficient way to doing this in one pass
(instead of looping)?

I'm always looking to learn something new. :-)

--------------------------------------------------------------------

$fields = array('username', 'passwd', 'firstname', 'lastname', 'email');

foreach ($fields as $field_entry)
{
$list[] = array( 'field_name' => $field_entry);
}


Reading the various responses, I think what you want is this:

$fields = array('username', 'passwd', 'firstname', 'lastname', 'email');
$list = array('field_name' => $fields);

The last line can be split into two, in case the $list array already
exists and you want to add a new key (so don't do the first line of the
next two below):

$list = array();
$list['field_name'] = $fields;

This makes the $list array have an element with key 'field_name' which
itself is an array. Then to use this in your code:

foreach ($list['field_name'] as $field) {
// do something with $field
}

If $list contains various arrays, you can loop over all of them like:

foreach ($list as $key => $fieldarray) {
foreach ($fieldarray as $field) {
// do something with $field and $key
}
}

- steve
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.