PHP获取二维数组中相同的元素
比如array(array('id'=>'10'),array('id'=>'2'),array('id'=>'10'),array('id'=>'3'),array('...
比如
array(array('id' => '10'),array('id' => '2'),array('id' => '10'),array('id' => '3'),array('id' => '2'))
怎么获取这个二维数组中相同id的数组有几个。id是多少啊! 展开
array(array('id' => '10'),array('id' => '2'),array('id' => '10'),array('id' => '3'),array('id' => '2'))
怎么获取这个二维数组中相同id的数组有几个。id是多少啊! 展开
展开全部
$a = array(array('id' => '10'),array('id' => '2'),array('id' => '10'),array('id' => '3'),array('id' => '2'));
$ids = array();
foreach($a as $k=>$v)
{
foreach($v as $value)
{
if(!in_array($value,$ids))
$ids[] = $value;
}
}
foreach($a as $k=>$v)
{
foreach($v as $value)
{
foreach($ids as $key=>$id)
{
if($id == $value)
{
$res[$key]['id'] = $id;
$res[$key]['num']++;
}
}
}
}
print_r($ids);
print_r($res);
Array
(
[0] => 10
[1] => 2
[2] => 3
)
Array
(
[0] => Array
(
[id] => 10
[num] => 2
)
[1] => Array
(
[id] => 2
[num] => 2
)
[2] => Array
(
[id] => 3
[num] => 1
)
)
$ids = array();
foreach($a as $k=>$v)
{
foreach($v as $value)
{
if(!in_array($value,$ids))
$ids[] = $value;
}
}
foreach($a as $k=>$v)
{
foreach($v as $value)
{
foreach($ids as $key=>$id)
{
if($id == $value)
{
$res[$key]['id'] = $id;
$res[$key]['num']++;
}
}
}
}
print_r($ids);
print_r($res);
Array
(
[0] => 10
[1] => 2
[2] => 3
)
Array
(
[0] => Array
(
[id] => 10
[num] => 2
)
[1] => Array
(
[id] => 2
[num] => 2
)
[2] => Array
(
[id] => 3
[num] => 1
)
)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询