Connecting Tech Pros Worldwide Forums | Help | Site Map

Array Processing

J Huntley Palmer
Guest
 
Posts: n/a
#1: Mar 3 '06
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
)

)

Sjoerd
Guest
 
Posts: n/a
#2: Mar 3 '06

re: Array Processing



J Huntley Palmer wrote:[color=blue]
> 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.[/color]

Do you mean this:

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

J Huntley Palmer
Guest
 
Posts: n/a
#3: Mar 3 '06

re: Array Processing


Sjoerd wrote:[color=blue]
> J Huntley Palmer wrote:[color=green]
>> 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.[/color]
>
> Do you mean this:
>
> for ($i = 0; $i < count($arr[0]); $i++) {
> $result[] = $arr[0][$i].$arr[1][$i];
> }
>[/color]

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
J Huntley Palmer
Guest
 
Posts: n/a
#4: Mar 3 '06

re: Array Processing


Sjoerd wrote:[color=blue]
> J Huntley Palmer wrote:[color=green]
>> 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.[/color]
>
> Do you mean this:
>
> for ($i = 0; $i < count($arr[0]); $i++) {
> $result[] = $arr[0][$i].$arr[1][$i];
> }
>[/color]


This will also create an out of bounds error since there are named
indexes mixed in.
Oli Filth
Guest
 
Posts: n/a
#5: Mar 3 '06

re: Array Processing


J Huntley Palmer said the following on 03/03/2006 17:43:[color=blue]
> 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
> )
>
> )[/color]

$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
Janwillem Borleffs
Guest
 
Posts: n/a
#6: Mar 4 '06

re: Array Processing


J Huntley Palmer wrote:[color=blue]
> 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.
>[/color]

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



Closed Thread