Php 如何推送/创建关联数组?

Php 如何推送/创建关联数组?,php,wordpress,loops,Php,Wordpress,Loops,我正在运行一个帖子循环,其中有四个属性需要关联:标题、日期、链接、位置: while (have_posts()) : the_post(); if( !in_array( $post->ID, $already_displayed_ids )) { array_push( $thisPostid, $post->ID ); $title = get_the_title(); $location = usp_get_meta(f

我正在运行一个帖子循环,其中有四个属性需要关联:标题、日期、链接、位置:

while (have_posts()) : the_post();
    if( !in_array( $post->ID, $already_displayed_ids )) {
        array_push( $thisPostid, $post->ID );
        $title = get_the_title();
        $location = usp_get_meta(false, 'usp-custom-8');
        $link = get_permalink();
        $contentYear = usp_get_meta(false, 'usp-custom-14');
        if ($contentYear >= 0 && $contentYear <= 2019) {
            array_push($yearsArray, $contentYear);
            if (($wp_query->current_post +1) == ($wp_query->post_count)) {
                $yearsArray = array_unique($yearsArray);
                sort($yearsArray);
            }
        }
        array_push( $already_displayed_ids, $post->ID );
    }
endwhile;
while(have_posts()):the_post();
如果(!在数组中($post->ID,$ready\u displated\u ID)){
数组\u push($thisposted,$post->ID);
$title=获取标题();
$location=usp_get_meta(false,'usp-custom-8');
$link=get_permalink();
$contentYear=usp_get_meta(false,'usp-custom-14');
如果($contentYear>=0&&$contentYear current\u post+1)=($wp\u query->post\u count)){
$yearsArray=array\u unique($yearsArray);
排序($yearsArray);
}
}
数组\u推送($ready\u displated\u ID,$post->ID);
}
结束时;
基本上,我正在运行一个循环,我检查
$ready\u display\u id
中是否存在重复,我需要关联并推送我推送的任何帖子的标题、位置、日期和链接
数组推送($yearsArray,$contentYear)


目前我只能推送
$contentYear
,但没有与推送帖子相关的
链接
标题
位置
,我想创建一个关联数组,以便推送每个帖子所需的所有规范
$contentYear
是一个带有日期的自定义字段,因此我使用这些值生成了一个导航,它们是日期。但我需要将它们的标题、位置和链接与每个日期关联起来,因为我目前不知道如何将这些规格与我正在推送的时间关联起来。

据我所知,没有直接的功能可以从帖子中获取关联数组。 以下是您的要求的一个片段:

$posts = array();
if (have_post()) :
    while (have_posts()) :
        array_push($posts, 
            Array(
                'id'=>the_ID(),
                'quantity'=>1,
                'size'=>$size,
                'colour'=>$colour
            )
        );
    endwhile;
    $_SESSION['cart'] = $posts[0]; // change 0 index
endif;

请注意上面的代码并不理想,您可能应该这样做。此外,您可能希望使用Cookie而不是会话。

我就是这样解决的:

while (have_posts()) : the_post();
    if( !in_array( $post->ID, $already_displayed_ids )) {
        array_push( $thisPostid, $post->ID );
        $contentYear = usp_get_meta(false, 'usp-custom-14');
        if ($contentYear >= 0 && $contentYear <= 2019) {
            array_push($yearsArray, 
                array(
                    'year'=> $contentYear,
                    'link'=> get_permalink(),
                    'location'=> usp_get_meta(false, 'usp-custom-8'),
                    'title'=> get_the_title()
                )
            );
        }
        array_push( $already_displayed_ids, $post->ID );
    }
endwhile;
while(have_posts()):the_post();
如果(!在数组中($post->ID,$ready\u displated\u ID)){
数组\u push($thisposted,$post->ID);
$contentYear=usp_get_meta(false,'usp-custom-14');
如果($contentYear>=0&&$contentYear$contentYear,
'link'=>get_permalink(),
'location'=>usp\u get\u meta(false,'usp-custom-8'),
'title'=>获取标题()
)
);
}
数组\u推送($ready\u displated\u ID,$post->ID);
}
结束时;

然后我可以做
foreach($yeararray as$year){
echo$year['title']

谢谢,我想用php做的更多,比如
$\u SESSION[“cart”]=array($id=>array(array(“数量”=>1,“大小”=>size,“颜色”=>color)))
也可以。但请注意,PHP会话是写入服务器上的文本文件中的。您不应该在循环中写入任何类型的会话。这只是我发现的一个示例,从未使用关联数组,您能否使用该示例进行详细说明,前提是我更好地理解我的案例?更新了我的问题一个真实的案例场景,我们能适应吗?你的问题变化太大了,我希望你能找到你想要的解决方案。我不清楚这个问题或问题到底是什么…!?@deceze检查更新问题是否清楚。不,不清楚。你解释了你想做什么,并展示了似乎可以做到的代码。问题/问题/i在哪里问题?@deceze再次更新,有更好的吗?@deceze解决了问题,发布了我自己的答案