简体   繁体   English

如何使用PHP从数组循环中获取唯一ID

[英]How to get unique ids from array loop using php

I have the following array which I am getting as mysql result set 我有以下数组作为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
        )

)

The both arrays are similar I want get the unique entry id using php. 这两个数组都是相似的,我想使用php获得唯一的条目ID。

I tried with the following code but it is producing 2 arrays again. 我尝试使用以下代码,但它再次产生2个数组。

foreach($block_results as $rowarr)
{
   foreach($rowarr as $k=>$v)
   {
        if($k == "entry_id")
        {
            $entid[] = $v; 
        }

   }
} 

Any help is highly appreciated. 非常感谢您的帮助。 Thanks in advance. 提前致谢。



You can use array_map() instead of foreach() . 您可以使用array_map()代替foreach() Example: 例:

$entry_ids = array_unique(
    array_map(
        function($v){ 
            return $v['entry_id'];
        }, 
        $array
    )
);
var_dump($entry_ids);

array_unique() is to remove duplicate elements from array() . array_unique()用于从array()删除重复的元素。

You can get the entry_id directly: 您可以直接获取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);

Output is: 输出为:

array
  0 => int 6
  1 => int 5

Assuming that you want a list of the unique values of entry_id from the array, and it is possible that some will not have an entry_id set: 假设您要从数组中获得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 );

Try this: 尝试这个:

PHP 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;
}

If they are always similiar and the same length: 如果它们总是相似且长度相同:

foreach ($array1 as $key => $value) {
    $firstValue = $array1[$key]['entry_id'];
    $secondValue = $array2[$key]['entry_id'];
}

Though, keep in mind this solution is very sensitive to errors. 但是,请记住,此解决方案对错误非常敏感。

try this like your code: 像您的代码一样尝试:

$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);

output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM