0

I'm experimenting with PHP and I was wondering if there is a simple way to accomplish this:

I have 2 arrays:

$array1 = ('A', 'B', '$', 'C', 'D', 'E', '%', 'F', 'G', 'H', 'I', '&', 'J')
$array2  =('$', '%', '&')

How can I get 4 separate arrays that are between the delimiters defined in array2? In other words, I should get 4 arrays:

('A', 'B')
('C', 'D', 'E')
('F', 'G', 'H', 'I')
('J')
3
  • br.php.net/array_slice will help you... Commented Sep 9, 2013 at 19:55
  • 3
    I don't think only array_slice will do the trick. Commented Sep 9, 2013 at 19:58
  • 1
    implode to a string; preg_split() on your set of delimiters, then str_split() on each result Commented Sep 9, 2013 at 20:00

5 Answers 5

3

How about this?

<?php

/**
 * @author - Sephedo
 * @for - ImTryinl @ Stackoverflow
 * @question - http://stackoverflow.com/questions/18705886/splitting-an-array-based-on-delimiters-from-another-array 
 */

$array1 = array('A', 'B', '$', 'C', 'D', 'E', '%', 'F', 'G', 'H', 'I', '&', 'J');
$array2 = array('$', '%', '&');

$return = array();
$x = 0;

foreach( $array1 as $value )
{
    if( in_array( $value, $array2 ) )
    {
        $x++;
    }
    else
    {
        $return[$x][] = $value;
    }
}

var_dump( $return );

?>

Tested and this returns

array
  0 => 
    array
      0 => string 'A' (length=1)
      1 => string 'B' (length=1)
  1 => 
    array
      0 => string 'C' (length=1)
      1 => string 'D' (length=1)
      2 => string 'E' (length=1)
  2 => 
    array
      0 => string 'F' (length=1)
      1 => string 'G' (length=1)
      2 => string 'H' (length=1)
      3 => string 'I' (length=1)
  3 => 
    array
      0 => string 'J' (length=1)
Sign up to request clarification or add additional context in comments.

1 Comment

Simple and efficient.
1
$array1 = array('A', 'B', '$', 'C', 'D', 'E', '%', 'F', 'G', 'H', 'I', '&', 'J');
$array2 = array('$', '%', '&');

$result = array_map(
    'str_split',
    preg_split(
        '/[' . preg_quote(implode('', $array2)) . ']/', 
        implode('', $array1)
    )
);

var_dump($result);

Comments

0

This should work:

function splitArrays($source, $delimiters) {
    $result = array();
    $result[] = array();
    $resultIndex = 0;
    foreach($source as $value) {
        if(in_array($value, $delimiters)) {
            $result[] = array();
            $resultIndex++;
        }
        else {
            $result[$resultIndex][] = $value;
        }
    }
}

Comments

0

answer here: http://codepad.org/dieF4Mbe

$array1 = array('A', 'B', '$', 'C', 'D', 'E', '%', 'F', 'G', 'H', 'I', '&', 'J');
$array2  = array('$', '%', '&');
$final_arrays = array();
$curr_array = 0;

foreach ($array1 as $char) {



    if (!in_array($char, $array2)) {

        $final_arrays[$curr_array][] = $char;

    } else {

        $curr_array++;
    }
}

print_r($final_arrays);

Comments

0
$array1 = array('A', 'B', '$', 'C', 'D', 'E', '%', 'F', 'G', 'H', 'I', '&', 'J');
$array2  =array('$', '%', '&');
$results = array();

$start = 0;
foreach($array2 as $key => $val){
 $results[] = array_slice($array1, $start, array_search($val, $array1)-$start);
 $start = array_search($val, $array1)+1;
}
print_r($results);

Output:

Array
(
    [0] => Array
        (
            [0] => A
            [1] => B
        )

    [1] => Array
        (
            [0] => C
            [1] => D
            [2] => E
        )

    [2] => Array
        (
            [0] => F
            [1] => G
            [2] => H
            [3] => I
        )

)

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.