Is there any difference between foreach and for in php, especially in processing Multithreading (in this example, pushing IOS )?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.