1

I have a string: a74b8c0d3845e7… etc etc.

The basic pattern is a letter followed by a number.

I want to create an array that would result in:

Array (
[a] => 74
[b] => 8
[c] => 0
[d] => 3845
[e] => 7
)

I know it'll be a foreach loop (or a couple) but just can't seem to figure it out.

2
  • I think you need to split the string into individual characters and compile if it manually.. Commented May 8, 2014 at 2:52
  • Lol looks like everyone is fighting for that checkmark. You've got several working algorithms here ... now you have to chose your favorite :D Commented May 8, 2014 at 3:21

6 Answers 6

1
$str = "a74b8c0d3845e7";
$letters = preg_split("/[0-9]+/",$str);
$nums = preg_split("/[a-z]+/", $str);
array_shift($nums);
array_pop($letters);
print_r(array_combine($letters,$nums));

prints out

Array
(
    [a] => 74
    [b] => 8
    [c] => 0
    [d] => 3845
    [e] => 7
)
Sign up to request clarification or add additional context in comments.

1 Comment

I try this and actually nothing prints out.
0

Instead of preg_split, you can use preg_match_all to get all the letters and corresponding numbers.

preg_match_all('/([a-z])(\d+)/', $string, $match);
$result = array();
foreach ($match[1] as $i => $letter) {
  $result[$letter] = $match[2][$i];
}

1 Comment

Yeah, I gotta give the green check to this one. Thanks to everyone for such quick responses!
0

You can look into using str_split which would split your string into an array that can be iterated over. Here is an example I just threw together:

// Set the string value.
$string = 'a74b8c0d3845e7';

// Create the string array using `str_split`.
$string_array = str_split($string);

// Init `$final_array`.
$final_array = array();

// Roll throgh the `$string_array` using `foreach`.
foreach ($string_array as $string_key => $string_value) {
  // If the value `is_numeric` assign it as `$final_value`.
  // Else set the `$final_key` & reset the `$final_value`.
  if (is_numeric($string_value)) {
    $final_value = $string_value;
  }
  else {
    $final_key = $string_value;
    $final_value = '';
  }
  // Set the value in the `$final_array`.
  $final_array[$final_key] .= $final_value;
}

// Dump the `$final_array` to show the concept works.
echo '<pre>';
print_r($final_array);
echo '</pre>';

The output is:

Array
(
    [a] => 74
    [b] => 8
    [c] => 0
    [d] => 3845
    [e] => 7
)

2 Comments

str_split splits it into equal-length blocks. How does that help here?
You're just iterating over the characters in the string. You don't need to use str_split for that, just a normal for loop.
0

This should work for you. How it works is it makes 2 arrays. The first array is split by letters, making an array of numbers. Then the next array is split by numbers, making an array of letters. Finally the two arrays are combined into a new array with the letter as its key and a number as its value! Pretty neat...

    $string = 'a74b8c0d3845e7';

    $array1 = preg_split("/[a-zA-Z]+/", $string);
    $array2 = preg_split("/[0-9]+/", $string);

    for($i = 1; $i <= count($array1); $i++) {
        if(isset($array1[$i])){
            $newarray[$array2[$i-1]] = $array1[$i];
        }
    }

    var_dump($newarray);

Comments

0

This should do the trick:

$str = 'a74b8c0d3845e7';

$arr = preg_split('/[a-zA-Z]+/', $str, -1, PREG_SPLIT_NO_EMPTY);
$no_num = preg_replace('/[0-9]/', '', $str);

$new_arr = array();

foreach ($arr as $i=>$a)
    $new_arr[$no_num[$i]] = $a;

print_r($new_arr);

Result:

Array
(
    [a] => 74
    [b] => 8
    [c] => 0
    [d] => 3845
    [e] => 7
)

Comments

0
<?php
$a = "a74b8c0d3845e7";
preg_match_all("/[\d]+/", $a, $numbers);
preg_match_all("/[a-z]+/i", $a, $strings);
$c=array_combine($strings[0],$numbers[0]);
print_r($c);

Check it here.

4 Comments

I try this and actually nothing prints out.
its working check it out here Hopefully You are using PHP v5+
The error will occur when the string ends with an char because the length of each array will differ
I had a trailing "Z" in my real code, hence the mistake on my end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.