473,786 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array Processing

How may I concatenate the index respective questions and answers into
one string per index using ONE foreach loop? I do not want to use
numerical indexes just the named ones as below.

In other words, in one foreach loop, if possible, I want to have the
results as question0+answe r0 and question1+answe r1 in 2 separate
strings entered into an array.

Thanks much.

Array
(
[0] => Array
(
[0] => question0
[1] => question1
)

[QUEST] => Array
(
[0] => question0
[1] => question1
)

[ANSW] => Array
(
[0] => answer0
[1] => answer1
)

[1] => Array
(
[0] => answer0
[1] => answer1
)

)
Mar 3 '06 #1
5 2765

J Huntley Palmer wrote:
How may I concatenate the index respective questions and answers into
one string per index using ONE foreach loop? I do not want to use
numerical indexes just the named ones as below.


Do you mean this:

for ($i = 0; $i < count($arr[0]); $i++) {
$result[] = $arr[0][$i].$arr[1][$i];
}

Mar 3 '06 #2
Sjoerd wrote:
J Huntley Palmer wrote:
How may I concatenate the index respective questions and answers into
one string per index using ONE foreach loop? I do not want to use
numerical indexes just the named ones as below.


Do you mean this:

for ($i = 0; $i < count($arr[0]); $i++) {
$result[] = $arr[0][$i].$arr[1][$i];
}


Actually I wanter to use the named indexes such as [QUEST] AND [ANSW]
not the numerical ones.

Array
(
[0] => Array
(
[0] => question0
[1] => question1
)

[QUEST] => Array
(
[0] => question0
[1] => question1
)

[ANSW] => Array
(
[0] => answer0
[1] => answer1
)

[1] => Array
(
[0] => answer0
[1] => answer1
)

)
Thanks
Mar 3 '06 #3
Sjoerd wrote:
J Huntley Palmer wrote:
How may I concatenate the index respective questions and answers into
one string per index using ONE foreach loop? I do not want to use
numerical indexes just the named ones as below.


Do you mean this:

for ($i = 0; $i < count($arr[0]); $i++) {
$result[] = $arr[0][$i].$arr[1][$i];
}

This will also create an out of bounds error since there are named
indexes mixed in.
Mar 3 '06 #4
J Huntley Palmer said the following on 03/03/2006 17:43:
How may I concatenate the index respective questions and answers into
one string per index using ONE foreach loop? I do not want to use
numerical indexes just the named ones as below.

In other words, in one foreach loop, if possible, I want to have the
results as question0+answe r0 and question1+answe r1 in 2 separate
strings entered into an array.

Thanks much.

Array
(
[0] => Array
(
[0] => question0
[1] => question1
)

[QUEST] => Array
(
[0] => question0
[1] => question1
)

[ANSW] => Array
(
[0] => answer0
[1] => answer1
)

[1] => Array
(
[0] => answer0
[1] => answer1
)

)


$stuff = array("QUEST" => array(0 => "question0" , 1 => "question1" ),
"ANSW" => array(0 => "answer0", 1 => "answer1")) ;
$strings = null;
foreach ($stuff["QUEST"] as $key => $value)
{
$strings[$key] = $value . $stuff["ANSW"][$key];
}

--
Oli
Mar 3 '06 #5
J Huntley Palmer wrote:
How may I concatenate the index respective questions and answers into
one string per index using ONE foreach loop? I do not want to use
numerical indexes just the named ones as below.

In other words, in one foreach loop, if possible, I want to have the
results as question0+answe r0 and question1+answe r1 in 2 separate
strings entered into an array.


How do you want to establish the relationship between the questions and
answers? With your example array, only the following would make sence:

$results = array(
$array['QUEST'][0] . $array['ANSWER'][0],
$array['QUEST'][1] . $array['ANSWER'][1]
);
JW

Mar 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3957
by: ChronoFish | last post by:
Hi there, I want to iterate through an array starting at a known index. However the indexes are not linear. For example I have an array of events keyed by timestamp. $eventList = array (1064263264 => "event1", 10642635555 => "event2", 1064266666 => "event3", 1064267782 => "event4", 1064268812 => "event5"); I basically want to do a "for" or "foreach" but I don't necessarily want to start at key 1064263264 (event1).
21
6460
by: Patrick Dunford | last post by:
PHP allows control arrays in forms e.g. this is a counter editing script echo "<input name=\"counterdelname\" type=\"hidden\" value=\""; echo "<input name=\"countername\" type=\"hidden\" value=\"".$namestr."\">"; echo "<input name=\"newvalue\" size=".VALUEWIDTH." value=\"".$valuestr."\">";
4
1771
by: LRW | last post by:
I'm not even sure this is possible, and if it is, I'm having a hard time focusing on a function that would make it work. What I have is a dynamically generated form with dozens of rows from a mySQL query. Each row in this form has a checkbox to select what rows the user wants sent to the processing page. Each row also has a slew of various text fields and dropdown menus with data from the query and editable by the user. The user checks...
3
2147
by: jdemello | last post by:
When using a multidimensional associative array to cross-reference data in a second, single-dimensional array, PHP stops processing data after a few iterations. Memory usage according to the task manager doesn't seem to spike and CPU usage only gets as high as 11 to 13 percent. A sample script is provided below, any help or alternative solutions are welcome. (By the way, the script below works fine on Linux with the same version of Apache...
6
6313
by: Harry Overs | last post by:
My program needs to take a pointer to BYTE array (unsigned char*) and convert it into a STL list so that each BYTE in the array has its own element in the list, i.e. if the array has hundred bytes then the list needs to have a hundred entries, at present when I try to do this each element in my list points to the entire BYTE array, when what I really need is copies of each single BYTE in its own part of the list. I have used code similar...
19
2455
by: MMMMM | last post by:
Hmmm, my array seems to be emptying itself for no reason. I just want to grab every form element posted to the page into an appropriately sized array... What is going on here? dim arrShort() Elements = 0 dim frm for each frm in Request.Form Elements = Elements + 1
5
1680
by: Geoff Jones | last post by:
Hi Suppose we have an array of DataRows e.g. Dim drMyRows() As DataRow = myTable.Rows(0).GetChildRows("PricesCompany") generated via a relationship. The question I have is this. Isn't this in essence a table i.e. a collection of rows? The reason I ask is that I wanted to extract from the array of rows
12
1942
by: jamie | last post by:
I wish to create an array that points to, say, the middle section of a different array. eg Original array > second array-------------^ Is this at all possible?? I think that it could be done in c++, but I dont have a clue as to how to do it in vb.net
14
52477
by: rocketman768 | last post by:
Man, that title should be in a poem, but anyways...So, I have this program, and it has an array of 40 million elements. The problem is that I have a for loop that continually is doing stuff to this array, and for each iteration, I have to zero out the array. This is how I am currently doing it: // Zero out the lnscores for( count=0; count < chunksize; count++ ) lnscores = 0; Is there no quicker way to do this? I know there must be...
2
1324
by: nospam | last post by:
I have an application that processes 30 kinds of strings of that fit in an array. By processing, I mean -check the value of certain characters or substrings to be within contant bounds or a member of a list -check the value of certain characters or substrings to be within bounds defined by other characters or substrings -call a series of functions based on value of certain substrings.
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.