Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 03:55 AM
M. Katz
Guest
 
Posts: n/a
Default string list to array

I'm trying to convert a slash-delimited string to an array, but I'm
wary of null elements that I want to eliminate. I'm frustrated with
explode(), array_unique(), and array_pop(), and I'm wondering if
there's a better way. I'm only interested in unique array elements.

Here's what I've got

$a = "10/20/30"
should become an array where
$b[0] = 10, $b[1] = 20, and $b[2] = 30.

Now here's the fun part. Often my arrays will have
$a = "10//"
I'd like that to become $b[0] = 10 and that's all.

So I tried applying array_unique() to explode("/", $a)
but you have to be careful about how many elements
are returned. Then I tried to use array_pop() to drop
the last element if it's empty, but array_pop returns
a scalar string when there's only one element left.
So when you use $b = array_pop($q), sometimes
there is no $b[0].

Does anyone have any good ideas?

Thanks,
M. Katz



  #2  
Old July 17th, 2005, 03:55 AM
Andy Hassall
Guest
 
Posts: n/a
Default Re: string list to array

On 3 Feb 2004 14:16:36 -0800, MKatz843@onebox.com (M. Katz) wrote:
[color=blue]
>I'm trying to convert a slash-delimited string to an array, but I'm
>wary of null elements that I want to eliminate.[/color]
[color=blue]
>I'm only interested in unique array elements.
>
>$a = "10/20/30"
> should become an array where
> $b[0] = 10, $b[1] = 20, and $b[2] = 30.
>
>Now here's the fun part. Often my arrays will have
>$a = "10//"
> I'd like that to become $b[0] = 10 and that's all.[/color]

<pre>
<?php
$a = "10/20/30//30";
$b = array_unique(array_filter(explode("/", $a), "strlen"));
var_dump($b);
?>
</pre>

array(3) {
[0]=>
string(2) "10"
[1]=>
string(2) "20"
[2]=>
string(2) "30"
}

--
Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
  #3  
Old July 17th, 2005, 03:55 AM
Pedro Graca
Guest
 
Posts: n/a
Default Re: string list to array

M. Katz wrote:[color=blue]
> $a = "10/20/30"
> should become an array where
> $b[0] = 10, $b[1] = 20, and $b[2] = 30.
>
> Now here's the fun part. Often my arrays will have
> $a = "10//"
> I'd like that to become $b[0] = 10 and that's all.[/color]
(snip)[color=blue]
> Does anyone have any good ideas?[/color]

Maybe something like this?

<?php
function explode_without_nulls($separator, $string) {
$retval = array();
$x = explode($separator, $string);
foreach ($x as $y) {
if (!empty($y)) $retval[]=$y;
}
return $z;
}

// usage example
print_r(explode_without_nulls("10///20///30/////////"));
?>


Output is

Array
(
[0] => 10
[1] => 20
[2] => 30
)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
  #4  
Old July 17th, 2005, 03:56 AM
Rahul Anand
Guest
 
Posts: n/a
Default Re: string list to array

Pedro Graca <hexkid@hotpop.com> wrote in message news:<bvp84f$v4geq$1@ID-203069.news.uni-berlin.de>...[color=blue]
> M. Katz wrote:[color=green]
> > $a = "10/20/30"
> > should become an array where
> > $b[0] = 10, $b[1] = 20, and $b[2] = 30.
> >
> > Now here's the fun part. Often my arrays will have
> > $a = "10//"
> > I'd like that to become $b[0] = 10 and that's all.[/color]
> (snip)[color=green]
> > Does anyone have any good ideas?[/color]
>
> Maybe something like this?
>
> <?php
> function explode_without_nulls($separator, $string) {
> $retval = array();
> $x = explode($separator, $string);
> foreach ($x as $y) {
> if (!empty($y)) $retval[]=$y;
> }
> return $z;
> }
>
> // usage example
> print_r(explode_without_nulls("10///20///30/////////"));
> ?>
>
>
> Output is
>
> Array
> (
> [0] => 10
> [1] => 20
> [2] => 30
> )[/color]

You can use *preg_split*:

[SNIP]

$str = '10//';
$myArray = preg_split("|/|",$str,-1,PREG_SPLIT_NO_EMPTY);
print_r($myArray);

[/SNIP]

--
Cheers,
Rahul Anand
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,414 network members.