Как проверить ключи массива в php для хранения значений массива в mysql

Как проверить ключи массива в php для хранения значений массива в mysql. Я пытаюсь сохранить «id», если массив имеет («сообщение») AND («сообщение» и «история»).

массив

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' => 'RIP 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 || RIP Jaihind ----------------------------------------------------------------- 

Вышеуказанный идентификатор будет связан с сообщением, если и только если в массиве существует «сообщение».

Надеюсь, я правильно понял вопрос.

здесь вы можете удалить некоторые строки кода (не включена проверка достоверности)

 $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!!!!!RIP 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!!!!! RIP 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!!!!!RIP JaihindJohnson & Johnson Finally Admits: Their Baby Products Contain Cancer-Causing Chemicals | | +-----------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+ 1 row in set (0,00 sec) mysql>