在foreach循环内创建的数组与下面的结果一样是个不同的。我需要它们在父数组中。
Array
(
[0] => Array
(
[url] => https://example.com/product/osaka-entry-tee-superdry-6/
[img] => <img width="114" height="130" src="//storage.googleapis.com/example-stateless/2017/11/489b0847-538228-0286_1-114x130.jpeg" class="attachment-shop_thumbnail size-shop_thumbnail wp-post-image wp-stateless-item" alt="" data-image-size="shop_thumbnail" data-stateless-media-bucket="example-stateless" data-stateless-media-name="2017/11/489b0847-538228-0286_1.jpeg" />
[name] => OSAKA ENTRY TEE SUPERDRY 6
[desc] => Superdry mens Big Dry Entry t-shirt. A crew neck t-shirt featuring a large Dry cracked print with embossed JPN logos within the print, a Super print on one sleeve and the t-shirt is finished with a Superdry logo tab on one sleeve. Specifications: - Material: cotton 100% Care: - Wash: machine wash - cold (30°C) - Bleach: do not bleach - Dry: line dry - Iron: iron - low - Dry clean: do not dry clean
[seller_url] => https://example.com/trade/project-store/
[author_name] => Project Store
[slug] => <a href="https://example.com/product/osaka-entry-tee-superdry-6/" class="button product_type_simple add_to_cart_button" target="_self" ><span>GO TO SHOP</span></a>
[geoinfo] => Array
(
[0] => -22.9813
[1] => -47.0265
)
)
)
Array
(
[0] => Array
(
[url] => https://example.com/product/on1-jersey-unif/
[img] => <img width="114" height="130" src="//storage.googleapis.com/example-stateless/2018/01/b1b0193f-258836-0426_1-510x600-114x130.jpeg" class="attachment-shop_thumbnail size-shop_thumbnail wp-post-image wp-stateless-item" alt="" data-image-size="shop_thumbnail" data-stateless-media-bucket="example-stateless" data-stateless-media-name="2018/01/b1b0193f-258836-0426_1-510x600.jpeg" />
[name] => ON1 JERSEY UNIF
[desc] => UNIF offers a collection of contemporary womenswear that features iconic graphic T-shirts as well fashion-forward silhouettes. Expect edgy details, bold prints, and pieces fit for the modern downtown girl. 100% poly. Unlined. Embroidered logo detailing on front, back and sleeves. Revolve Style No. UNIX-WD35. Manufacturer Style No. USJ-4006.
[seller_url] => https://example.com/trade/project-store/
[author_name] => Project Store
[slug] => <a href="https://example.com/product/on1-jersey-unif/" class="button product_type_simple add_to_cart_button" target="_self" ><span>GO TO SHOP</span></a>
[geoinfo] => Array
(
[0] => -23.0313
[1] => -46.9725
)
)
)
Bellow是创建上述结果的功能。 $ tradesInfo数组是在嵌套的foreach循环之外声明的,但它没有解决问题!此功能是与Woocommerce和Dokan一起使用的Wordpress脚本的一部分。
if($pageposts)
{
$tradesInfo = Array();
foreach($pageposts as $prd)
{
if (!is_object($prd)) {
$query = $wpdb->get_results("SELECT * FROM wp_geo_location WHERE productid=" . $prd . "");
} else {
$query = $wpdb->get_results("SELECT * FROM wp_geo_location WHERE productid=" . $prd->post_id . "");
}
$array = json_decode(json_encode($query), true);
foreach($array as $plots)
{
$product = wc_get_product($plots["productid"]);
$author = get_user_by('id', $product->post->post_author);
// Add cart button or reservation link
$slug = basename(get_permalink());
if (function_exists('dokan_get_store_url')) {
$sellerurl = dokan_get_store_url($author->ID);
} else {
$sellerurl = '';
}
// Get parent cat ID by product ID
$prod_terms = get_the_terms($plots["productid"], 'product_cat');
foreach ($prod_terms as $prod_term) {
// gets product cat id
$product_cat_id = $prod_term->term_id;
// gets an array of all parent category levels
$product_parent_categories_all_hierachy = get_ancestors($product_cat_id, 'product_cat');
// This cuts the array and extracts the last set in the array
$last_parent_cat = array_slice($product_parent_categories_all_hierachy, -1, 1, true);
foreach ($last_parent_cat as $last_parent_cat_value) {
// $last_parent_cat_value is the id of the most top level category, can be use whichever one like
$catID = $last_parent_cat_value;
}
}
$tmp = '<a href="' . get_permalink($product->get_id()) . '" class="button product_type_simple add_to_cart_button" target="_self" ><span>' . __("GO TO SHOP", "pinnergeolocation") . '</span></a>';
// PRODUCT INFO IN GOOGLE MAP
if($product != "")
{
if ($slug == "medicaments") {
$result_slug = printf('<br><a href="%s" title=" ' .__("More Details", "pinnergeolocation") . ' "><span color="#fa6a6a"><b>%s</b></span></a>', esc_url(get_permalink($product->get_id())), __("More Info", "pinnergeolocation"));
} else {
$result_slug = str_replace(array("\n", "\r"), '', $tmp);
}
$tradesInfo[] = array(
"url" => esc_url(get_permalink($plots["productid"])),
"img" => $product->get_image(),
"name" => mb_strtoupper(get_the_title($plots["productid"]), "UTF-8"),
"desc" => preg_replace('/\s+/', ' ', get_the_excerpt( $product->post )),
"seller_url" => $sellerurl,
"author_name" => $author->display_name,
"slug" => $result_slug,
"geoinfo" => array($plots["latitude"], $plots["longitude"])
);
$tradesJSON = json_encode($tradesInfo);
echo $tradesJSON;
}
}
}
}
答案 0 :(得分:-2)
让我们说这是你的代码:
foreach($var as $arr){
$myarray=....; // here you get your array
print_r($myarray);
}
更改它将每个数组插入另一个数组:
$wrapArray=array();
foreach($var as $arr){
$myarray=....; // here you get your array
$wrapArray[]=$myarray;
}
print_r($wrapArray);