2

Here is my codes:

$sc = 'hello 8491241 some text 6254841 some text 568241 414844:412';
preg_match_all('/[0-9]{5,10}/', $sc, $matches1);
preg_match_all('/[0-9]{5,10}:[0-9]{1,5}/', $sc, $matches2);

function cub1($match)
{

    return array(
      'batch' => $match,
      'type' => '1',
    );

}

function cub2($match)
{
    return array(
      'batch' => $match,
      'type' => '2',
    );
}

$pr_matches1 = array_map('cub1', $matches1[0]);
$pr_matches2 = array_map('cub2', $matches2[0]);

$all_matches = array_merge($pr_matches1,$pr_matches2);

It just works fine, i'm asking about if it possible to improve my codes and make the array_map callback functions (cub1 and cub2) as one function (cub), i just need to set different 'types' for $matches1 and $matches2

Any idea please?

1
  • This question would be clearer if we had the exact desired output as the final piece of the minimal reproducible example. Commented Sep 12, 2022 at 5:27

3 Answers 3

3

Yes it is possible, it's just a little bit tricky to identify in the function from which array it comes. But this should work for you:

(Here I just use strpos() to identify if it is a match form $matches1 or from $matches2, since only the second array can contain :)

<?php

    $sc = 'hello 8491241 some text 6254841 some text 568241 414844:412';
    preg_match_all('/[0-9]{5,10}/', $sc, $matches1);
    preg_match_all('/[0-9]{5,10}:[0-9]{1,5}/', $sc, $matches2);

    function cub($m) {

        if(strpos($m, ":") !== FALSE) {
            return array(
              'batch' => $m,
              'type' => '2',
            );
        } else {
            return array(
              'batch' => $m,
              'type' => '1',
            );
        }


    }

    $all_matches = array_map("cub", array_merge($matches1[0], $matches2[0]));
    print_r($all_matches);

?>

Output:

Array ( [0] => Array ( [batch] => 8491241 [type] => 1 ) [1] => Array ( [batch] => 6254841 [type] => 1 ) [2] => Array ( [batch] => 568241 [type] => 1 ) [3] => Array ( [batch] => 414844 [type] => 1 ) [4] => Array ( [batch] => 414844:412 [type] => 2 ) )
Sign up to request clarification or add additional context in comments.

3 Comments

Rizier, Super observation sir.
Rizier, thank you, this going to work for my special case, but outside of that, is there is any solution can work with all?
@user2203703 No you would just have to identify the matches so that you can tell from which array it comes
3

I'd simplify the whole thing to this:

$sc = 'hello 8491241 some text 6254841 some text 568241 414844:412';
preg_match_all('/([0-9]{5,10})(:[0-9]{1,5})?/', $sc, $matches, PREG_SET_ORDER);

$all_matches = array_reduce($matches, function (array $all, array $match) {
    $all[] = ['batch' => $match[1], 'type'  => '1'];
    if (isset($match[2])) {
        $all[] = ['batch' => $match[0], 'type' => '2'];
    }
    return $all;
}, []);

Use an optional capture group instead of two separate regexen, then distinguishing between both types of results becomes as simple as checking for the existence of the capture group.

1 Comment

Code of the question produces 5 elements.
1

Some functional stuff

$sc = 'hello 8491241 some text 6254841 some text 568241 414844:412';
preg_match_all('/[0-9]{5,10}/', $sc, $matches1);
preg_match_all('/[0-9]{5,10}:[0-9]{1,5}/', $sc, $matches2);

$my_matches[1] = $matches1[0];
$my_matches[2] = $matches2[0];

$cub[1] = function($match)
    {
    return array(
        'batch' => $match,
        'type' => '1',
    );
    };

$cub[2] = function($match)
    {
    return array(
        'batch' => $match,
        'type' => '2',
    );
    };

$result = call_user_func_array('array_merge', array_map(function($a, $b)
        { return array_map($a, $b); }, $cub, $my_matches));
var_dump($result);

Demo

So, you need two arrays of arbitrary (but the same) length: array of values, array of callbacks.

Comments

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.