【问题标题】:How can print array value of key如何打印键的数组值
【发布时间】:2017-10-11 09:09:56
【问题描述】:

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

    $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"

【问题讨论】:

  • 请您打印所有结果,以便您可以在 foreach 之前将 var_dump 放在 $product 上,var_dump($product)
  • 您好,能否打印一下所有结果:$user1 内容?
  • Hassan Ali Salem @是的,我可以打印
  • 你好桑杰,我更新了答案,试试看告诉我

标签: php mysql


【解决方案1】:

您可以使用 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);

【讨论】:

  • somin@invalid forech 参数
  • 使用 ARRAY_A 它将返回查询的数组结果,因此您需要在 foreach 中进行更改,因为您使用数据作为对象而不是显示为数组
  • Somin@可以使用explode和打印这个值。
  • 我可以在您尝试的地方提供链接以便我检查吗?
【解决方案2】:

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

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

您可以在没有 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;

【讨论】:

  • Hassan Ali Salem@我正在尝试打印,但打印的是数值 4
  • 嗨@sanjaykumar,试着这样做:$v[0]['product'],请告诉我产品变量的内容
  • 哈桑·阿里·塞勒姆@它的工作很好。但是如果我打印了多个列,那么我该怎么做。所以请你告诉我我打印了多个数据,比如 $v['product']; $v['用户名'];等
  • Hassan Ali Salem@请看我更新的问题兄弟,我需要你的帮助。拜托,你在那儿,只有你能做到这一点
  • 你好@sanjaykumar,我会为你做,但我需要一些时间,因为我现在在工作:D,我会在休息时间做
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多