【问题标题】:How to make a unique associative array?如何制作唯一的关联数组?
【发布时间】:2018-11-17 06:53:12
【问题描述】:

我有一个这样填充的数组:

array_push($comboUserPosts, 
    array(
        'link'=> get_permalink(),
        'dates'=> $value,
        'title'=> get_the_title()
    )
);

然后当我完成所有循环时,我会这样做:

array_unique($comboUserPosts);

但它仍然给我重复。如果我这样做:

echo '<pre>' . var_export($comboUserPosts, true) . '</pre>';

这是我得到的:

array (
  0 => 
  array (
    'link' => 'https://example.com/test/test-values-users/',
    'dates' => '1920',
    'title' => 'test values users',
  ),
  1 => 
  array (
    'link' => 'https://example.com/test/test-values-users/',
    'dates' => ' 1954',
    'title' => 'test values users',
  ),
  2 => 
  array (
    'link' => 'https://example.com/test/provo-filter/',
    'dates' => '1600',
    'title' => 'provo filter',
  ),
  3 => 
  array (
    'link' => 'https://example.com/test/provo-filter/',
    'dates' => '1450',
    'title' => 'provo filter',
  ),
  4 => 
  array (
    'link' => 'https://example.com/test/provo-filter/',
    'dates' => '1330',
    'title' => 'provo filter',
  ),
)

但是通过查看,我只有 2 个唯一链接和标题,我应该只显示这两个链接+标题:

foreach ($comboUserPosts as $value) { ?>
    <h2><a href="<?php echo $value['link']; ?>"><?php echo $value['title']; ?></a>
<?php }

【问题讨论】:

标签: php arrays associative-array


【解决方案1】:

我能想到的最简单的方法是通过链接(使用array_column())对数组进行索引,然后提取值...

$comboUserPosts = array_values(array_column($comboUserPosts, null, 'link'));
echo var_export($comboUserPosts, true);

上面的测试数据给出了...

array (
  0 => 
  array (
    'link' => 'https://example.com/test/test-values-users/',
    'dates' => ' 1954',
    'title' => 'test values users',
  ),
  1 => 
  array (
    'link' => 'https://example.com/test/provo-filter/',
    'dates' => '1330',
    'title' => 'provo filter',
  ),
)

【讨论】:

  • 您也可以这样做:$link_titles = array_column($data, 'title', 'link'); 然后遍历链接和标题的键和值。
【解决方案2】:

您只需要先删除元素dates,然后使用array_unique

foreach($arr as $k=> &$v){
  unset($v['dates']);
}
print_r(array_unique($arr, SORT_REGULAR));

Live Demo

【讨论】:

  • 好的 NP 但你说@C2486 I don't care about the dates, I only need the two unique links and title. 所以我想出了这个解决方案
  • 你说得对,我的意思是我不在乎代码中的这一点,因为我认为你会提出一个忽略它的解决方案,但我没有考虑删除它们,谢谢你,我会投票,因为它确实按照我的要求回答了这个问题:)
【解决方案3】:

你可以这样写代码

<?php 
$ar = array (
  0 => 
  array (
    'link' => 'https://example.com/test/test-values-users/',
    'dates' => '1920',
    'title' => 'test values users',
  ),
  1 => 
  array (
    'link' => 'https://example.com/test/test-values-users/',
    'dates' => ' 1954',
    'title' => 'test values users',
  ),
  2 => 
  array (
    'link' => 'https://example.com/test/provo-filter/',
    'dates' => '1600',
    'title' => 'provo filter',
  ),
  3 => 
  array (
    'link' => 'https://example.com/test/provo-filter/',
    'dates' => '1450',
    'title' => 'provo filter',
  ),
  4 => 
  array (
    'link' => 'https://example.com/test/provo-filter/',
    'dates' => '1330',
    'title' => 'provo filter',
  ),
);
function unique_multidimensional_array($array, $key) { 
            $temp_array = array(); 
            $i = 0; 
            $key_array = array(); 

            foreach($array as $val) { 
                if (!in_array($val[$key], $key_array)) { 
                    $key_array[$i] = $val[$key]; 
                    $temp_array[$i] = $val; 
                } 
                $i++; 
            } 
            return $temp_array; 
        } 
$unique = unique_multidimensional_array($ar, 'link');
echo "<pre>";
print_r($unique);

?>

【讨论】:

  • 将进行测试,但请记住我也需要标题,因为 mm 看起来不错但要小心,因为我也需要标题 &lt;h2&gt;&lt;a href="&lt;?php echo $value['link']; ?&gt;"&gt;&lt;?php echo $value['title']; ?&gt;&lt;/a&gt;
  • 如果您想使用多个键进行测试,您可以将键更改为数组。一般来说,Title 与 url 匹配,所以必须是唯一的,所以不需要测试 title
  • 好的,谢谢,我投了赞成票,但我会接受另一个答案,因为它要短得多,除非你发现它有问题
【解决方案4】:

你可以用这个方法

array_unique($associativeArray,SORT_REGULAR);

使用名称和年龄键定义关联数组 $associativeArray

  $associativeArray[] = array("name" => "Yogesh Singh","no"=>4);
  $associativeArray[] = array("name" => "Sonarika Singh","no"=>4);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-30
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多