1

I am working within a foreach loop and PARTS my code looks like this:

foreach ($query->rows as $row) {

  $myarray = explode(",",$row['text']);
                
  print_r($myarray);

}

The Output result of the above is this:

Array
(
    [0] = Charcoal
    [1] = Natural Gas
    [2] = Combo
)
Array
(
    [0] = Charcoal
    [1] = Propane
    [2] = Combo
)
Array
(
    [0] = Charcoal
    [1] = Propane
    [2] = Natural Gas
    [3] = Combo
)
Array
(
    [0] = coal
)
Array
(
    [0] = Natural Gas
    [1] = Wood
)

Yes I see there are similar questions to this. But none of their answers seem to work for me. I'm thinking it might be because I am working inside an foreach loop. Either way, I was wondering if there was a way to get my output above to look like this:

Array
(
    [0] = Charcoal
    [1] = Natural Gas
    [2] = Combo
)
Array
(
    [0] = Propane
)
Array
(
    [0] = Coal
)
Array
(
    [0] = wood
)

All the duplicates gone, without loosing the formatting of this array. Code I have tried.. but "maybe" wrong was:

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));

EDIT for Sharanya Dutta:

I have alot of other code, but basically this is where Im trying to use it.

$arr = array();

foreach($query->rows as $row){
    $_arr = explode(",", $row["text"]);
    $diff = array_values(array_diff($_arr, $arr));
    if($diff !== array()) print_r($diff);
    $arr = array_merge($arr, $_arr);
    $output[$row['attribute_id']]['values'][] = $diff; // <--- USE IT HERE
}

2 Answers 2

2

Use an array ($arr in the following code) to store the values and print_r only those values which are different from the already stored values:

$arr = array();

foreach($query->rows as $row){
    $_arr = explode(",", $row["text"]);
    $diff = array_values(array_diff($_arr, $arr));
    if($diff !== array()) print_r($diff);
    $arr = array_merge($arr, $_arr);
}

DEMO


You may even use $diff after the last line in the foreach loop:

$arr = array();

foreach($query->rows as $row){
    $_arr = explode(",", $row["text"]);
    $diff = array_values(array_diff($_arr, $arr));
    $arr = array_merge($arr, $_arr);
    if($diff !== array()) print_r($diff);
}

DEMO

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

6 Comments

this seems to work very well, but how do I use the array below the last line but still within the foreach?
That depends on what you actually need to do with the array.
I just made an edit to my question, with code of what im trying to do.
I’ve seen the edit. It’s a piece of valid code that should work perfectly. What problems are you facing now?
whenever I use $diff anywhere below this line $arr = array_merge($arr, $_arr); I get a completely different output array.
|
0

As you iterate the result set rows and explode the text string, filter the individual values in the current row against all values in all previously encountered rows.

If there are any individual values encountered for the first time, then save the unique values of that row as a new row in the result array.

Code: (Demo)

$resultSet = [
    ['text' => 'Charcoal,Natural Gas,Combo'],
    ['text' => 'Charcoal,Propane,Combo'],
    ['text' => 'Charcoal,Propane,Natural Gas,Combo'],
    ['text' => 'coal'],
    ['text' => 'Natural Gas,wood'],
];

$result = [];
foreach ($resultSet as $row) {
    $clean = array_diff(
        explode(',', $row['text']),
        ...$result
    );
    if ($clean) {
        $result[] = array_values($clean);
    }
}
var_export($result);

Output:

array (
  0 => 
  array (
    0 => 'Charcoal',
    1 => 'Natural Gas',
    2 => 'Combo',
  ),
  1 => 
  array (
    0 => 'Propane',
  ),
  2 => 
  array (
    0 => 'coal',
  ),
  3 => 
  array (
    0 => 'wood',
  ),
)

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.