Shri
Shri

Reputation: 731

create array based on one unique value

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']);

        }               

    }

Upvotes: 3

Views: 88

Answers (3)

Jan.J
Jan.J

Reputation: 3080

$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',
  ),
)

Upvotes: 2

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

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

Upvotes: 4

Emil
Emil

Reputation: 1816

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
                )

        )

)

Upvotes: -1

Related Questions