我有以下数组,我将其作为mysql结果集
Array
(
[0] => Array
(
[slug] => block_three_column
[title] => CSG 2
[type_id] => 8
[entry_id] => 6
[stream_id] => 11
)
[1] => Array
(
[slug] => block_three_column
[title] => CSG
[type_id] => 8
[entry_id] => 5
[stream_id] => 11
)
)
Array
(
[0] => Array
(
[slug] => block_three_column
[title] => CSG 2
[type_id] => 8
[entry_id] => 6
[stream_id] => 11
)
[1] => Array
(
[slug] => block_three_column
[title] => CSG
[type_id] => 8
[entry_id] => 5
[stream_id] => 11
)
)
两个数组都很相似我想用php获取唯一的条目ID。
我尝试使用以下代码,但它再次生成2个数组。
foreach($block_results as $rowarr)
{
foreach($rowarr as $k=>$v)
{
if($k == "entry_id")
{
$entid[] = $v;
}
}
}
非常感谢任何帮助。提前谢谢。
答案 0 :(得分:2)
您可以使用array_map()
代替foreach()
。例如:
$entry_ids = array_unique(
array_map(
function($v){
return $v['entry_id'];
},
$array
)
);
var_dump($entry_ids);
array_unique()
将删除array()
中的重复元素。
答案 1 :(得分:1)
您可以直接获得entry_id
:
$array = array(
array(
'slug' => 'block_three_column',
'title' => 'CSG 2',
'type_id' => 8,
'entry_id' => 6,
'stream_id' => 11
),
array(
'slug' => 'block_three_column',
'title' => 'CSG',
'type_id' => 8,
'entry_id' => 5,
'stream_id' => 11
)
);
foreach ($array as $innerArray) {
if (isset($innerArray['entry_id'])) {
$entid[] = $innerArray['entry_id'];
}
}
var_dump($entid);
输出是:
array
0 => int 6
1 => int 5
答案 2 :(得分:1)
假设您需要数组中entry_id
的唯一值列表,并且有些值可能没有设置entry_id
:
$entid = array();
foreach ( $array as $blockArray) {
if ( isset( $blockArray['entry_id'] )
&& !in_array( $blockArray['entry_id'], $entid ) ) {
$entid[] = $blockArray['entry_id'];
}
}
var_dump( $entid );
答案 3 :(得分:0)
试试这个:
<强> PHP 强>
$entid = array(); //Holds unique id's
foreach($block_results as $rowarr)
{
//Make sure entry_id exists
$entid[] = array_key_exists('entry_id',$rowarry) ? $rowarr['entry_id'] : false;
}
答案 4 :(得分:0)
如果它们总是相似且长度相同:
foreach ($array1 as $key => $value) {
$firstValue = $array1[$key]['entry_id'];
$secondValue = $array2[$key]['entry_id'];
}
但是,请记住,此解决方案对错误非常敏感。
答案 5 :(得分:0)
试试这个代码:
$block_results = array(
array(
'slug' => 'block_three_column',
'title' => 'CSG 2',
'type_id' => 8,
'entry_id' => 6,
'stream_id' => 11
),
array(
'slug' => 'block_three_column',
'title' => 'CSG',
'type_id' => 8,
'entry_id' => 6,
'stream_id' => 11
),
array(
'slug' => 'block_three_column',
'title' => 'CSG',
'type_id' => 8,
'entry_id' => 7,
'stream_id' => 11
)
);
var_dump($block_results);
$entid=array();
if(count($block_results)>0)foreach($block_results as $rowarr)
{
$entid[] = $rowarr['entry_id'];
}
$entid=array_unique($entid);
var_dump($entid);
输出:
array
0 =>
array
'slug' => string 'block_three_column' (length=18)
'title' => string 'CSG 2' (length=5)
'type_id' => int 8
'entry_id' => int 6
'stream_id' => int 11
1 =>
array
'slug' => string 'block_three_column' (length=18)
'title' => string 'CSG' (length=3)
'type_id' => int 8
'entry_id' => int 6
'stream_id' => int 11
2 =>
array
'slug' => string 'block_three_column' (length=18)
'title' => string 'CSG' (length=3)
'type_id' => int 8
'entry_id' => int 7
'stream_id' => int 11
array
0 => int 6
2 => int 7