【发布时间】:2019-12-17 18:32:11
【问题描述】:
我有一个数组,其中包含多个数组(目前为 3 个)。 里面的每个数组都有多个帖子对象。
我正在尝试遍历这 3 个数组,每次从其中一个中拉出 1 个帖子对象,然后将其推送到我创建的新空数组,顺序与我在其他。
// dynamic number
$count_posts_query = 9
//the array that contains 3 arrays with posts in it (dynamic can be more
)
$term_posts = array(
[0] => array( <--- contains 26 posts objects
[0] => {post object}.
[1] => {post object},
[2] => {post object},
[... and so on 26 objects]
)
[1] => array( <--- contains 58 posts objects
[0] => {post object}.
[1] => {post object},
[2] => {post object},
[... and so on 58 objects]
)
[2] => array( <--- contains 103 posts objects
[0] => {post object}.
[1] => {post object},
[2] => {post object},
[... and so on 103 objects]
)
),
for ( $i = 0; $i < $count_posts_query; $i ++ ) {
array_push( $new_terms_arrays, $term_posts[$i][ $i ] );
}
这个循环的问题是 $i 不正确,它只运行了 3 次,每次输入数组的位置和具有相同 $i 编号的对象,然后其他 6 个对象为空而不是升序正确的
错误图像 - https://i.ibb.co/4PxXxgw/Screenshot-at-Aug-10-09-15-20.png
【问题讨论】:
-
每次都需要相同的帖子吗?你必须确保选择是平衡的吗?例如。每个数组中的帖子数量相同吗?
-
你的
$count_posts_query = 9但$term_posts只有三个数组。 -
$count_posts_query代表什么?您希望如何使用它来构建您的新阵列? -
@Scuzzy
$count_posts_query是一个动态数字,表示我想从 $_term_posts 中提取的帖子总数