Connecting Tech Pros Worldwide Forums | Help | Site Map

Query on explode()

mosesdinakaran@gmail.com
Guest
 
Posts: n/a
#1: Oct 16 '06
Hi All,

Have a look at the following code

$test = explode('-','one-two-three-four-five');
print_r($test);

Output:
Array
(
[0] =one
[1] =two
[2] =three
[3] =four
[4] =five
)

But What I need is

Array
(
[one] =one
[two] =two
[three] =three
[four] =four
[five] =five
)

Any Help Please........

regards
moses


Janwillem Borleffs
Guest
 
Posts: n/a
#2: Oct 16 '06

re: Query on explode()


mosesdinakaran@gmail.com schreef:
Quote:
But What I need is
>
Array
(
[one] =one
[two] =two
[three] =three
[four] =four
[five] =five
)
>
For PHP5, you can use array_combine:

http://www.php.net/manual/hk/function.array-combine.php

For PHP4, have a look at the user contributions on that page.


JW
NurAzije
Guest
 
Posts: n/a
#3: Oct 16 '06

re: Query on explode()


Try this one, this will work :
---------------------------------------------------
$test = explode('-','one-two-three-four-five');
$view = new Array();
foreach($test as $boo =$foo){
$view[$boo] = $foo;
}
print_r($view);
------------------------------------------------------

For php/ajax/javascript tutorials and tips, visit me on my blog at
http://www.nurazije.co.nr

.:[ ikciu ]:.
Guest
 
Posts: n/a
#4: Oct 16 '06

re: Query on explode()


Hmm NurAzije <nurazije@gmail.comwrote:
Quote:
Try this one, this will work :
---------------------------------------------------
$test = explode('-','one-two-three-four-five');
$view = new Array();
foreach($test as $boo =$foo){
$view[$boo] = $foo;
}
print_r($view);

heheheh

i can write your solution a bit simpler

$view = $test;

DONE - but your solution is wrong


correct solution:
$view = array_combine($test, $test);

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

2be || !2be $this =mysql_query();


NurAzije
Guest
 
Posts: n/a
#5: Oct 16 '06

re: Query on explode()


Try this one, this will work :
---------------------------------------------------
$test = explode('-','one-two-three-four-five');
$view = new Array();
foreach($test as $boo =$foo){
$view[$foo] = $foo; // typing error, do it like this
}

print_r($view);
------------------------------------------------------

For php/ajax/javascript tutorials and tips, visit me on my blog at
http://www.nurazije.co.nr

.:[ ikciu ]:.
Guest
 
Posts: n/a
#6: Oct 16 '06

re: Query on explode()


Hmm NurAzije <nurazije@gmail.comwrote:
Quote:
Try this one, this will work :
---------------------------------------------------
$test = explode('-','one-two-three-four-five');
$view = new Array();
foreach($test as $boo =$foo){
$view[$foo] = $foo; // typing error, do it like this
}
>
print_r($view);
still too much of lines ... try array_combine


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

2be || !2be $this =mysql_query();


Rik
Guest
 
Posts: n/a
#7: Oct 17 '06

re: Query on explode()


..:[ ikciu ]:. wrote:
Quote:
Hmm NurAzije <nurazije@gmail.comwrote:
Quote:
>Try this one, this will work :
>---------------------------------------------------
>$test = explode('-','one-two-three-four-five');
>$view = new Array();
>foreach($test as $boo =$foo){
> $view[$foo] = $foo; // typing error, do it like this
>}
>>
>print_r($view);
>
still too much of lines ... try array_combine
Which Jan-Willem already said, and stated that it doesn't work on PHP < 5.
You seem to miss that the poster is trying to give a solution for PHP4.

About the question why the OP would want an array like this, it's beyond
me. I cannot see any advantage, but then again, I don't know what he's
trying to accomplish

Grtz,
--
Rik Wasmus


.:[ ikciu ]:.
Guest
 
Posts: n/a
#8: Oct 17 '06

re: Query on explode()


Hmm Rik <luiheidsgoeroe@hotmail.comwrote:
Quote:
Which Jan-Willem already said, and stated that it doesn't work on PHP
< 5. You seem to miss that the poster is trying to give a solution
for PHP4.
ofc, but if some1 didn't write what php does he use i try to answer with
that php what i actualy use


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

2be || !2be $this =mysql_query();


Rik
Guest
 
Posts: n/a
#9: Oct 18 '06

re: Query on explode()


..:[ ikciu ]:. wrote:
Quote:
Hmm Rik <luiheidsgoeroe@hotmail.comwrote:
Quote:
>Which Jan-Willem already said, and stated that it doesn't work on PHP
>< 5. You seem to miss that the poster is trying to give a solution
>for PHP4.
>
ofc, but if some1 didn't write what php does he use i try to answer
with that php what i actualy use
It's someone, I thought...

First of all, a lot of hosting companies still use PHP4.
Second of all, why do you keep insisting on giving an answer for PHP5,
which was already given 2 hours before your first post in the thread, as
the very first reply on the OP?

Grtz,
--
Rik Wasmus


NurAzije
Guest
 
Posts: n/a
#10: Oct 18 '06

re: Query on explode()


I think he wants to show the world he knows PHP5 ;)
------------------------------------------------------

For php/ajax/javascript tutorials and tips, visit me on my blog at
http://www.nurazije.co.nr

Closed Thread