Jump to content

Recommended Posts

Hi Guys:

 

I got a array:

 

$aaa  = 
array(
array('id'=>1,'name'=>aaa,'pld'=>1),
array('id'=>1,'name'=>aaa,'pld'=>2),
array('id'=>3,'name'=>ccc,'pld'=>1),
array('id'=>2,'name'=>bbb,'pld'=>1),
array('id'=>1,'name'=>aaa,'pld'=>3),
array('id'=>3,'name'=>ccc,'pld'=>2),
array('id'=>2,'name'=>bbb,'pld'=>2),
array('id'=>3,'name'=>ccc,'pld'=>3),
array('id'=>2,'name'=>bbb,'pld'=>3)
);

 

Finally, I would make it looks like:

 

$aaa  = 
array(
array('id'=>1,'name'=>aaa,'pld'=>6),
array('id'=>2,'name'=>bbb,'pld'=>6),
array('id'=>3,'name'=>ccc,'pld'=>6)
);

 

Here is my code :

 

foreach($aaa as $key => $row)
{
        $id[$key]  = $row['id'];
}
array_multisort($id,SORT_ASC,$aaa);
for($i=0;$i<=sizeof($aaa)-1;$i++)
{
if ($i == 0)
{ 	
	$final[$aaa[$i]['id']]['pld'] = $aaa[$i]['pld'];
}
elseif($aaa[$i]['id'] == $aaa[$i - 1]['id'])
{     
	$nom = $aaa[$i]['pld'] + $aaa[$i -1]['pld'];
	$final[$aaa[$i - 1]['id']]['id'] = $aaa[$i]['id'];
	$final[$aaa[$i - 1]['id']]['name'] = $aaa[$i]['name'];
	$final[$aaa[$i - 1]['id']]['pld'] = $nom;
}
else
{ 	
	$final[$aaa[$i]['id']]['pld'] = $aaa[$i]['pld'];
}
}

 

The Result from my code is:

 

Array
(
    [1] => Array
        (
            [pld] => 5
            [id] => 1
            [name] => aaa
        )

    [2] => Array
        (
            [pld] => 5
            [id] => 2
            [name] => bbb
        )

    [3] => Array
        (
            [pld] => 5
            [id] => 3
            [name] => ccc
        )
)

 

Please help me with this, cheers

 

Link to comment
https://forums.phpfreaks.com/topic/123763-solved-array-self-merge-problem/
Share on other sites

Bit confused as to what you're actually trying to achieve. Are you trying to find the sum of the 'pld' for each different id? Or each different name? Or something entirely different?

 

 

Hi Thanks for the reply,

 

The question is to find the sum of 'pld', when  'id' and 'name' are the same.

 

cheers

Hi Guys:

 

I got a array:

I would like to  find the sum of 'pld', when  'id' and 'name' are the same.

 

$aaa  = 
array(
array('id'=>1,'name'=>aaa,'pld'=>1),
array('id'=>1,'name'=>aaa,'pld'=>2),
array('id'=>3,'name'=>ccc,'pld'=>1),
array('id'=>2,'name'=>bbb,'pld'=>1),
array('id'=>1,'name'=>aaa,'pld'=>3),
array('id'=>3,'name'=>ccc,'pld'=>2),
array('id'=>2,'name'=>bbb,'pld'=>2),
array('id'=>3,'name'=>ccc,'pld'=>3),
array('id'=>2,'name'=>bbb,'pld'=>3)
);

 

Finally, I would make it looks like:

 

$aaa  = 
array(
array('id'=>1,'name'=>aaa,'pld'=>6),
array('id'=>2,'name'=>bbb,'pld'=>6),
array('id'=>3,'name'=>ccc,'pld'=>6)
);

 

Here is my code :

 

foreach($aaa as $key => $row)
{
        $id[$key]  = $row['id'];
}
array_multisort($id,SORT_ASC,$aaa);
for($i=0;$i<=sizeof($aaa)-1;$i++)
{
if ($i == 0)
{ 	
	$final[$aaa[$i]['id']]['pld'] = $aaa[$i]['pld'];
}
elseif($aaa[$i]['id'] == $aaa[$i - 1]['id'])
{     
	$nom = $aaa[$i]['pld'] + $aaa[$i -1]['pld'];
	$final[$aaa[$i - 1]['id']]['id'] = $aaa[$i]['id'];
	$final[$aaa[$i - 1]['id']]['name'] = $aaa[$i]['name'];
	$final[$aaa[$i - 1]['id']]['pld'] = $nom;
}
else
{ 	
	$final[$aaa[$i]['id']]['pld'] = $aaa[$i]['pld'];
}
}

 

The Result from my code is:

 

Array
(
    [1] => Array
        (
            [pld] => 5
            [id] => 1
            [name] => aaa
        )

    [2] => Array
        (
            [pld] => 5
            [id] => 2
            [name] => bbb
        )

    [3] => Array
        (
            [pld] => 5
            [id] => 3
            [name] => ccc
        )
)

 

Please help me with this, cheers

 

try

<?php
$aaa  = 
array(
array('id'=>1,'name'=>'aaa','pld'=>1),
array('id'=>1,'name'=>'aaa','pld'=>2),
array('id'=>3,'name'=>'ccc','pld'=>1),
array('id'=>2,'name'=>'bbb','pld'=>1),
array('id'=>1,'name'=>'aaa','pld'=>3),
array('id'=>3,'name'=>'ccc','pld'=>2),
array('id'=>2,'name'=>'bbb','pld'=>2),
array('id'=>3,'name'=>'ccc','pld'=>3),
array('id'=>2,'name'=>'bbb','pld'=>3)
);
$out = array();
foreach ($aaa as $a){
$out[$a['id']][$a['name']] = isset($out[$a['id']][$a['name']]) ? $out[$a['id']][$a['name']] + $a['pld'] : $a['pld'];
}
$aaa = array();
foreach ($out as $id => $a){
foreach ($a as $name => $pld) $aaa[] = array('id' => $id, 'name' => $name, 'pld' => $pld);
}
sort($aaa);
print_r($aaa);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.