0

I am having an issue splitting a multidimensional array into sub-arrays based on repeating values. Here is the issue-

Current array:

Array (
    [2] => first=1
    [3] => second=2
    [4] => third=
    [5] => first=4
    [9] => second=3
    ...
);

Intended result:

Array(
    [0] =>
        Array(
            [first] => 1,
            [second] => 2,
            [third] => 
        ...
    ),
    [1] =>
        Array(
            [first] => 4,
            [second] => 3
        ...
    ),
);

Trying to achieve by manipulating it like this but stuck this far-

$filtered_array_splitted = [];
$array_tracker = 0;
$array_tracker_in = 0;
foreach ($filtered_array as $key => $value) {

    parse_str(str_replace('', '=', $value), $mapped_value_as_pair);

    $filtered_array_splitted[$array_tracker++] = array_merge($mapped_value_as_pair);
}

print_r($filtered_array_splitted);

This just split them into sub-arrays, I need to insert them based on duplication occurrence. Anytime same key is found, new sub-array will be formed.

0

2 Answers 2

1

This keeps a list of the current list of fields being added, then if it finds one that is already present (using isset()), then it stores the list and starts again.

At the end if there are any left over, it adds them into the final list...

$filtered_array_splitted = [];
$current_list = [];
foreach ($filtered_array as $string) {
    list($name, $value) = explode("=", $string);
    if ( isset($current_list[$name]) ){
        $filtered_array_splitted[] = $current_list;
        $current_list = [];
    }
    $current_list[$name] = $value;
}
if ( !empty($current_list)) {
    $filtered_array_splitted[] = $current_list;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Worked like a charm! Could you explain a bit line 6 and 7?
$filtered_array_splitted[] takes the list of items so far added and just appends it to the array, the next line just resets the array so all of the previous list of items are removed. (hope this is the right lines).
0

A reference variable is the perfect tool to keep the script concise, clean, and direct.

Every iteration of the loop must push a value. It is the destination of the pushed value that must be dynamic.

By "pre-pushing" the reference variable into the output array when:

  1. there is no reference variable yet or
  2. the current reference variable already has a specific key

you never need to mop up after looping.

Think of $entry as a basketball hoop -- you always shoot into the hoop. The "trick" is that you can move the hoop to wherever you like.

Code: (Demo)

$array = [
    2 => 'first=1',
    3 => 'second=2',
    4 => 'third=',
    5 => 'first=4',
    9 => 'second=3',
];

$result = [];
foreach ($array as $string) {
    [$key, $value] = explode('=', $string, 2);

    if (!isset($entry) || isset($entry[$key])) {
        unset($entry);
        $result[] = &$entry;
    }
    $entry[$key] = $value;
}
var_export($result);

Output:

array (
  0 => 
  array (
    'first' => '1',
    'second' => '2',
    'third' => '',
  ),
  1 => 
  array (
    'first' => '4',
    'second' => '3',
  ),
)

p.s. the unset() call is not necessary before $entry is declared the first time, but it must be called before each subsequent push so that the reference variable is "reset" so-to-speak.

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.