我正在尝试合并三个不同的数组以实现自定义帖子类型,因此所有第一个项目都是一个对象,所有第二个对象都是另一个对象等。如果我的措辞不正确,请原谅我。我是 MySQL 新手。最终,这将成为网站访问者可以输入他们的地址并找到离他们最近的企业的表格,但我们不要超前。这是我所拥有的:
<?php
if(isset($_POST['submit'])) {
$address = $_POST['address']; $conn= mysqli_connect('localhost', 'username', 'password', 'database');
if($conn) {
$streets = $wpdb->get_results("SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE 'street address' ORDER BY `wp_postmeta`.`post_id` ASC");
$postmeta = $wpdb->get_results("SELECT * FROM `wp_posts` WHERE `post_type` LIKE 'location' ORDER BY `wp_posts`.`ID` ASC");
$geos = $wpdb->get_results("SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE 'geolocation' ORDER BY `wp_postmeta`.`post_id` ASC");
$results = array_merge_recursive($geos, $streets, $postmeta);
if ( !empty( $results) ) { echo '<pre>'; echo print_r($results); echo '</pre>';}}
else {die('connection failed');} }
?>
array_merge_recursive
只是堆叠结果:
Array
(
[0] => stdClass Object
(
[meta_id] => 28837
[post_id] => 25347
[meta_key] => geolocation
[meta_value] => manchgeo
)
[1] => stdClass Object
(
[meta_id] => 28835
[post_id] => 25348
[meta_key] => geolocation
[meta_value] => dovergeo
)
[2] => stdClass Object
(
[meta_id] => 30442
[post_id] => 25347
[meta_key] => street address
[meta_value] => 21 Manchester St.
)
[3] => stdClass Object
(
[meta_id] => 30820
[post_id] => 25348
[meta_key] => street address
[meta_value] => 213 Dover St.
)
[4] => stdClass Object
(
[ID] => 25347
[post_author] => 4
[post_date] => 2023-11-09 15:19:04
[post_date_gmt] => 2023-11-09 20:19:04
[post_content] =>
[post_title] => Manchester
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => mancheter
[to_ping] =>
[pinged] =>
[post_modified] => 2023-11-22 16:02:18
[post_modified_gmt] => 2023-11-22 21:02:18
[post_content_filtered] =>
[post_parent] => 0
[guid] => https://flexhuboffice.wpengine.com/?post_type=location&p=25347
[menu_order] => 0
[post_type] => location
[post_mime_type] =>
[comment_count] => 0
)
[5] => stdClass Object
(
[ID] => 25348
[post_author] => 4
[post_date] => 2023-11-09 15:19:45
[post_date_gmt] => 2023-11-09 20:19:45
[post_content] =>
[post_title] => Dover
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => dover
[to_ping] =>
[pinged] =>
[post_modified] => 2023-11-22 16:02:01
[post_modified_gmt] => 2023-11-22 21:02:01
[post_content_filtered] =>
[post_parent] => 0
[guid] => https://flexhuboffice.wpengine.com/?post_type=location&p=25348
[menu_order] => 0
[post_type] => location
[post_mime_type] =>
[comment_count] => 0
)
)
如有任何帮助,我们将不胜感激!也欢迎任何关于如何更好地编码的建议。
array_merge
和array_merge_recusive
都返回相同的东西。
<?php
if (isset($_POST['submit'])) {
$address = $_POST['address'];
$conn = mysqli_connect('localhost', 'username', 'password', 'database');
if ($conn) {
$streets = $wpdb->get_results("SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE 'street address' ORDER BY `wp_postmeta`.`post_id` ASC");
$postmeta = $wpdb->get_results("SELECT * FROM `wp_posts` WHERE `post_type` LIKE 'location' ORDER BY `wp_posts`.`ID` ASC");
$geos = $wpdb->get_results("SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE 'geolocation' ORDER BY `wp_postmeta`.`post_id` ASC");
$results = [];
foreach ($streets as $street) {
$results[$street->post_id]['street_address'] = $street->meta_value;
}
foreach ($postmeta as $post) {
$results[$post->ID]['post'] = $post;
}
foreach ($geos as $geo) {
$results[$geo->post_id]['geolocation'] = $geo->meta_value;
}
if (!empty($results)) {
echo '<pre>';
print_r($results);
echo '</pre>';
}
} else {
die('connection failed');
}
}
?>
在此示例中使用帖子 ID 作为 $results 数组的键。该代码单独运行每个数组,根据帖子 ID 用数据填充 $results 数组。结果,您将获得一个结构化数组,其中每个项目都包含相同帖子 ID 的街道地址、帖子元数据和地理位置。更改代码以满足您的需求和数据结构。