Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8117539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:12:16+00:00 2026-06-06T04:12:16+00:00

I am looking for something similar to array_chunk() but using an empty value as

  • 0

I am looking for something similar to array_chunk() but using an empty value as the separator.

I have:

Array
(
    [0] => /some/path:
    [1] => file.csv
    [2] => file.dat
    [3] => 
    [4] => /some/other/path:
    [5] => file.csv
    [6] => file.csv.gz
    [7] => file.dat
    [8] => 
    [9] => /some/other/other/path:
    [10] => file.csv
    [11] => file.dat
)

And would like to achieve –

Array
(
    [0] => Array
        (
            [0] => /some/path:
            [1] => file.csv
            [2] => file.dat
        )

    [1] => Array
        (
            [0] => /some/other/path:
            [1] => file.csv
            [2] => file.csv.gz
            [3] => file.dat
        )

    [2] => Array
        (
            [0] => /some/other/other/path:
            [1] => file.csv
            [2] => file.dat
        )
)

Note, I can’t just chunk on every 3 as you can see some locations will have more than 1 file.

I can achieve via loop and counter, but I am hoping there is a cleaner method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:12:19+00:00Added an answer on June 6, 2026 at 4:12 am

    Best way I can think of is this loop which I think is pretty clean:

    /**
     * @param array  $inputArr  The array you wish to split.
     * @param string $splitStr  The string you wish to split by.
     *
     * @return array
     */
    function split_arrays($inputArr, $splitStr) {
        $outputArr = array();
        $i         = 0;
        foreach ($inputArr as $data) {
            if ($data == $splitStr) {
                $i++;
                continue;
            }
            $outputArr[$i][] = $data;
        }
        return $outputArr;
    }
    

    The other way I thought of also uses loops but is not as clean. It searches for the index of the next split string and breaks off that chunk.

    /**
     * @param array  $inputArr  The array you wish to split.
     * @param string $splitStr  The string you wish to split by.
     *
     * @return array
     */
    function split_arrays2($inputArr,$splitStr) {
        $outputArr = array(); $i=0;
        while(count($inputArr)) {
            $index = array_search($splitStr,$inputArr)
            if($index === false) $index = count($inputArr);
            $outputArr[$i++] = array_slice($inputArr,0,$index);
            $inputArr = array_slice($inputArr,$index+1);
        }
        return $outputArr;
    }
    

    If and only if you are using a string for the split string which is not ever going to show up inside the middle of the other strings (space may or may not be the case for this) then I agree with the others that implode and explode are much simpler. In your case:

    /**
     * @param array  $inputArr  The array you wish to split.
     * @param string $splitStr  The string you wish to split by.
     *
     * @return array
     */
    function split_arrays3($inputArr,$splitStr) {
        $outputArr = array(); $i=0;
        $str = implode("|",$inputArr);
        $arr = explode("|$splitStr|");
        foreach($arr as $i=>$string)
            $outputArr[$i] = explode("|",$string);
        return $outputArr;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for something similar to xerces for parsing an xml file in ruby. I
I'm looking for something similar to the table creation form from phpmyadmin, but it
I've done something similar with str_replace using this: $string = $url; $patterns = array();
I have an array I'm using as a stack to store a path through
I want something similar to Mysql's sorting of data, but in jquery. I have
I'm looking for something similar to this: Active navigation with jQuery - can't apply
I'm looking for something similar to facebook suggestion list with cities and countries. You
i'm looking into implementing something similar to the li's in chrome extension page .
I'm really looking for something very similar to the way SO is setup where
I am looking for a way to use a UILabel (or something similar) to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.