如何检查php中的数组键以在mysql中存储数组值。我正在尝试存储" id"如果数组有(" message")AND(" message" AND" story")key。
阵列
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才会与消息相关联。 key存在于数组中。
答案 0 :(得分:0)
我希望我能正确理解这个问题。
这里你可以删除一些代码行(不包括有效性检查)
$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>