2

I am in the situation where I want to group the arrays in PHP to group on the base of there group ID, What I am getting is the following

Array ( 

[0] => Array ( 
[raw] => I am text string 
[group_id] => 1 
) 

[1] => Array ( 
[raw] => Another text string 
[group_id] => 1 
) 

[2] => Array ( 
[raw] => text placeholder 
[group_id] => 2 
)

) 

but I want the output like below

Array ( 

[0] => Array ( 

Array(
[0] => Array (
[raw] => I am text string 
[group_id] => 1 
) 
[1] => Array ( 
[raw] => Another text string 
[group_id] => 1 
) 

)

[1] => Array ( 
[raw] => text placeholder 
[group_id] => 2 
)

) 

In the current output, You can notice that 'group_id' of the first two array elements are the same i.e. 1, and in the expected output the elements with the same 'group_id' are wrapped into a parent array.

for your consideration, here is the array code;

<?php    
$output = array(
array ("raw"=> "I am text string", "group_id" => 1), 
array ("raw"=> "Another text string", "group_id" => 1), 
array ("raw"=> "text placeholder", "group_id" => 2), 
);
print_r($output);
?>

So ultimately, I want to group the arrays with same 'group_id' into another array.

Thank you in advance for your time and help.

any help is appreciated as I am not getting the logic.

0

3 Answers 3

2

I don't think there is a shortcut for trimming nested dimensions for single/lone group id's but you could do something like this:

$output = array(
    array ("raw"=> "I am text string", "group_id" => 1),
    array ("raw"=> "Another text string", "group_id" => 1),
    array ("raw"=> "text placeholder", "group_id" => 2),
);

$new = array();
foreach ($output as $value) {
    $new[$value['group_id']][] = $value;
}
// trim single/lone arrays
foreach ($new as &$value) {
    if(count($value) <= 1) {
        $value = reset($value);
    }
}

echo '<pre>';
print_r($new);
Sign up to request clarification or add additional context in comments.

1 Comment

@Ghost This worked for me. Thanks!!!. Also I've modified it little bit. Coz your code does not work for 1 array element. So I changed "if(count($value) <= 1) {" to "if(count($value) <= 0) { ".
2
$newArr = array();
foreach($output as $values) {
    $new[$values['group_id']][] =  $values;
}

It will create the array for you.

Comments

0

Use the following code:

<?php    
$output = array(
array ("raw"=> "I am text string", "group_id" => 1), 
array ("raw"=> "Another text string", "group_id" => 1), 
array ("raw"=> "text placeholder", "group_id" => 2), 
);

$new = array();
foreach($output as $values) {
    $new[$values['group_id']][] =  $values;
}
$new = array_values($new);
echo '<pre>';
print_r($new);
?>

1 Comment

it's better logic but result is not as expected, as it's also wrapping the single 'group_id' into array too, I want this "array ("raw"=> "text placeholder", "group_id" => 2)," not to be wrapped into array, but only if there are similar group_ids

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.