3

I have following array uidValues. I want to create final array based on uid. Will concatenate values if uid is same in loop.

Array ( 
[0] => Array ( 
    [id] => 1 
    [pid] => 121 
    [uid] => 1  
    )
[1] => Array ( 
    [id] => 2 
    [pid] => 13
    [uid] => 1
    )
[2] => Array ( 
    [id] => 5 
    [pid] => 121 
    [uid] => 1
    )
) 

i want to create final array like..

 Array ( 
[0] => Array ( 
    [id] => 1,2,5 
    [pid] => 121,13,121 
    [uid] => 1  
    ) 
) 

how i can make it using foreach loop?

I tried following code..

foreach($uidValues as $r)
    {

        if(in_array($r['uid'],$finalArray))
        {   

            $finalArray['id'] .= ','.$r['id'];                                                                               
            $finalArray['pid'] .= $r['pid'];          
            $finalArray['uid'] = ','.$r['uid'];         

        }
        else{

            $finalArray[] = array("id" => $r['id'], "pid" => $r['pid'], "uid" => $r['uid']);

        }               

    }
2
  • 2
    Use the uid as key for your new array. BTW have you tried anything? Commented Aug 21, 2015 at 12:03
  • @b0s3 yes. updated question Commented Aug 21, 2015 at 12:13

3 Answers 3

4

Try simply as

$result = [];
foreach ($arr as $key => $value) {
    $hash = $value['uid'];
    if (isset($result[$hash])) {
        $result[$hash]['id'] = $result[$hash]['id'] . ',' . $value['id'];
        $result[$hash]['pid'] = $result[$hash]['pid'] . ',' . $value['pid'];
        $result[$hash]['uid'] = $result[$hash]['uid'] . ',' . $value['uid'];
    } else {
        $result[$hash]['id'] = $value['id'];
        $result[$hash]['pid'] = $value['pid'];
        $result[$hash]['uid'] = $value['uid'];
    }
}

print_r(array_values($result));

Demo

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

1 Comment

Depending on the uid.
2
$before = array(
    '0' => array(
        'id'  => 1,
        'pid' => 121,
        'uid' => 1,
    ),
    '1' => array(
        'id'  => 2,
        'pid' => 13,
        'uid' => 1,
    ),
    '2' => array(
        'id'  => 5,
        'pid' => 121,
        'uid' => 1,
    ),
);

$after = array();
foreach ($before as $row) {
    if (array_key_exists($row['uid'], $after)) {
        $after[$row['uid']]['id'] .= ',';
        $after[$row['uid']]['pid'] .= ',';
    } else {
        $after[$row['uid']]['uid'] = $row['uid'];
    }

    $after[$row['uid']]['id'] .= $row['id'];
    $after[$row['uid']]['pid'] .= $row['pid'];
}

You can then drop array keys to get exact same array:

$after = array_values($after);

Output:

array (
  0 => 
  array (
    'uid' => 1,
    'id' => '1,2,5',
    'pid' => '121,13,121',
  ),
)

1 Comment

Thanks for your answer. It helps me.
-1

As @b0s3 said in the comments: use uid as key for the new array.

foreach($uidValues as $item) {

        $result[$item['uid']]['pid'][]  = $item['pid'];
        $result[$item['uid']]['id'][]   = $item['id'];

}

Output:

Array
(
    [1] => Array
        (
            [pid] => Array
                (
                    [0] => 121
                    [1] => 13
                    [2] => 121
                )

            [id] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 5
                )

        )

)

1 Comment

my desire output array is different

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.