2016-08-04 34 views
0

我有一个数组,看起来像这样:如何在使用array_push()时控制数组键?

$date_data = array(
       $date_title => array(
        'title' => get_field('jahr'), 
        'permalink' => get_the_permalink(), 
      ) 
); 

这个数组是一个循环内被发布到阵列$archive_years。如果$date_title的值已经作为数组中的键存在,我不希望它被复制,所以值应该被推送到现有的键。

现在这是发生了什么:

array(2) { 
    [0]=> 
    array(1) { 
    ["2020 - 2010"]=> 
    array(2) { 
     ["title"]=> 
     string(4) "2013" 
     ["permalink"]=> 
     string(68) "http://example.com" 
    } 
    } 
    [1]=> 
    array(1) { 
    ["2020 - 2010"]=> 
    array(2) { 
     ["title"]=> 
     string(4) "2016" 
     ["permalink"]=> 
     string(66) "http://example.com/example" 
    } 
    } 
} 

正如你看到2020 - 2010列出了两次。如何将我的值(get_field('jahr')get_the_permalink())分配给现有密钥,因此没有重复的密钥?

这是整个代码:

$archive_years = array(); 
$args = array('post_type' => 'archiv'); 
$loop = new WP_Query($args); 
while ($loop->have_posts()) : $loop->the_post();   

    // Specify the interval 
    $date_up = ceil(get_field('jahr')/10) * 10; 
    $date_down = ceil(get_field('jahr')/10) * 10 - 10; 
    $date_title = $date_up . ' - ' . $date_down; 

    $date_data = array(
        $date_title => array(
         'title' => get_field('jahr'), 
         'permalink' => get_the_permalink(), 
       ) 
    ); 
    if (!array_key_exists($date_title, $archive_years)) { 
     array_push($archive_years, $date_data); 
    } 

endwhile; 

foreach ($archive_years as $key => $value) { 
    foreach ($value as $k => $v) { 
     echo ' 
      <button type="button" class="btn btn-info collapsible" data-toggle="collapse" data-target="#archiv_'. $v['title'] .'">'. $k .'</button> 
      <div id="archiv_'. $v['title'] .'" class="collapse"> 
       <a href="'. $v["permalink"] .'">'. $v["title"] .'</a><br> 
      </div>'; 
    } 
} 

var_dump($archive_years); 

回答

-1

您只需在您的逻辑错误。考虑此摘录你的代码:

$date_title => array(
        'title' => get_field('jahr'), 
        'permalink' => get_the_permalink(), 
      ) 
); 
if (!array_key_exists($date_title, $archive_years)) { 

记得,array_key_exists()documented定义如下:

bool array_key_exists (mixed $key, array $search) 

因此,您正试图为“数组键”是用什么数组本身。你不能那样做。 (不过,PHP可能不会告诉你,它可能只是做了错误的事情。)

你将不得不包括循环通过您的阵列搜索,深比较反对$date_title每个元素确定它是否已经存在。或者,您可能希望使用另一个数组(如title索引),以快速确定以前是否提及特定标题。

扩展上面的概念更进一步,你可以索引,为了“另一个阵列”并举titlepermalink串联快速检测任何两个值的组合曾经出现过。

在任何情况下,您的程序设计的修改是必要的。由于这不是代码写作服务,所以我希望我的意见能够指出您的方向。

+0

你当然**可以**“做那个”,因为在使用一个*数组* *作为*键*到'array_key_exists',这正是* mixed *关键字的含义。然而,这可能不是他*想要的*。 – alzee

+0

我不知道在这种情况下数组可以工作。 (而且,我当然不会*做*它。) –

+0

实际上它不,我错过了。我认为它在这里工作,但它实际上只意味着* string *或* int *在这种情况下。 – alzee

0
array(2) { 
    [0]=> 
    array(1) { 
    ["2020 - 2010"]=> ... 
    } 
    [1]=> 
    array(1) { 
    ["2020 - 2010"]=> ... 
    } 
} 

这个数组结构对你而言是无稽之谈。你想阵列少一个层次,只是:

array(0) { 
    ["2020 - 2010"]=> ... 
} 

要做到这一点,只是做:

$archive_years[$date_title] = array(
    'title' => get_field('jahr'), 
    'permalink' => get_the_permalink() 
); 
0

这样你可以检查项存在,并将其推到数组:

if (!array_key_exists($date_title, $archive_years)) { 
    $archive_years[$date_title] = array(); 
} 
array_push($archive_years[$date_title], $date_data);