【问题标题】:foreach() Invalid argumentforeach() 参数无效
【发布时间】:2016-02-13 00:37:21
【问题描述】:

您好,我一直收到此错误 在下面的此循环中为 foreach() 提供的参数无效,我收到两个错误

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

警告:无法修改标头信息 - 第 100 行 /home/xxx/xx.php 中的标头已由(输出开始于 /home/xxx/xx.php:36)发送 {"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();

似乎是什么问题?

【问题讨论】:

  • 请发布完整错误信息...
  • 只有第一个,也就是第二个,只是后续。
  • 请告诉我们get_post_meta() 做了什么,所以发布它的代码。谢谢。
  • 我们能看到行号吗?
  • 好吧,$extra_attribute_records 是一个数组吗?

标签: php wordpress foreach


【解决方案1】:

要解决此问题,请将get_post_meta() 方法中的第三个参数设置为 FALSE。您可以选择完全删除第三个参数,因为默认值也是 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);

为了防止“警告:为 foreach() 提供的参数无效”错误发生,如果出于任何原因您无法取回数组,请在 foreach 循环周围放置一个 if 语句以检查 $extra_attribute_records 是否包含数组。

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

PHP 错误“警告:foreach() 提供的参数无效”正在输出到页面,这就是为什么在您到达“header('Content-type: text/json ');"。修复上述问题应该允许您发送 Content-type 标头。

【讨论】:

  • 这试图解决症状,而不是原因。
  • 我无法解决这个问题,因为问题出在 get_post_meta() 方法中,我们尚未获得该方法的代码。
  • 那么也许您不应该发布“答案”,因为这不能回答问题?
  • 我现在添加了我建议的修复程序,以使 get_post_meta() 方法返回一个数组而不是一个字符串。
  • 这是正确答案,但您不必传入 false - 这是默认值。
猜你喜欢
  • 1970-01-01
  • 2012-11-08
  • 2023-03-20
  • 1970-01-01
  • 2015-06-09
  • 2015-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多