Kintamasis
Kintamasis

Reputation: 263

Split a flat array into a 2d array with chunked rows when a delimiting element is encountered

I have array with ~ 10000 values.

Example:

  $arr = array("Name", "1.00", "87.70", "12.30",     
  "3.30", "3.30", "0.00", "3.50", "2.10", "1.11", "0.10", "14.00",
  "4.80", "0.00", "4.80", "0.00", "0.70", "44.00", "12.00", "85.00", 
  "138.00", "118.00", "0.10", "0.32", "1.00", "9.00", "40.00", "0.05", 
  "0.11", "0.04", "0.17", "0.10", "0.70", "5.00", "0.05", "0.40", 
  "1.00", "0.00", "65.00", "^_^", "Name2", "1.00", 
  "88.00", "12.00", "3.30", "3.30", "0.00", "3.20", "1.92", "1.01", 
  "0.08", "13.00", "4.80", "0.00", "4.80", "0.00", "0.70", "44.00", 
  "12.00", "85.00", "139.00", "118.00", "0.10", "0.32", "1.00", "9.00", 
  "36.00", "0.00", "0.10", "0.04", "0.17", "0.10", "0.70", "7.00", 
  "0.05", "0.40", "1.00", "0.00", "61.00", "^_^",

and so on....

I need to explode array when it reach value in array "^_^" and make array in array. And then I will and info into database.

I tried to explode, foreach and explode but didn't find a solution for this problem.

Upvotes: -1

Views: 145

Answers (3)

mickmackusa
mickmackusa

Reputation: 47903

Push grouped data into the resultarray via reference variables.

Code: (Demo)

$result = [];
foreach ($arr as $v) {
    if ($v === '^_^') {
        unset($ref);
        continue;
    }
    if (!isset($ref)) {
        $result[] =& $ref;
    }
    $ref[] = $v;
}
var_export($result);

Upvotes: 0

fusion3k
fusion3k

Reputation: 11689

$result = array_map
(
    function( $row )
    {
        return explode( ',', $row );
    },
    explode( ',^_^,', implode( ',',$arr ) )
);

print_r( $result );

output:

Array
(
    [0] => Array
        (
            [0] => Name
            [1] => 1.00
            (...)
        )
    [1] => Array
        (
            [0] => Name2
            [1] => 1.00
            (...)
        )
    (...)
)

Above code will works if the last element of original array is not ^_^. Otherwise you have to modify the script in this way:

explode( ',^_^,', implode( ',', array_slice( $arr, 0, -1 ) ) )
#                               ↑↑↑↑↑↑↑↑↑↑↑↑     ↑↑↑↑↑↑↑↑↑

You pass to array_map an array obtained by imploding original array with ',' and exploding resulting string by ',^_^,', then array_map returns each string exploded by ','.


Upvotes: 0

Kuba Birecki
Kuba Birecki

Reputation: 3016

You will need to write your own function to do this. Here is a simple example that should explain it:

function array_split($arr, $splitBy) {
    $result = [];
    $subArray = [];

    foreach ($arr as $elem) {
        // If an element matches your delimiter,
        // add the current sub-array to the result set and start a new sub-array
        // Else add the element to the sub-array
        if ($elem == $splitBy) {
            $result[] = $subArray;
            $subArray = [];
        } else {
            $subArray[] = $elem;
        }
    }

    // Make sure the last sub-array also gets added to the result set
    $result[] = $subArray;

    return $result;
}

Upvotes: 1

Related Questions