如何从数组中获取数据并插入数据库

时间:2022-09-26 08:17:19

Hello i Have a problem related to array,i am trying to request graph api and saves data in database.here is my array

你好我有一个与数组有关的问题,我正在尝试请求图api并将数据保存在database.here是我的数组

Array
(
[threadId] => t_mid.1445648572476:316f06a4adc6d7dd57
[sender] => Array
    (
        [0] => Array
            (
                [0] => stdClass Object
                    (
                        [name] => rajesh Prasad Shah
                        [email] => 543634645764577@facebook.com
                        [id] => 543634645764577
                    )

                [1] => stdClass Object
                    (
                        [name] => rjtest
                        [email] => 849929567895403@facebook.com
                        [id] => 849929567895403
                    )

            )

    )

[messages] => Array
    (
        [messages] => Array
            (
                [0] => Array
                    (
                        [message] => Same to u dear.
                        [from] => stdClass Object
                            (
                                [name] => rajesh Prasad Shah
                                [email] => 543634645764577@facebook.com
                                [id] => 543634645764577
                            )

                        [created_time] => 2015-12-31T11:31:24+0000
                        [id] => m_mid.1451561484487:5b3276ddd9d86d1480
                        [to] => stdClass Object
                            (
                                [data] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [name] => rjtest
                                                [email] => 849929567895403@facebook.com
                                                [id] => 849929567895403
                                            )

                                    )

                            )

                        [tags] => stdClass Object
                            (
                                [data] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [name] => inbox
                                            )

                                        [1] => stdClass Object
                                            (
                                                [name] => source:chat
                                            )

                                    )

                            )

                    )

                [1] => Array
                    (
                        [message] => Happy New Year to all of you
                        [from] => stdClass Object
                            (
                                [name] => rjtest
                                [email] => 849929567895403@facebook.com
                                [id] => 849929567895403
                            )

                        [created_time] => 2015-12-31T10:37:47+0000
                        [id] => m_mid.1451558267514:4c017e5a1f8e997586
                        [to] => stdClass Object
                            (
                                [data] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [name] => rajesh Prasad Shah
                                                [email] => 543634645764577@facebook.com
                                                [id] => 543634645764577
                                            )

                                    )

                            )

                        [tags] => stdClass Object
                            (
                                [data] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [name] => inbox
                                            )

                                        [1] => stdClass Object
                                            (
                                                [name] => read
                                            )

                                        [2] => stdClass Object
                                            (
                                                [name] => sent
                                            )

                                        [3] => stdClass Object
                                            (
                                                [name] => source:web
                                            )

                                    )

                            )

                    )

                [2] => Array
                    (
                        [message] => Happy New Year to all of you
                        [from] => stdClass Object
                            (
                                [name] => rjtest
                                [email] => 849929567895403@facebook.com
                                [id] => 849929567895403
                            )

                        [created_time] => 2015-12-31T09:44:49+0000
                        [id] => m_mid.1451555089630:1036078c7bcabeed78
                        [to] => stdClass Object
                            (
                                [data] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [name] => rajesh Prasad Shah
                                                [email] => 543634645764577@facebook.com
                                                [id] => 543634645764577
                                            )

                                    )

                            )

                        [tags] => stdClass Object
                            (
                                [data] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [name] => inbox
                                            )

                                        [1] => stdClass Object
                                            (
                                                [name] => read
                                            )

                                        [2] => stdClass Object
                                            (
                                                [name] => sent
                                            )

                                        [3] => stdClass Object
                                            (
                                                [name] => source:web
                                            )

                                    )

                            )

                    )

                [3] => Array
                    (
                        [message] => Happy New Year to all of you
                        [from] => stdClass Object
                            (
                                [name] => rjtest
                                [email] => 849929567895403@facebook.com
                                [id] => 849929567895403
                            )

                        [created_time] => 2015-12-31T09:43:28+0000
                        [id] => m_mid.1451555008621:62c3365e35ededc773
                        [to] => stdClass Object
                            (
                                [data] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [name] => rajesh Prasad Shah
                                                [email] => 543634645764577@facebook.com
                                                [id] => 543634645764577
                                            )

                                    )

                            )

                        [tags] => stdClass Object
                            (
                                [data] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [name] => inbox
                                            )

                                        [1] => stdClass Object
                                            (
                                                [name] => read
                                            )

                                        [2] => stdClass Object
                                            (
                                                [name] => sent
                                            )

                                        [3] => stdClass Object
                                            (
                                                [name] => source:web
                                            )

                                    )

                            )

                    )

                [4] => Array
                    (
                        [message] => Very Good
                        [from] => stdClass Object
                            (
                                [name] => rajesh Prasad Shah
                                [email] => 543634645764577@facebook.com
                                [id] => 543634645764577
                            )

                        [created_time] => 2015-09-01T09:13:34+0000
                        [id] => m_mid.1445648574720:c2746f8b55fff17369
                        [to] => stdClass Object
                            (
                                [data] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [name] => rjtest
                                                [email] => 849929567895403@facebook.com
                                                [id] => 849929567895403
                                            )

                                    )

                            )

                        [tags] => stdClass Object
                            (
                                [data] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [name] => inbox
                                            )

                                        [1] => stdClass Object
                                            (
                                                [name] => read
                                            )

                                        [2] => stdClass Object
                                            (
                                                [name] => source:chat
                                            )

                                    )

                            )

                    )

                [5] => Array
                    (
                        [message] => Good
                        [from] => stdClass Object
                            (
                                [name] => rajesh Prasad Shah
                                [email] => 543634645764577@facebook.com
                                [id] => 543634645764577
                            )

                        [created_time] => 2015-09-01T09:13:32+0000
                        [id] => m_mid.1445648572476:316f06a4adc6d7dd57
                        [to] => stdClass Object
                            (
                                [data] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [name] => rjtest
                                                [email] => 849929567895403@facebook.com
                                                [id] => 849929567895403
                                            )

                                    )

                            )

                        [tags] => stdClass Object
                            (
                                [data] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [name] => inbox
                                            )

                                        [1] => stdClass Object
                                            (
                                                [name] => read
                                            )

                                        [2] => stdClass Object
                                            (
                                                [name] => source:chat
                                            )

                                    )

                            )

                    )

            )

    )

)

This is my php code from that i fetch data

这是我获取数据的PHP代码

foreach($data as $evaluatemessage){
   //echo "<pre>";print_r($evaluatemessage); die;
 $inbox=array();
  $inbox['thread_id']=$evaluatemessage['threadId'];
  foreach($evaluatemessage['messages'] as $messagedet){
    //print_r($messagedet[0]['id']); die;
  $inbox['smg_id']=$messagedet[$j]['id'];
  $inbox['body']=$messagedet[$j]['message'];
  $inbox['time']=$messagedet[$j]['created_time'];

  //$inbox['msg_type']=$data;
 //$inbox['user_id']=$data;
  $inbox['From_name']=$messagedet[$j]['from']->name;
   $inbox['To_name']=$messagedet[$j]['to']->data[0]->name;
   $inbox['From_id']=$messagedet[$j]['from']->id;
   $inbox['To_id']=$messagedet[$j]['to']->data[0]->id;


  $inbox['tags']=$messagedet[$j]['tags']->data[1]->name;
 //$inbox['Page_id']=$data;
 //echo "<pre>";print_r($inbox); die;
 $inbox['Page_id']='12345';
   $this->facebook_inboxes->save($inbox);
   $j++;
  }


 $i++;
 }

when i hit with this code only a single threadid and single message contents are saved.but rest are not,i want every thread id with there all messages id and message body are saved row by row but unable to do that can anyone help me how i do that.

当我用这个代码命中时只保存一个threadid和单个消息内容。但是其余的不是,我希望每个线程id都有所有消息id和消息体一行一行地保存但是无法做到这一点任何人都可以帮助我我这样做。

2 个解决方案

#1


2  

Write this code before Save function

在保存功能之前写下此代码

$this->facebook_inboxes->create();

This is enbuild function of cake to store multiple row.

这是蛋糕存储多行的结构功能。

#2


0  

There is another messages array inside the messages array. So according to your array structure, your second loop will be like this :

messages数组中还有另一个消息数组。所以根据你的数组结构,你的第二个循环将是这样的:

foreach($evaluatemessage['messages']['messages'] as $messagedet){

  //print_r($messagedet[0]['id']); die;
  $inbox['smg_id']=$messagedet[$j]['id'];
  $inbox['body']=$messagedet[$j]['message'];
  $inbox['time']=$messagedet[$j]['created_time'];

.
.
.

}

and you are using only:

而你只是使用:

foreach($evaluatemessage['messages'] as $messagedet){}

here is the full code :

这是完整的代码:

foreach($data as $evaluatemessage){
   //echo "<pre>";print_r($evaluatemessage); die;
 $inbox=array();
  $inbox['thread_id']=$evaluatemessage['threadId'];
  foreach($evaluatemessage['messages']['messages'] as $messagedet){
    //print_r($messagedet[0]['id']); die;
  $inbox['smg_id']=$messagedet[$j]['id'];
  $inbox['body']=$messagedet[$j]['message'];
  $inbox['time']=$messagedet[$j]['created_time'];

  //$inbox['msg_type']=$data;
 //$inbox['user_id']=$data;
  $inbox['From_name']=$messagedet[$j]['from']->name;
   $inbox['To_name']=$messagedet[$j]['to']->data[0]->name;
   $inbox['From_id']=$messagedet[$j]['from']->id;
   $inbox['To_id']=$messagedet[$j]['to']->data[0]->id;


  $inbox['tags']=$messagedet[$j]['tags']->data[1]->name;
 //$inbox['Page_id']=$data;
 //echo "<pre>";print_r($inbox); die;
 $inbox['Page_id']='12345';
   $this->facebook_inboxes->save($inbox);
   $j++;
  }


 $i++;
 }

#1


2  

Write this code before Save function

在保存功能之前写下此代码

$this->facebook_inboxes->create();

This is enbuild function of cake to store multiple row.

这是蛋糕存储多行的结构功能。

#2


0  

There is another messages array inside the messages array. So according to your array structure, your second loop will be like this :

messages数组中还有另一个消息数组。所以根据你的数组结构,你的第二个循环将是这样的:

foreach($evaluatemessage['messages']['messages'] as $messagedet){

  //print_r($messagedet[0]['id']); die;
  $inbox['smg_id']=$messagedet[$j]['id'];
  $inbox['body']=$messagedet[$j]['message'];
  $inbox['time']=$messagedet[$j]['created_time'];

.
.
.

}

and you are using only:

而你只是使用:

foreach($evaluatemessage['messages'] as $messagedet){}

here is the full code :

这是完整的代码:

foreach($data as $evaluatemessage){
   //echo "<pre>";print_r($evaluatemessage); die;
 $inbox=array();
  $inbox['thread_id']=$evaluatemessage['threadId'];
  foreach($evaluatemessage['messages']['messages'] as $messagedet){
    //print_r($messagedet[0]['id']); die;
  $inbox['smg_id']=$messagedet[$j]['id'];
  $inbox['body']=$messagedet[$j]['message'];
  $inbox['time']=$messagedet[$j]['created_time'];

  //$inbox['msg_type']=$data;
 //$inbox['user_id']=$data;
  $inbox['From_name']=$messagedet[$j]['from']->name;
   $inbox['To_name']=$messagedet[$j]['to']->data[0]->name;
   $inbox['From_id']=$messagedet[$j]['from']->id;
   $inbox['To_id']=$messagedet[$j]['to']->data[0]->id;


  $inbox['tags']=$messagedet[$j]['tags']->data[1]->name;
 //$inbox['Page_id']=$data;
 //echo "<pre>";print_r($inbox); die;
 $inbox['Page_id']='12345';
   $this->facebook_inboxes->save($inbox);
   $j++;
  }


 $i++;
 }