如何打印键的数组值

时间:2017-10-11 09:09:56

标签: php mysql

我无法打印此查询返回的数组键的值:

    $user1 = $wpdb->get_results(
        "select product,checked_by,date(submit_date) from diary_user_form_storage where DATE(submit_date) = CURDATE() ;
    ");

    $jsonString = "";
    foreach ($user1 as $key => $value) {
    $productString = stripslashes($value->product);
    $checked = stripslashes($value->checked_by);
    $suppliers = stripslashes($value->suppliers);
    $van_temperature = stripslashes($value->van_temperature);
    $comments = stripslashes($value->comments);
    $product = json_decode($productString, true);
    $checked_by = json_decode($checked , true);
    $suppliers = json_decode($suppliers , true);
    $van_temperature = json_decode($van_temperature, true);
    $comments = json_decode($comments, true);
//This code is running for single variable $product But I have to print all //multiple variable here . How can I print multiple variable here 

    foreach ($product as $v){
       echo  $v['product']; 
    }
}
?>

这是打印的结果

[1]=>
  array(1) {
    ["product"]=>
    string(5) "GSSHS"
  }
}

如果我为我的SQL查询print_r($ user1)执行了print_r,那么输出就是你可以看到的数组列表。

 [1] => stdClass Object
        (
            [store_id] => 2
            [user_id] => 7
            [unit_data] => [{\"unit_data\":\"10\"}]
            [suppliers] => 
            [product] => [{\"product\":\"AA\"}]
            [checked_by] => [{\"checked_by\":\"Rakesh Kushwaha\"}]
            [temperature_in] => [{\"temperature_in\":\"QWW\"}]
            [temperature_out] => 
            [time_in] => [{\"time_in\":\"44:44\"}]
            [time_out] => 
            [van_temperature] => 
            [center_temperature] => 
            [cooling_temperature] => 
            [end_cooling_temperature] => 
            [comments] => 
            [submit_date] => 2017-10-18 22:12:39
            [time_holded] => 
            [temperature] => 
            [time_reheated] => 
            [extra_timing] => 
            [image_url] => 
            [category_name] => opening
            [category] => [{\"category\":\"Frozen\"}]
        )

我只需要数组的这个键的值:["product"]=>"GSSHS"

2 个答案:

答案 0 :(得分:0)

您可以使用ARRAY_A

$user1 = $wpdb->get_results(
    "select product,checked_by,date(submit_date) from diary_user_form_storage where DATE(submit_date) = CURDATE() ;
",ARRAY_A);

答案 1 :(得分:0)

首先你不能使用数组的箭头符号

因为json_decode($ jsonString,true)将返回一个数组

您可以在没有真正标志的情况下使用它,或者只将其作为数组

访问

所以你不能这样使用它:

foreach ($product as $v){
   echo $v->product; 
}

尝试使用它:

foreach ($product as $v){
   echo $v['product']; 
}

根据您的更新

你正在做的一切都是对的。但问题出在你的索引中。 现在尝试这样做:

foreach($product as $p) {
    if(!empty($p[0]['product'])) {
        echo $p[0]['product'];
    }
}

或者您可以用较短的一个替换if:

echo !empty($p[0]['product'])? $p[0]['product']: null;