470,810 Members | 863 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

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+answer0 and question1+answer1 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 2644

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+answer0 and question1+answer1 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+answer0 and question1+answer1 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by ChronoFish | last post: by
21 posts views Thread by Patrick Dunford | last post: by
4 posts views Thread by LRW | last post: by
6 posts views Thread by Harry Overs | last post: by
19 posts views Thread by MMMMM | last post: by
5 posts views Thread by Geoff Jones | last post: by
12 posts views Thread by jamie | last post: by
14 posts views Thread by rocketman768 | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.