php - Combine 3 array into one foreach loop -
i seek success when combine 2 array 1 foreach loop ..
<?php //var $phone_prefix_array = $_post['phone_prefix']; //prefix eg. 60 (for country code) $phone_num_array = $_post['phone']; $c_name_array = $_post['customer_name']; foreach (array_combine($phone_prefix_array, $phone_num_array) $phone_prefix => $phone_num) { //combine prefix array , phone number array $phone_var = $phone_prefix . $phone_num; $phone = '6' . $phone_var; if ($phone_prefix == true && $phone_num == true) { //filter if no prefix number dont show echo $phone; //customer_name_here } else { } } ?>
the result should :
60125487541 jake 60355485541 kane 60315488745 ray 63222522125 josh
but im not sure how combine array $c_name_array
foreach lopp
php version : 5.2.17
array_combine
terrible workaround case , not work if value in first array not valid key (i.e. not int or string)
php 5.3+ has multipleiterator
this:
$iterator = new multipleiterator(); $iterator->attachiterator(new arrayiterator($phone_prefix_array)); $iterator->attachiterator(new arrayiterator($phone_num_array)); foreach ($iterator $current) { $phone_prefix = $current[0]; $phone_num = $current[1]; // ... }
since php 5.4 can write loop more concise:
foreach ($iterator list($phone_prefix, $phone_num)) { // ... }
php
No comments:
Post a Comment