所以我试图用php(wordpress)
来回显mysql表中的值大多数人都表现得很好
但是当我试图回应
时$ row->表单我得到一个像这样返回的数组:
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;
}
我希望回应第一个名字。感谢
答案 0 :(得分:0)
echo $row->form;
上面的行将回显数组。
仅回显名字使用,如下面的模式
echo $row->form['first_name'];
答案 1 :(得分:0)
如果您找不到合适的解决方案,这只是一种解决方法。您应该尝试使用get_var()
或get_col()
之类的内容。咨询wordpress reference。
<?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)