如何在嵌套循环中创建多维数组?
How to create multidimensional array in nested loop while?
我有嵌套循环,我想用这些循环中的一些数据创建多维数组
输出应如下所示:
'productId'=>1
'productImg'=>some_url
'productContent'=> subarray(
'question1'=>value1
'answer1'=>value1,
'question2'=value2,
'question2'=>value2
)
我的带有嵌套 while 循环的代码
if (have_rows('dodaj_pytania_do_produktu')):
while (have_rows('dodaj_pytania_do_produktu')) : the_row();
$counter = 1;
$productID = get_sub_field('nazwa_produktu_faq');
$productImage = get_sub_field('dodaj_zdjecie');
$productsData[] = array(
'productId' => $productID,
'productImg' => $productImage,
);
$product = wc_get_product($productID);
$productName = $product->get_title();
// Loop over sub repeater rows.
if (have_rows('dodaj_pytanie_i_odpowiedz')):
while (have_rows('dodaj_pytanie_i_odpowiedz')) : the_row();
$question = get_sub_field('dodaj_pytanie');
$answer = strip_tags(get_sub_field('dodaj_odpowiedz'));
$productsData[] = array(
'productDesc'=> array(
'question' => $question,
'answer' => $answer
)
);
endwhile;
endif;
endwhile;
else :
echo "brak faq";
endif;
?>
现在我的输出创建了错误的子数组而不是创建子数组
不急着建数组,等你万事俱备了,所以在外层循环的最后新建一个数组,给productContent
数组建一个临时数组,全部放上去然后一起结束
if (have_rows('dodaj_pytania_do_produktu')):
while (have_rows('dodaj_pytania_do_produktu')) : the_row();
$productID = get_sub_field('nazwa_produktu_faq');
$productImage = get_sub_field('dodaj_zdjecie');
$product = wc_get_product($productID);
$productName = $product->get_title();
// Loop over sub repeater rows.
if (have_rows('dodaj_pytanie_i_odpowiedz')):
$x = 1; // counter
$pc = [];
while (have_rows('dodaj_pytanie_i_odpowiedz')) :
the_row();
$question = get_sub_field('dodaj_pytanie');
$answer = strip_tags(get_sub_field('dodaj_odpowiedz'));
// build the inner loop content here
$pc[] = ["question$x" => $question,
"answer$x" => $answer
];
$x++;
endwhile;
// put it all together here
$productsData[] = [
'productId' => $productID,
'productImg' => $productImage,
'productContent = $pc
];
endif;
endwhile;
else :
echo "brak faq";
endif;
我有嵌套循环,我想用这些循环中的一些数据创建多维数组 输出应如下所示:
'productId'=>1
'productImg'=>some_url
'productContent'=> subarray(
'question1'=>value1
'answer1'=>value1,
'question2'=value2,
'question2'=>value2
)
我的带有嵌套 while 循环的代码
if (have_rows('dodaj_pytania_do_produktu')):
while (have_rows('dodaj_pytania_do_produktu')) : the_row();
$counter = 1;
$productID = get_sub_field('nazwa_produktu_faq');
$productImage = get_sub_field('dodaj_zdjecie');
$productsData[] = array(
'productId' => $productID,
'productImg' => $productImage,
);
$product = wc_get_product($productID);
$productName = $product->get_title();
// Loop over sub repeater rows.
if (have_rows('dodaj_pytanie_i_odpowiedz')):
while (have_rows('dodaj_pytanie_i_odpowiedz')) : the_row();
$question = get_sub_field('dodaj_pytanie');
$answer = strip_tags(get_sub_field('dodaj_odpowiedz'));
$productsData[] = array(
'productDesc'=> array(
'question' => $question,
'answer' => $answer
)
);
endwhile;
endif;
endwhile;
else :
echo "brak faq";
endif;
?>
现在我的输出创建了错误的子数组而不是创建子数组
不急着建数组,等你万事俱备了,所以在外层循环的最后新建一个数组,给productContent
数组建一个临时数组,全部放上去然后一起结束
if (have_rows('dodaj_pytania_do_produktu')):
while (have_rows('dodaj_pytania_do_produktu')) : the_row();
$productID = get_sub_field('nazwa_produktu_faq');
$productImage = get_sub_field('dodaj_zdjecie');
$product = wc_get_product($productID);
$productName = $product->get_title();
// Loop over sub repeater rows.
if (have_rows('dodaj_pytanie_i_odpowiedz')):
$x = 1; // counter
$pc = [];
while (have_rows('dodaj_pytanie_i_odpowiedz')) :
the_row();
$question = get_sub_field('dodaj_pytanie');
$answer = strip_tags(get_sub_field('dodaj_odpowiedz'));
// build the inner loop content here
$pc[] = ["question$x" => $question,
"answer$x" => $answer
];
$x++;
endwhile;
// put it all together here
$productsData[] = [
'productId' => $productID,
'productImg' => $productImage,
'productContent = $pc
];
endif;
endwhile;
else :
echo "brak faq";
endif;