By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

single array to multidimensional

P: n/a
Hi there,

I have a string, comma separated, with links and their respective URLs
in it. Example:
Google,http://www.google.com,Yahoo!,http://www.yahoo.com,WikiPedia,http://www.wikipedia.org

etc, etc.

i make an array of this with explode( ',', $string );

now what i wonder is how can i turn this array into something like
this:

$array[0]['name'] = Google
$array[0]['url'] = http://www.google.com
$array[1]['name'] = Yahoo!
$array[1]['url'] = http://www.yahoo.com
$array[2]['name'] = WikiPedia
$array[2]['url'] = http://www.wikipedia.org

I hope/think this is all clear ...

Frizzle.

Jun 22 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
frizzle wrote:
Hi there,

I have a string, comma separated, with links and their respective URLs
in it. Example:
Google,http://www.google.com,Yahoo!,http://www.yahoo.com,WikiPedia,http://www.wikipedia.org

etc, etc.

i make an array of this with explode( ',', $string );

now what i wonder is how can i turn this array into something like
this:

$array[0]['name'] = Google
$array[0]['url'] = http://www.google.com
$array[1]['name'] = Yahoo!
$array[1]['url'] = http://www.yahoo.com
$array[2]['name'] = WikiPedia
$array[2]['url'] = http://www.wikipedia.org

I hope/think this is all clear ...

Frizzle.

Something like the following should do it:

$a = explode(",", $string)
for($i=0,$j=0; $i<count($a), $i+=2,$j++) {
$array[$j]["name"] = $a[$i];
$array[$j]["url"] = $a[$i+1];
}
Jun 22 '06 #2

P: n/a
On 22 Jun 2006 09:24:04 -0700, "frizzle" <ph********@gmail.com> wrote:
I have a string, comma separated, with links and their respective URLs
in it. Example:
Google,http://www.google.com,Yahoo!,http://www.yahoo.com,WikiPedia,http://www.wikipedia.org

etc, etc.

i make an array of this with explode( ',', $string );

now what i wonder is how can i turn this array into something like
this:

$array[0]['name'] = Google
$array[0]['url'] = http://www.google.com
$array[1]['name'] = Yahoo!
$array[1]['url'] = http://www.yahoo.com
$array[2]['name'] = WikiPedia
$array[2]['url'] = http://www.wikipedia.org


<?php
$str =
"Google,http://www.google.com,Yahoo!,http://www.yahoo.com,WikiPedia,http://www.wikipedia.org";
$parts = explode(',', $str);
$array = array();
for ($i=0; $i<count($parts)/2; $i++)
{
$array[$i]['name'] = $parts[$i*2];
$array[$i]['url'] = $parts[$i*2+1];
}

print "<pre>";
var_export($array);
print "</pre>"
?>

There are undoubtably faster, more concise and cleverer ways of doing this.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Jun 22 '06 #3

P: n/a

Andy Hassall wrote:
On 22 Jun 2006 09:24:04 -0700, "frizzle" <ph********@gmail.com> wrote:
I have a string, comma separated, with links and their respective URLs
in it. Example:
Google,http://www.google.com,Yahoo!,http://www.yahoo.com,WikiPedia,http://www.wikipedia.org

etc, etc.

i make an array of this with explode( ',', $string );

now what i wonder is how can i turn this array into something like
this:

$array[0]['name'] = Google
$array[0]['url'] = http://www.google.com
$array[1]['name'] = Yahoo!
$array[1]['url'] = http://www.yahoo.com
$array[2]['name'] = WikiPedia
$array[2]['url'] = http://www.wikipedia.org


<?php
$str =
"Google,http://www.google.com,Yahoo!,http://www.yahoo.com,WikiPedia,http://www.wikipedia.org";
$parts = explode(',', $str);
$array = array();
for ($i=0; $i<count($parts)/2; $i++)
{
$array[$i]['name'] = $parts[$i*2];
$array[$i]['url'] = $parts[$i*2+1];
}

print "<pre>";
var_export($array);
print "</pre>"
?>

There are undoubtably faster, more concise and cleverer ways of doing this.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

Thank you both! They look quite complex to me, but i understand what
they do,
by using the arrays index keys.

Thanks again!

Frizzle.

Jun 22 '06 #4

P: n/a
Andy Hassall wrote:
On 22 Jun 2006 09:24:04 -0700, "frizzle" <ph********@gmail.com> wrote:
I have a string, comma separated, with links and their respective URLs
in it. Example:
Google,http://www.google.com,Yahoo!,http://www.yahoo.com,WikiPedia,http://www.wikipedia.org

etc, etc.

Ooh, seems to be "comma separated string" week.

[snipped non-regex code :)] There are undoubtably faster, more concise and cleverer ways of doing
this.


Rik and Andy J. proved regular expressions was faster and says it is the
clever thing to do. But I am not sure about concise.

$string
= "Google,http://www.google.com,Yahoo!,http://www.yahoo.com,WikiPedia,http://www.wikipedia.org";

function mickey($minnie, $goofy=null) {
static $pluto = array();
if ($goofy) return $pluto;
$pluto[] = array('name'=>$minnie[1],'url'=>$minnie[2]);
return $goofy;
}

preg_replace_callback('/([^,]*),([^,]*),?/','mickey',$string);

$array = mickey('loves','minnie');

print_r($array);
--
/Bent
Jun 22 '06 #5

P: n/a
Andy Hassall wrote:

There are undoubtably faster, more concise and cleverer ways of doing this.


Which would involve, of course, regular expression :-)

preg_match_all('/(?P<name>[^,]+),(?P<url>[^,]+)/', $list, $m,
PREG_SET_ORDER);

Enjoy!

Jun 22 '06 #6

P: n/a
On 22 Jun 2006 13:21:50 -0700, "Chung Leong" <ch***********@hotmail.com> wrote:
Andy Hassall wrote:

There are undoubtably faster, more concise and cleverer ways of doing this.


Which would involve, of course, regular expression :-)

preg_match_all('/(?P<name>[^,]+),(?P<url>[^,]+)/', $list, $m,
PREG_SET_ORDER);


Ah ha, was trying to work out how to get it with PCRE, knew it was possible
:-)

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Jun 22 '06 #7

P: n/a

Andy Hassall wrote:
On 22 Jun 2006 13:21:50 -0700, "Chung Leong" <ch***********@hotmail.com> wrote:
Andy Hassall wrote:

There are undoubtably faster, more concise and cleverer ways of doing this.


Which would involve, of course, regular expression :-)

preg_match_all('/(?P<name>[^,]+),(?P<url>[^,]+)/', $list, $m,
PREG_SET_ORDER);


Ah ha, was trying to work out how to get it with PCRE, knew it was possible
:-)

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool


I'm sorry, but i don't completely understand the logics in the
Disny-function, and i don't completely understand the logics in the
pattern of the preg_match_all part.

Could someone please explain a little bit more ?

Frizzle.

Jun 23 '06 #8

P: n/a

frizzle wrote:
Andy Hassall wrote:
On 22 Jun 2006 13:21:50 -0700, "Chung Leong" <ch***********@hotmail.com> wrote:
Andy Hassall wrote:
>
> There are undoubtably faster, more concise and cleverer ways of doing this.

Which would involve, of course, regular expression :-)

preg_match_all('/(?P<name>[^,]+),(?P<url>[^,]+)/', $list, $m,
PREG_SET_ORDER);


Ah ha, was trying to work out how to get it with PCRE, knew it was possible
:-)

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool


I'm sorry, but i don't completely understand the logics in the
Disny-function, and i don't completely understand the logics in the
pattern of the preg_match_all part.

Could someone please explain a little bit more ?

Frizzle.


Especially the "?P" - part in the pattern i cannot figure out ...

Frizzle.

Jun 23 '06 #9

P: n/a

frizzle wrote:
frizzle wrote:
Andy Hassall wrote:
On 22 Jun 2006 13:21:50 -0700, "Chung Leong" <ch***********@hotmail.com> wrote:

>Andy Hassall wrote:
>>
>> There are undoubtably faster, more concise and cleverer ways of doing this.
>
>Which would involve, of course, regular expression :-)
>
>preg_match_all('/(?P<name>[^,]+),(?P<url>[^,]+)/', $list, $m,
>PREG_SET_ORDER);

Ah ha, was trying to work out how to get it with PCRE, knew it was possible
:-)

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool


I'm sorry, but i don't completely understand the logics in the
Disny-function, and i don't completely understand the logics in the
pattern of the preg_match_all part.

Could someone please explain a little bit more ?

Frizzle.


Especially the "?P" - part in the pattern i cannot figure out ...

Frizzle.


The use of (?P<> ... ) indicates named capturing. It only affects how
the captured texts are referenced. Neither the P, the angle brackets,
or the text inside them are a part of the pattern. Thus

/(?P<name>[^,]+),(?P<url>[^,]+)/

is the exact same pattern as

/([^,]+),([^,]+)/

Compare the results from using the two and you'll see the difference.
Here a code snippet:

<?php

header('Content-Type: text/plain');
$list = "Google,http://www.google.com";
preg_match_all('/([^,]+),([^,]+)/', $list, $m1, PREG_SET_ORDER);
preg_match_all('/(?P<name>[^,]+),(?P<url>[^,]+)/', $list, $m2,
PREG_SET_ORDER);
var_dump($m1);
var_dump($m2);

?>

Look in the manual for how preg_match_all() works.

Jun 23 '06 #10

P: n/a

Chung Leong wrote:
frizzle wrote:
frizzle wrote:
Andy Hassall wrote:
> On 22 Jun 2006 13:21:50 -0700, "Chung Leong" <ch***********@hotmail.com> wrote:
>
> >Andy Hassall wrote:
> >>
> >> There are undoubtably faster, more concise and cleverer ways of doing this.
> >
> >Which would involve, of course, regular expression :-)
> >
> >preg_match_all('/(?P<name>[^,]+),(?P<url>[^,]+)/', $list, $m,
> >PREG_SET_ORDER);
>
> Ah ha, was trying to work out how to get it with PCRE, knew it was possible
> :-)
>
> --
> Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
> http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

I'm sorry, but i don't completely understand the logics in the
Disny-function, and i don't completely understand the logics in the
pattern of the preg_match_all part.

Could someone please explain a little bit more ?

Frizzle.


Especially the "?P" - part in the pattern i cannot figure out ...

Frizzle.


The use of (?P<> ... ) indicates named capturing. It only affects how
the captured texts are referenced. Neither the P, the angle brackets,
or the text inside them are a part of the pattern. Thus

/(?P<name>[^,]+),(?P<url>[^,]+)/

is the exact same pattern as

/([^,]+),([^,]+)/

Compare the results from using the two and you'll see the difference.
Here a code snippet:

<?php

header('Content-Type: text/plain');
$list = "Google,http://www.google.com";
preg_match_all('/([^,]+),([^,]+)/', $list, $m1, PREG_SET_ORDER);
preg_match_all('/(?P<name>[^,]+),(?P<url>[^,]+)/', $list, $m2,
PREG_SET_ORDER);
var_dump($m1);
var_dump($m2);

?>

Look in the manual for how preg_match_all() works.


Ok, thanks, this named capturing is new to me, but looks kind of ideal!
Thanks a lot guys!

I have one little question left, not really essential, but to clean
things up:
can the preg-match-all be adjusted that output would be

[0] => Array
(
[name] => Google
[url] => http://www.google.com
)

instead of (current):

[0] => Array
(
[0] => Google,http://www.google.com
[name] => Google
[1] => Google
[url] => http://www.google.com
[2] => http://www.google.com
)

per name/url -set ?

Frizzle.

Jun 23 '06 #11

P: n/a

frizzle wrote:
Chung Leong wrote:
frizzle wrote:
frizzle wrote:
> Andy Hassall wrote:
> > On 22 Jun 2006 13:21:50 -0700, "Chung Leong" <ch***********@hotmail.com> wrote:
> >
> > >Andy Hassall wrote:
> > >>
> > >> There are undoubtably faster, more concise and cleverer ways of doing this.
> > >
> > >Which would involve, of course, regular expression :-)
> > >
> > >preg_match_all('/(?P<name>[^,]+),(?P<url>[^,]+)/', $list, $m,
> > >PREG_SET_ORDER);
> >
> > Ah ha, was trying to work out how to get it with PCRE, knew it was possible
> > :-)
> >
> > --
> > Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
> > http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
>
> I'm sorry, but i don't completely understand the logics in the
> Disny-function, and i don't completely understand the logics in the
> pattern of the preg_match_all part.
>
> Could someone please explain a little bit more ?
>
> Frizzle.

Especially the "?P" - part in the pattern i cannot figure out ...

Frizzle.


The use of (?P<> ... ) indicates named capturing. It only affects how
the captured texts are referenced. Neither the P, the angle brackets,
or the text inside them are a part of the pattern. Thus

/(?P<name>[^,]+),(?P<url>[^,]+)/

is the exact same pattern as

/([^,]+),([^,]+)/

Compare the results from using the two and you'll see the difference.
Here a code snippet:

<?php

header('Content-Type: text/plain');
$list = "Google,http://www.google.com";
preg_match_all('/([^,]+),([^,]+)/', $list, $m1, PREG_SET_ORDER);
preg_match_all('/(?P<name>[^,]+),(?P<url>[^,]+)/', $list, $m2,
PREG_SET_ORDER);
var_dump($m1);
var_dump($m2);

?>

Look in the manual for how preg_match_all() works.


Ok, thanks, this named capturing is new to me, but looks kind of ideal!
Thanks a lot guys!

I have one little question left, not really essential, but to clean
things up:
can the preg-match-all be adjusted that output would be

[0] => Array
(
[name] => Google
[url] => http://www.google.com
)

instead of (current):

[0] => Array
(
[0] => Google,http://www.google.com
[name] => Google
[1] => Google
[url] => http://www.google.com
[2] => http://www.google.com
)

per name/url -set ?

Frizzle.


No, unfortunately. I wish an option like that exists.

Jun 23 '06 #12

P: n/a

Chung Leong wrote:
frizzle wrote:
Chung Leong wrote:
frizzle wrote:
> frizzle wrote:
> > Andy Hassall wrote:
> > > On 22 Jun 2006 13:21:50 -0700, "Chung Leong" <ch***********@hotmail.com> wrote:
> > >
> > > >Andy Hassall wrote:
> > > >>
> > > >> There are undoubtably faster, more concise and cleverer ways of doing this.
> > > >
> > > >Which would involve, of course, regular expression :-)
> > > >
> > > >preg_match_all('/(?P<name>[^,]+),(?P<url>[^,]+)/', $list, $m,
> > > >PREG_SET_ORDER);
> > >
> > > Ah ha, was trying to work out how to get it with PCRE, knew it was possible
> > > :-)
> > >
> > > --
> > > Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
> > > http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
> >
> > I'm sorry, but i don't completely understand the logics in the
> > Disny-function, and i don't completely understand the logics in the
> > pattern of the preg_match_all part.
> >
> > Could someone please explain a little bit more ?
> >
> > Frizzle.
>
> Especially the "?P" - part in the pattern i cannot figure out ...
>
> Frizzle.

The use of (?P<> ... ) indicates named capturing. It only affects how
the captured texts are referenced. Neither the P, the angle brackets,
or the text inside them are a part of the pattern. Thus

/(?P<name>[^,]+),(?P<url>[^,]+)/

is the exact same pattern as

/([^,]+),([^,]+)/

Compare the results from using the two and you'll see the difference.
Here a code snippet:

<?php

header('Content-Type: text/plain');
$list = "Google,http://www.google.com";
preg_match_all('/([^,]+),([^,]+)/', $list, $m1, PREG_SET_ORDER);
preg_match_all('/(?P<name>[^,]+),(?P<url>[^,]+)/', $list, $m2,
PREG_SET_ORDER);
var_dump($m1);
var_dump($m2);

?>

Look in the manual for how preg_match_all() works.


Ok, thanks, this named capturing is new to me, but looks kind of ideal!
Thanks a lot guys!

I have one little question left, not really essential, but to clean
things up:
can the preg-match-all be adjusted that output would be

[0] => Array
(
[name] => Google
[url] => http://www.google.com
)

instead of (current):

[0] => Array
(
[0] => Google,http://www.google.com
[name] => Google
[1] => Google
[url] => http://www.google.com
[2] => http://www.google.com
)

per name/url -set ?

Frizzle.


No, unfortunately. I wish an option like that exists.


No problem, i reached my goal with all of your help, and things are
fixed properly.
Thanks again!

Frizzle.

Jun 23 '06 #13

P: n/a
frizzle wrote:
[snip]
I'm sorry, but i don't completely understand the logics in the
Disny-function, and i don't completely understand the logics in the
pattern of the preg_match_all part.

Could someone please explain a little bit more ?


Don't bother with the Disney-thing. Allthough it works, it's wasn't meant to
be useful. Just me being silly.

--
/Bent
Jun 24 '06 #14

P: n/a
Bent Stigsen wrote:
frizzle wrote:
[snip]
I'm sorry, but i don't completely understand the logics in the
Disny-function, and i don't completely understand the logics in the
pattern of the preg_match_all part.

Could someone please explain a little bit more ?


Don't bother with the Disney-thing. Allthough it works, it's wasn't meant to
be useful. Just me being silly.

--
/Bent


Silly you. ;)

Jun 24 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.