繁体   English   中英

从mysql数组回显特定值

[英]echo specific value from mysql array

所以我试图使用php(wordpress)从mysql表中回显值

大多数异端都表现良好

但是当我试图回声

$ row-> form我得到这样的数组返回:

string(832) "[{"id":"1","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"First name","value":"firstname1"},{"id":"2","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Last name","value":"lastname1"},{"id":"6","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Accomodation","value":"Hotel Sample"},{"id":"3","is_email":"true","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Email","value":"sample@example.com"},{"id":"4","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Phone","value":"333333"},{"id":"5","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Message","value":"Goog Job"}]" 

string(973) "[{"id":"1","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"First name","value":"ddd"},{"id":"2","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Last name","value":"ddd"},{"id":"3","is_email":"true","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Email","value":"sample1@example.com"},{"id":"6","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Accomodation\/PikUp Address:","value":"House no 203"},{"id":"8","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Room Number:","value":"d"},{"id":"4","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Phone","value":"44444"},{"id":"5","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Message","value":"444444"}]" 

这是我的sql代码:

$result = $wpdb->get_results( "SELECT * FROM wp_table_1");

foreach($result as $row)
 {
echo $row->form;
}

我希望呼应名字。 谢谢

echo $row->form;

上面的行将回显数组。

要仅回显名字,请按照以下模式使用

echo $row->form['first_name'];

如果您找不到合适的解决方案,这只是一种解决方法。 您应该尝试使用类似get_var()get_col() 请查阅wordpress参考

<?php             
$rows = [ 
'
[ {"id":"1","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"First name","value":"firstname1"},
  {"id":"2","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Last name","value":"lastname1"},
  {"id":"6","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Accomodation","value":"Hotel Sample"},
  {"id":"3","is_email":"true","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Email","value":"sample@example.com"},
  {"id":"4","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Phone","value":"333333"},
  {"id":"5","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Message","value":"Goog Job"}
] 
',

'
[ {"id":"1","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"First name","value":"ddd"},
  {"id":"2","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Last name","value":"ddd"},
  {"id":"3","is_email":"true","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Email","value":"sample1@example.com"},
  {"id":"6","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Accomodation\/PikUp Address:","value":"House no 203"},
  {"id":"8","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Room Number:","value":"d"},
  {"id":"4","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Phone","value":"44444"},
  {"id":"5","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Message","value":"444444"}
] 
'];

var_dump($rows);

$names = [ 1 => 'firstname', 2 => 'lastname' ];
foreach($rows as &$row)
{
  $row = json_decode($row);
  $cols = [];
  foreach($row as &$col)
  { 
    if(isset($names[$col->id]))
      $cols[$names[$col->id]] = $col;
  }
  $row = $cols;
}

var_dump($rows);

结果是:

array (size=2)
  0 => 
    array (size=2)
      'firstname' => 
        object(stdClass)[1]
          public 'id' => string '1' (length=1)
          public 'is_email' => string 'false' (length=5)
          public 'add_to_day_hour_info' => string 'false' (length=5)
          public 'add_to_day_hour_body' => string 'false' (length=5)
          public 'translation' => string 'First name' (length=10)
          public 'value' => string 'firstname1' (length=10)
      'lastname' => 
        object(stdClass)[2]
          public 'id' => string '2' (length=1)
          public 'is_email' => string 'false' (length=5)
          public 'add_to_day_hour_info' => string 'false' (length=5)
          public 'add_to_day_hour_body' => string 'false' (length=5)
          public 'translation' => string 'Last name' (length=9)
          public 'value' => string 'lastname1' (length=9)
  1 => 
    array (size=2)
      'firstname' => 
        object(stdClass)[5]
          public 'id' => string '1' (length=1)
          public 'is_email' => string 'false' (length=5)
          public 'add_to_day_hour_info' => string 'false' (length=5)
          public 'add_to_day_hour_body' => string 'false' (length=5)
          public 'translation' => string 'First name' (length=10)
          public 'value' => string 'ddd' (length=3)
      'lastname' => 
        object(stdClass)[4]
          public 'id' => string '2' (length=1)
          public 'is_email' => string 'false' (length=5)
          public 'add_to_day_hour_info' => string 'false' (length=5)
          public 'add_to_day_hour_body' => string 'false' (length=5)
          public 'translation' => string 'Last name' (length=9)
          public 'value' => string 'ddd' (length=3)
①如果本文未解决您的问题,请点击查看与本文相关的问题
②如果本文未解决您的问题,请向程序员专用AI小助手提问
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM