foreach()无效的参数

时间:2016-02-13 00:37:21

标签: php wordpress foreach

你好我一直收到这个错误下面这个循环为foreach()提供的参数无效,我得到两个错误

  

警告:第36行的/home/xxx/xx.php中为foreach()提供的参数无效

     

警告:无法修改标题信息 - 已在第100行的/home/xxx/xx.php中发送的标题(在/home/xxx/xx.php:36处开始输出)   { “状态”: “成功”, “计数”:0 “数据”:[]}

    <?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();

似乎有什么问题?

1 个答案:

答案 0 :(得分:2)

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

替换示例中的以下行:

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

使用以下代码:

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

防止&#34;警告:为foreach()提供的参数无效&#34;发生错误如果由于任何原因你没有得到一个数组回来在foreach循环周围放置一个if语句来检查$extra_attribute_records是否包含一个数组。

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

PHP错误&#34;警告:为foreach()提供的参数无效&#34;正在输出到页面,这就是为什么在您到达&#34;标题时已经发送标题的原因(&#39;内容类型:text / json&#39;);&#34;。修复上述问题应该允许您发送Content-type标头。