473,387 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Is there a more efficent way to doing this?

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
5 1573
"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

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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Boris Boutillier | last post by:
Hi all, I'm looking for parsing a Verilog file in my python module, is there already such a tool in python (a module in progress) to help instead of doing a duplicate job. And do you know of...
6
by: Kyler Laird | last post by:
I'm trying to discover if there's an efficient way to determine if all of the values of a Numeric array are the same. In C, I would search from the second value, checking each against the first...
3
by: Amy G | last post by:
I have a whole bunch of tuples that look something like this, aTuple = ('1074545869.6580.msg', 't_bryan_pw@gmcc.ab.ca', 'Your one stop prescriptions') now that I have this I try for x, y, z...
0
by: Daniel Kasak | last post by:
Hi all. I need to test whether the first bit of a field is numeric. For example, the field might contain: 154 boxes I'm currently testing it by doing: if(abs(left(field,locate(' ',...
15
by: Andrew Brampton | last post by:
Hi, I'm new to using std classes, but I was wondering how I could do the following efficiently with the string class: Say I had a string with delimited values such as:...
3
by: Laurence | last post by:
Hi there, Does somebody know the efficent way to connect DB2/400? Through iSeries Access ODBC/OLEDB driver or DB2 Connect? Which will more fast and efficent? In addition, does DB2 Connect use...
0
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005 and .net 2.0. I have used 2 DirectorySearchers to retrieve Active Directory data and put them into 2 separate dataset tables. I want to create a 3rd dataset table by using the...
2
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005 and .net 2.0 I used 2 DirectorySearchers to retrieve data from Active Directory and placed them in 2 dataset tables. I need to perform the SQL equuvakebt of join operation to...
10
by: DavidSeck.com | last post by:
Hi, I am working with the Facebook API right now, an I have kind of a problem, but I don't know what I am doing wrong. So I have a few arrays, f.ex.: User albums: array(2) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.