2

I just pasted my sample input & output.

Sample Input:

Array
(
    [0] => Array
        (
            [id] => 1
            [msisdn] => 10
            [sc] => 8155
        )

    [1] => Array
        (
            [id] => 2
            [msisdn] => 20
            [sc] => 22020
        )

    [2] => Array
        (
            [id] => 3
            [msisdn] => 10
            [sc] => 8155
        )

    [3] => Array
        (
            [id] => 4
            [msisdn] => 10
            [sc] => 8155
        )

    [4] => Array
        (
            [id] => 5
            [msisdn] => 20
            [sc] => 22020
        )

    [5] => Array
        (
            [id] => 6
            [msisdn] => 30
            [sc] => 22020
        )

)

Sample Output:

Array
(
    [0] => Array
        (
            [id] => 1,3,4
            [msisdn] => 10
            [sc] => 8155
        )

    [1] => Array
        (
            [id] => 2,5
            [msisdn] => 20
            [sc] => 22020
        )

    [2] => Array
        (
            [id] => 6
            [msisdn] => 30
            [sc] => 8155
        )

)
2
  • @CodingAnt sometimes people just have no idea on what to do. Give him a break. Commented Nov 10, 2014 at 13:43
  • @reformed Mate I just wanted to know at what level he is stucked . Anyways thanks for the enlightment Commented Nov 10, 2014 at 13:45

2 Answers 2

1

Just make that particular value that key, then just concatenate when already pushed/exists:

$new_array = array();
foreach ($array as $value) {
    if(!isset($new_array[$value['msisdn']])) {
        // if not yet pushed, just initialize
        $new_array[$value['msisdn']] = $value;
    } else {
        // if already inside, then just concatenate
        $new_array[$value['msisdn']]['id'] .= ', ' . $value['id'];
    }
}

$new_array = array_values($new_array);
echo '<pre>';
print_r($new_array);

Sample Output

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

15 Comments

you should use array_key_exists instead of isset in this case
if [msisdn] value is string instead of numeric value, then what will be the condition? @Ghost
@Rezuan condition? sorry i don't follow, this will group those values that share the same msidsn. as long as they have the same key, the ids will be concatenated.
Let, there is 5 id's has the same msisdn value. Can I dynamically define the number of id value to split the that array with 2 or 3 id value? For more details please check this link to get the idea of sample input and output [txt.do/o2i0] sorry for my poor English. @Ghost
@Rezuan yes you could do that also, you could chunk them by three's so that they will be split by 3, then 2. but of course you'll put them in another process to do that. could you explain the big picture here? i think there something more to it that you haven't explained. anyways, the link you gave is not accessible
|
0

Live on codepad: http://codepad.org/0fw9k2w9

You can use the fact that in PHP array is an hash map. By creating an intermediate array you can solve this in O(n) time.

$merged = array();

foreach($array as $v) {
    $merged[$v['msisdn']][$v['sc']] [] = $v['id'];
}


$final = array();
foreach($merged as $msisdn=>$v) {
   foreach($v as $sc=>$ids) {
       $final [] = array('msisdn'=>$msisdn,'sc'=>$sc,'id'=>$ids);
   }
}

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.