Jump to content

[SOLVED] Array Key by alpha


xiaix

Recommended Posts

I have a string coming in from a database.  For example: a0b34c7d0e2333

 

What I would like to do is build an end result keyed array with that string, like this:

$dataArray = array("a" => "0", "b" => "34", "c" => "7", "d" => "0", "e" => "2333");

 

Since I don't have a valid delimiter for the explode function to work, I would like to know if I can use the letters (alpha) of the incoming string as a delimiter.

 

What coding hurdles would I need to convert my incoming string into that keyed array?  Is it even possible to do this?

 

I thank you for your time and review.

Link to comment
https://forums.phpfreaks.com/topic/170361-solved-array-key-by-alpha/
Share on other sites

Here's one way,

 

<?php
$str = "a0b34c7d0e2333";

preg_match_all('/([a-z])([\d]+)/', $str, $part);
$out = array_combine($part[1], $part[2]);
      
print_r($out);
?>

 

Output

Array
(
    [a] => 0
    [b] => 34
    [c] => 7
    [d] => 0
    [e] => 2333
)

 

Here's one way,

 

<?php
$str = "a0b34c7d0e2333";

preg_match_all('/([a-z])([\d]+)/', $str, $part);
$out = array_combine($part[1], $part[2]);
      
print_r($out);
?>

 

Output

Array
(
    [a] => 0
    [b] => 34
    [c] => 7
    [d] => 0
    [e] => 2333
)

 

You, my friend, are genius.  Worked wonderfully.  Thank you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.