gpt4 book ai didi

php - 如何检查php中的数组键以将数组值存储在mysql中

转载 作者:行者123 更新时间:2023-11-30 22:25:08 25 4
gpt4 key购买 nike

如何检查 php 中的数组键以将数组值存储在 mysql 中。如果数组具有 ("message") AND ("message"AND "story") 键,我将尝试存储“id”。

数组

array (
0 =>
array (
'message' => 'Insane!!!!!',
'story' => 'Ankan shared TIMES NOW\'s video.',
'created_time' =>
array (
'date' => '2016-02-16 17:15:49.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154555849367892',
),
1 =>
array (
'story' => 'Ankan shared Filmydrama\'s video.',
'created_time' =>
array (
'date' => '2016-02-12 14:05:15.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154545385862892',
),
2 =>
array (
'story' => 'Ankan shared Kya Yehi Hain Acche Din?\'s video.',
'created_time' =>
array (
'date' => '2016-02-12 03:34:32.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154544563382892',
),
3 =>
array (
'story' => 'Ankan shared a link.',
'created_time' =>
array (
'date' => '2016-02-12 03:28:09.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154544555572892',
),
4 =>
array (
'message' => 'R.I.P Jaihind',
'story' => 'Ankan shared The Hindu\'s post.',
'created_time' =>
array (
'date' => '2016-02-11 07:46:59.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154542597202892',
),
5 =>
array (
'story' => 'Ankan posted from Change.org.',
'created_time' =>
array (
'date' => '2016-02-11 05:09:08.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154542373792892',
),
6 =>
array (
'message' => 'Johnson & Johnson Finally Admits: Their Baby Products Contain Cancer-Causing Chemicals | ',
'created_time' =>
array (
'date' => '2016-02-11 01:38:33.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154542027992892',
),
7 =>
array (
'story' => 'Ankan shared a link.',
'created_time' =>
array (
'date' => '2016-02-09 17:16:07.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154538723082892',
),

我尝试了以下代码,但它没有提供任何东西。

$total_posts = array();
$posts_response = $posts_request->getGraphEdge();
if($fb->next($posts_response)) {
$response_array = $posts_response->asArray();
$total_posts = array_merge($total_posts, $response_array);
while ($posts_response = $fb->next($posts_response)) {
$response_array = $posts_response->asArray();
$total_posts = array_merge($total_posts, $response_array);
}
$array = json_encode($total_posts, true);

} else {
$posts_response = $posts_request->getGraphEdge()->asArray();
print_r($posts_response);
}


$my_arr = json_decode($array, true);

ob_start();
$data = var_export($my_arr);
ob_end_clean();


/*$story = $data;
foreach( $my_arr as $row )

$story .= " $row[message]\n";

$message_id = $data;
foreach( $my_arr as $row )

$message_id .= " $row[id]\n";*/


$total_posts[0] = array('message', 'story', 'created_time', array('date', 'timezone_type', 'timezone'),'id');


foreach ($total_posts as $row) {
if (isset($row['story']) {
if (isset($row['message']) {
$message_id .= "$row[id]\n";
$story .= " $row[message]\n";
} else {
// only story
}
} else {
$message_id .= "$row[id]\n";
$story .= " $row[message]\n";
}
}

我想得到这样的输出

-----------------------------------------------------------------
id | Messages
-----------------------------------------------------------------
10154521329397892_10154555849367892 | Insane!!!!!
10154521329397892_10154542597202892 || R.I.P Jaihind
-----------------------------------------------------------------

当且仅当数组中存在“message”键时,上述 id 才会与消息相关联。

最佳答案

我希望我已经正确理解了这个问题。

这里你可以删除一些代码行(不包括有效性检查)

$total_posts = array();
$posts_response = $posts_request->getGraphEdge();
do {
$response_array = $posts_response->asArray();
$total_posts = array_merge($total_posts, $response_array);
} while ($posts_response = $fb->next($posts_response))
$array = json_encode($total_posts, true);
ini_set('display_errors', true);
try {
$stmt = $db->prepare("insert into `<table>` (`id`,`message`) values(?,?);");
$message_ids = array();
$stories = array();
foreach ($total_posts as $row) {
if (isset($row['message'])) {
// any record with 'message' is allowed
// whenever has it 'story' or no
$message_ids[] = $row['id'];
$stories[] = $row['message'];

} else {
if (isset($row['story'])) {
// only story
} else {
// no message & no story
}
}
}

$message_id = implode("\n",$message_ids);
$story = implode("\n",$stories);
$stmt->bind_param("ss", $message_id, $story);
if ($stmt->execute()) {
// success
} else {
// error handler
$error = $stmt->error;
// debug
echo $error.PHP_EOL;
die();
}

// or output to console
// echo 'id: '.$message_id.PHP_EOL;
// echo 'message: '.$story.PHP_EOL;
// echo PHP_EOL;
} catch (\Exception $e) {
// debug
echo $e->getMessage();
}

希望对您有所帮助。

更新结果:

id: 10154521329397892_1015455584936789210154521329397892_1015454259720289210154521329397892_10154542027992892
msg: Insane!!!!!R.I.P JaihindJohnson & Johnson Finally Admits: Their Baby Products Contain Cancer-Causing Chemicals |

我对数据库没有任何问题:

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
2 rows in set (0,00 sec)

mysql> use test;
Database changed

mysql> create table `test` (`id` varchar(128), `msg` varchar(128));
Query OK, 0 rows affected (0,10 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test |
+----------------+
1 row in set (0,00 sec)

mysql> explain `test`;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | varchar(128) | YES | | NULL | |
| msg | varchar(128) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0,01 sec)

mysql>

/// console
[65] $ php ./a.php
id: 10154521329397892_10154555849367892
10154521329397892_10154542597202892
10154521329397892_10154542027992892
msg: Insane!!!!!
R.I.P Jaihind
Johnson & Johnson Finally Admits: Their Baby Products Contain Cancer-Causing Chemicals |

/// back to mysql shell
mysql> select * from `test`;
+-----------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+
| id | msg |
+-----------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+
| 10154521329397892_1015455584936789210154521329397892_1015454259720289210154521329397892_10154542027992892 | Insane!!!!!R.I.P JaihindJohnson & Johnson Finally Admits: Their Baby Products Contain Cancer-Causing Chemicals | |
+-----------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+
1 row in set (0,00 sec)

mysql>

关于php - 如何检查php中的数组键以将数组值存储在mysql中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35487882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com