Skip to main content
added 258 characters in body
Source Link
mickmackusa
  • 49.2k
  • 13
  • 98
  • 165

The professional advice is to not do what you are doing. You should not be storing comma-separated values in table columns. These should be in their own table and JOINed when needed.

Anyhow, you can unconditionally explode and push the generated array into the perpetually flat result array using the spread operator (...).

Code: (Demo)

$array = [
    "10003",
    "10016",
    "10008, 10007, 10010",
];

$result = [];
foreach ($downloads_query as $row) {
    array_push($result, ...explode(', ', $row['product']));
}
var_export($result);

Output:

array (
  0 => '10003',
  1 => '10016',
  2 => '10008',
  3 => '10007',
  4 => '10010',
)

Alternatively, just join the strings with the same delimiting substring as the delimited values, then explode on that same delimiter. (Demo)

var_export(
    explode(', ', implode(', ', $array))
);

The professional advice is to not do what you are doing. You should not be storing comma-separated values in table columns. These should be in their own table and JOINed when needed.

Anyhow, you can unconditionally explode and push the generated array into the perpetually flat result array using the spread operator (...).

Code: (Demo)

$array = [
    "10003",
    "10016",
    "10008, 10007, 10010",
];

$result = [];
foreach ($downloads_query as $row) {
    array_push($result, ...explode(', ', $row['product']));
}
var_export($result);

Output:

array (
  0 => '10003',
  1 => '10016',
  2 => '10008',
  3 => '10007',
  4 => '10010',
)

The professional advice is to not do what you are doing. You should not be storing comma-separated values in table columns. These should be in their own table and JOINed when needed.

Anyhow, you can unconditionally explode and push the generated array into the perpetually flat result array using the spread operator (...).

Code: (Demo)

$array = [
    "10003",
    "10016",
    "10008, 10007, 10010",
];

$result = [];
foreach ($downloads_query as $row) {
    array_push($result, ...explode(', ', $row['product']));
}
var_export($result);

Output:

array (
  0 => '10003',
  1 => '10016',
  2 => '10008',
  3 => '10007',
  4 => '10010',
)

Alternatively, just join the strings with the same delimiting substring as the delimited values, then explode on that same delimiter. (Demo)

var_export(
    explode(', ', implode(', ', $array))
);
Source Link
mickmackusa
  • 49.2k
  • 13
  • 98
  • 165

The professional advice is to not do what you are doing. You should not be storing comma-separated values in table columns. These should be in their own table and JOINed when needed.

Anyhow, you can unconditionally explode and push the generated array into the perpetually flat result array using the spread operator (...).

Code: (Demo)

$array = [
    "10003",
    "10016",
    "10008, 10007, 10010",
];

$result = [];
foreach ($downloads_query as $row) {
    array_push($result, ...explode(', ', $row['product']));
}
var_export($result);

Output:

array (
  0 => '10003',
  1 => '10016',
  2 => '10008',
  3 => '10007',
  4 => '10010',
)