2

Eg: I have an Following array
$array = array("abc","def","ghi", "jkl", "mno", "abc", "pqr", "stu", "vwy", "abc");

And i need to split this array for every occurrences of a string called "abc" I have tried array_chunk not helped for me..

4
  • 2
    What would be the result? Why not use foreach? Commented Sep 2, 2013 at 5:04
  • What do you mean with "split the array" It's not a string, so do you want to turn one array into two or more arrays? Please update your post to show what you expect the code to do. Commented Sep 2, 2013 at 5:07
  • php.net/manual/en/function.array-count-values.php ? Commented Sep 2, 2013 at 5:15
  • This question does not have a minimal reproducible example. What is the exact desired output? Commented May 20, 2022 at 6:26

3 Answers 3

6

A simple way using foreach. Just going through all element in an array and split it on the condition.

<?PHP
$array = array("abc","def","ghi", "jkl", "mno", "abc", "pqr", "stu", "vwy", "abc");
$acc_arra = array();
$i=0;
foreach($array as $occurrences)
{
    if($occurrences=='abc') //checking occurance
    {
        $i++;
    }
    else
    {
        if(!$acc_arra) //To start array from 0(if first element is 'abc')
        {
            $i=0;
        }
        $acc_arra[$i][]=$occurrences; //add to array
    }

}
print_r($acc_arra);

OUTPUT

Array
(
    [0] => Array
        (
            [0] => def
            [1] => ghi
            [2] => jkl
            [3] => mno
        )

    [1] => Array
        (
            [0] => pqr
            [1] => stu
            [2] => vwy
        )

)
Sign up to request clarification or add additional context in comments.

5 Comments

Your top-level array elements should start at 0, not 1. Otherwise, looks good to me.
Why not just initialize $i to -1 instead of adding a special case?
then the array will start from -1, if first element not 'abc'
You're right. I was thinking that there would always be "abc" at the beginning of the array, but we don't know if that's guaranteed.
@Barmar can you check out my answer and tell me what you think? please and thank you.
1

There no direct function in php to split array using values, so please use foreach loop for splitting the array. Please find a sample function to split array using values.

<?php
$array = array("abc","def","ghi", "jkl", "mno", "abc", "pqr", "stu", "vwy", "abc");


function splitArray($array, $value = '') {
    $returnArray = array();
    $inc = 0;
    if(is_array($array)) {
        foreach($array as $arrayValue) {
            if($value == $arrayValue) {
                $inc++;
            }
            $returnArray[$inc][] = $arrayValue;
        }
    }
    return $returnArray;
}
$finalArray = splitArray($array, 'abc');

print_r($finalArray);
?>

Comments

1

Here is a way to do it by first getting the array elements into a string. Then you can split it up using a regex.

Simple Way

<?php
$array = array("abc","def","ghi", "jkl", "mno", "abc", "pqr", "stu", "vwy", "abc");

$string = implode(' ', $array);

$a = preg_split("/[abc]+/", $string);


print_r($a);


Array
(
    [0] =>
    [1] =>  def ghi jkl mno
    [2] =>  pqr stu vwy
    [3] =>
)

Complex Way

<?php
$array = array("abc","def","ghi", "jkl", "mno", "abc", "pqr", "stu", "vwy", "abc");

$string = implode(' ', $array);

$a = preg_split("/[abc]+/", $string);

$b = array();

$d = array();

for($i = 0; $i < count($a); $i++) {
    if($a[$i] == NULL) continue;
    $b[] = $a[$i];
}
for($i = 0; $i < count($b); $i++) {

    //echo $b[$i] . PHP_EOL;

    $c = explode(' ', $b[$i]);

    for($ii = 0; $ii < count($c); $ii++) {

        if($c[$ii] == NULL) continue;
        $d[$i][] = $c[$ii];

    }
}

print_r($d);

output

Array
(
    [0] => Array
        (
            [0] => def
            [1] => ghi
            [2] => jkl
            [3] => mno
        )

    [1] => Array
        (
            [0] => pqr
            [1] => stu
            [2] => vwy
        )

)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.