2016-02-13 116 views
-1

你好,我不断收到对的foreach()下面这个循环提供的这个错误无效的说法,我得到两个错误的foreach()无效参数

警告:的foreach提供了无效的参数()在/ home/XXX在线/xx.php 36

警告:不能更改头信息 - 在/home/xxx/xx.php头已经发出(输出开始/home/xxx/xx.php:36)上线100 {“status”:“success”,“count”:0,“data”:[]}

<?php 
require('../../../../wp-blog-header.php'); 
header("HTTP/1.1 200 OK"); 
//$condition = isset($_REQUEST['condition']) ? $_REQUEST['condition'] : ''; 
//$condition = str_replace('\\','',$condition); 
//$condition = '{"id":200,"items":[{"size":"m","color":"green"}]}'; 
$condition = isset($_REQUEST['condition'])? stripslashes($_REQUEST['condition']) : ''; 
$search_condition = json_decode($condition,true); 
/* 
$search_condition = array(
'id'=>194, 
'items'=>array(
    array(
     'size'=>'xl', 
     'color'=>'blue', 
    ), 
) 
); 
*/ 


$id = $search_condition['id']; 
$items= $search_condition['items']; 

$condition_pair =array(); 
global $wpdb; 
$table_posts = $wpdb->prefix . 'posts'; 
$table_post_meta = $wpdb->prefix . 'postmeta'; 
$string = 'product-'.$id.'-variation'; 
$posts = $wpdb->get_results("select * from $table_posts WHERE post_name LIKE '%$string%' "); 

//echo "select * from $table_posts p INNER JOIN $table_post_meta pm ON pm.post_id = p.ID WHERE p.post_name LIKE '%$string%'";exit; 

$extra_attribute_records = get_post_meta($id, '_product_attributes', TRUE); 
$attribute_names = array(); 
foreach($extra_attribute_records as $key => $value) 
{ 
$attribute_names[] = $key; 

} 

$list_product = array(); 
$i= 0; 
foreach($posts as $post) 
{ 
    $image_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID)); 

$price = get_post_meta($post->ID, '_regular_price', TRUE); 
$promotion = get_post_meta($post->ID, '_sale_price', TRUE); 
$weight = get_post_meta($post->ID, '_weight', TRUE); 
$height = get_post_meta($post->ID, '_height', TRUE); 
$width = get_post_meta($post->ID, '_width', TRUE); 
$length = get_post_meta($post->ID, '_length', TRUE); 
$quantity = get_post_meta($post->ID, '_stock', TRUE); 
$quantity = (int)$quantity; 
$sku = get_post_meta($post->ID, '_sku', TRUE); 
$stock_status = get_post_meta($post->ID, '_stock_status', TRUE); 

$extra_attribute_records = array(); 
foreach($attribute_names as $att_name) 
{ 
    $extra_attribute_records[$att_name] = strtolower(get_post_meta($post->ID, 'attribute_'.$att_name, TRUE)); 
} 

if(in_array($extra_attribute_records,$items)) 
{ 

    $title = get_the_title($id).' '.strtoupper(implode('/', $extra_attribute_records)).''; 
    $arr = array(
     'id' => $post->ID, 
     'title' => $title, 
     'description' => get_post_field('post_content', $id), 
     'excerpt' => get_post_field('post_excerpt', $id), 
     'price' => $price, 
     'currency' => get_woocommerce_currency(), 
     'weight' => $weight, 
     'sku' => $sku, 
     'quantity' => $quantity, 
     'featured' => 0, 
     'mark_as_new' => $post->mark_as_new, 
     'width' => $width, 
     'height' => $height, 
     'length' => $length, 
     'sort_order' => $post->sort_order, 
     'promotion_price'=>(int)$promotion, 
     'thumbnail' => $image_url[0], 
     'category_slug'=>$post->slug, 
     'extra_attributes' => array(),//'$extra_attribute_records, 
     'categories'=>get_the_product_category(), 
     'stock_status'=>$stock_status, 
     'post_date' => $post->post_date, 
    ); 
    $list_product[] = $arr; 
    $i++; 
} 

} 


header('Content-type: text/json'); 
echo json_encode(
array(
    "status" => "success", 
    'count' => $i, 
    "data" => $list_product, 
)); 
exit(); 

什么似乎是问题?

+0

请发布_full_错误消息... – arkascha

+0

只有第一个,也就是第二个只是后续。 – arkascha

+1

请告诉我们'get_post_meta()'做了什么,所以发布它的代码。谢谢。 – arkascha

回答

2

要解决该问题,请将get_post_meta()方法中的第3个参数设置为FALSE。可选,您可以完全删除第三个参数,因为defualt值也是FALSE。你现在应该得到一个返回的数组。见https://developer.wordpress.org/reference/functions/get_post_meta/

替换的例子中,以下行:

$extra_attribute_records = get_post_meta($id, '_product_attributes', TRUE); 

随着这行代码: “:()提供的foreach无效参数警告” 错误从

$extra_attribute_records = get_post_meta($id, '_product_attributes', FALSE); 

为了防止如果出于任何原因,如果不在数组中返回一个if语句,则会发生在您的foreach循环中,以检查$extra_attribute_records是否包含数组。

if(is_array($extra_attribute_records)){ 
    foreach($extra_attribute_records as $key => $value) { 
     $attribute_names[] = $key; 
    } 
} 

的PHP错误“警告:提供的foreach()无效参数”是被输出到这就是为什么你的头已经被你到“头('内容类型时发送的原因页面:text/json');“。解决上述问题应该可以让你发送Content-type头。

+0

这试图解决症状,而不是原因。 – arkascha

+0

由于问题存在于我们尚未提供代码的'get_post_meta()'方法中,所以我无法修复原因。 –

+0

那么也许你不应该发布“答案”,因为这不能回答这个问题? – arkascha