Is there any difference between foreach and for in php, especially in processing Multithreading (in this example, pushing IOS? I encountered a problem and solved it for a long time. Finally, I found that it was easy to use for & nbsp; or foreach. & Lt ;? Php & nbsp; the loop has a problem. require_once & nbsp; homebaeapp1_dedb. php; & nbsp; $ Is there any difference between foreach and for in db php, especially in processing Multithreading (in this example, pushing IOS?
I encountered a problem and solved it for a long time. Finally, I found that it was difficult to use for, and I used foreach.
// Loop error
Require_once "/home/bae/app/include/db. php ";
$ Db_obj = new DB ();
$ SQL = "select appleID from appleDevice ";
$ Result = $ db_obj-> db_array ($ SQL );
Print_r ("the array is:". $ result );
Var_dump ($ result );
$ CountArray = count ($ result );
Print_r ("number:". $ countArray );
$ Passphrase = 'zhaojian ';
$ Message = 'My first push notification! ';
$ Ctx = stream_context_create ();
Stream_context_set_option ($ ctx, 'SSL ', 'local _ cert', 'CK. pem ');
Stream_context_set_option ($ ctx, 'SSL ', 'passphrase', $ passphrase );
// Open a connection to the APNS server
$ Fp = stream_socket_client (
'SSL: // gateway.sandbox.push.apple.com: 8080', $ err,
$ Errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ ctx );
If (! $ Fp)
Exit ("Failed to connect: $ err $ errstr". PHP_EOL );
Echo 'connectedto apns'. PHP_EOL;
// Create the payload body
$ Body ['aps '] = array (
'Alert '=> $ message,
'Sound' => 'default'
);
// Encode the payload as JSON
$ Payload = json_encode ($ body );
// $ Result = array ("success", "success ");
Foreach ($ result as $ device ){
$ DeviceToken = $ device ['appleid'];
Print_r ("the". $ I. "item:". $ deviceToken );
$ Msg = chr (0 ). pack ('N', 32 ). pack ('H * ', $ deviceToken ). pack ('N', strlen ($ payload )). $ payload;
$ Result = fwrite ($ fp, $ msg, strlen ($ msg ));
If (! $ Result)
Echo 'message not delivered'. PHP_EOL;
Else
Echo 'message successfully delivered'. PHP_EOL;
}
Fclose ($ fp );
?>
This code is the code pushed to IOS by php.
Use foreach. if it is changed to for I = 0 I <长度,那样,print_r("the ".$i." item :".$deviceToken);这里就只能打印出数组第一条内容(我确定如果抛开这个代码单独打印数组是可以打印出来所有内容的),如果用foreach则都能打印。。
------ Solution --------------------
No way to write your for loop
But you have foreach
Foreach ($ result as $ device ){
$ Result = fwrite ($ fp, $ msg, strlen ($ msg ));
Changed the original volume
If the for loop is the same, it will not work.
Foreach loops the array reference.