首先,感谢您的关注!
运行查询后,我得到如下结果:
--------------------------------------
|post_id| meta_key | meta_value |
--------------------------------------
| 1 | name | jeff |
--------------------------------------
| 1 | email | x@mail.com |
--------------------------------------
| 1 | color | blue |
--------------------------------------
| 2 | name | mary |
--------------------------------------
| 2 | email | y@mail.com |
--------------------------------------
| 2 | color | pink |
--------------------------------------
数组
array(6)
{
[0]=> object(stdClass)#448 (3)
{
["post_id"] => string(1) "1"
["meta_key"] => string(4) "name"
["meta_value"] => string(4) "jeff"
}
[1]=> object(stdClass)#449 (3)
{
["post_id"] => string(1) "1"
["meta_key"] => string(4) "email"
["meta_value"] => string(10) "x@mail.com"
}
[2]=> object(stdClass)#450 (3)
{
["post_id"] => string(1) "1"
["meta_key"] => string(5) "color"
["meta_value"] => string(4) "blue"
}
[3]=> object(stdClass)#448 (3)
{
["post_id"] => string(1) "2"
["meta_key"] => string(4) "name"
["meta_value"] => string(4) "mary"
}
[4]=> object(stdClass)#449 (3)
{
["post_id"] => string(1) "2"
["meta_key"] => string(4) "email"
["meta_value"] => string(10) "y@mail.com"
}
[5]=> object(stdClass)#450 (3)
{
["post_id"] => string(1) "2"
["meta_key"] => string(5) "color"
["meta_value"] => string(4) "pink"
}
}
好吧,我有1个ID用于3个标签和3个值。使用PHP,我需要知道如何做foreach或每个打印这样的数据:
---------------------------------------------------
|post_id| name | email | color |
---------------------------------------------------
| 1 | jeff | x@mail.com | blue |
---------------------------------------------------
| 2 | mary | y@mail.com | pink |
---------------------------------------------------
现在,在Kostas Mitsarakis的帮助下,我有一个代码,但在我的例子中,这个代码返回NULL值。
代码:
global $wpdb;
$meta_value = interested_get_meta( 'interested_email' );
$results = $wpdb->get_results(
'SELECT post_id, meta_key, meta_value
FROM wp_postmeta
WHERE post_id
IN ( SELECT post_id
FROM wp_postmeta
WHERE meta_value = "'.$meta_value.'"
)', OBJECT
);
$data = array();
$result = array($results);
foreach($result as $key => $value) {
if (!in_array($value['post_id'], $data)) {
$data[$value['post_id']]['post_id'] = $value['post_id'];
}
if ($value['meta_key'] != 'post_id') {
$data[$value['post_id']][$value['meta_key']] = $value['meta_value'];
}
}
var_dump($data);
var_dump的回报:
array(1) { [""]=> array(2) { ["post_id"]=> NULL [""]=> NULL } }
答案 0 :(得分:1)
您可以使用简单的foreach转换数组:
$output = array();
foreach ($input as $data) {
$output[$data['id']]['id'] = $data['id'];
$output[$data['id']][$data['label']] = $data['value'];
}
答案 1 :(得分:0)
我同意@KostasMitsarakis在上面的评论中说这可能在sql级别上做得更好。
但是这里是您可能正在寻找的PHP代码:
<?php
$input = [
[
'id' => 1,
'name' => 'jeff',
'email' => 'x@mail.com',
'color' => 'blue',
],
[
'id' => 2,
'name' => 'mary',
'email' => 'y@mail.c',
'color' => 'pink',
]
];
$output = [];
foreach ($input as $entry) {
$element = [];
foreach($entry as $key=>$val) {
$element[$key] = $val;
}
$output[] = $element;
}
var_dump($output);
这个的输出是:
array(2) {
[0] =>
array(4) {
'id' =>
int(1)
'name' =>
string(4) "jeff"
'email' =>
string(10) "x@mail.com"
'color' =>
string(4) "blue"
}
[1] =>
array(4) {
'id' =>
int(2)
'name' =>
string(4) "mary"
'email' =>
string(8) "y@mail.c"
'color' =>
string(4) "pink"
}
}
答案 2 :(得分:0)
尝试使用ARRAY_A将结果作为数组。
$meta_value = interested_get_meta( 'interested_email' );
$results = $wpdb->get_results(
'SELECT post_id, meta_key, meta_value
FROM wp_postmeta
WHERE post_id
IN ( SELECT post_id
FROM wp_postmeta
WHERE meta_value = "'.$meta_value.'"
)', ARRAY_A
);
$data = array();
$result = json_decode($results , true); //Return results as array
foreach($result as $key => $value) {
if (!in_array($value['post_id'], $data)) {
$data[$value['post_id']]['post_id'] = $value['post_id'];
}
if ($value['meta_key'] != 'post_id') {
$data[$value['post_id']][$value['meta_key']] = $value['meta_value'];
}
}
echo 'id name email color <br />';
foreach($data as $key => $value) {
echo $value['post_id'].' '.$value['name'].' '.$value['email'].' '.$value['color'].'<br />';
}
var_dump($data);
结果(表格)
id name email color
1 jeff x@mail.com blue
2 mary y@mail.com pink
结果(var_dump)
array (size=2)
1 =>
array (size=4)
'post_id' => int 1
'name' => string 'jeff' (length=4)
'email' => string 'x@mail.com' (length=10)
'color' => string 'blue' (length=4)
2 =>
array (size=4)
'post_id' => int 2
'name' => string 'mary' (length=4)
'email' => string 'y@mail.com' (length=10)
'color' => string 'pink' (length=4)
答案 3 :(得分:0)
好吧,经过几个小时和咖啡,在这里写的所有人的帮助下,我找到了解决方案。
var dataString = $('#appForm').serialize(); //alert (dataString);return false;
$.ajax({
type: "POST",
url: "application_form_params.php",
data: dataString,
dataType: 'json',
success: function (data) {
window.location.reload();
$('.register-alert').html("You have successfully registered an applicant with rand " + data.rand);
}
});
感谢所有以任何方式提供帮助的人!