how to merge array with the same value in smarty? - arrays

i have two arrays and i want two merge using the same value within each array..
the first array is :
[0] => Array
(
[id] => 1
[uid] => 0090000157
[cid] => 0090000007
[extension] => 202
[secret] => Myojyo42f!
[leader] => 1
[simultaneous] =>
[confbridge_id] => 2
[created_at] => 2015-07-26 12:20:20
[updated_at] => 2015-07-26 12:20:20
)
[1] => Array
(
[id] => 2
[uid] => 0090000159
[cid] => 0090000007
[extension] =>
[secret] => Myojyo42f!
[leader] =>
[simultaneous] =>
[confbridge_id] => 2
[created_at] => 2015-07-26 14:23:41
[updated_at] => 2015-07-26 14:23:41
)
)
and the second array is:
Array
(
[0] => Array
(
[_id] => 55b52f4c2bab38fc63b6272a
[event] => ConfbridgeJoin
[channel] => SIP/peer_voip301confbridge-0000001b
[uniqueid] => 1437937478.63
[conference] => 0090000156
[calleridnum] => 0090000157
[calleridname] => 0090000157
[__v] => 0
[sipSetting] => Array
(
[accountcode] =>
[accountcode_naisen] => 202
[extentype] => 0
[extenrealname] =>
[name] => 0090000157
[secret] => Myojyo42f!
[username] => 0090000157
[context] => innercall_xdigit
[gid] => 101
[cid] => 0090000007
)
)
[1] => Array
(
[_id] => 55b53a2e2bab38fc63b6272b
[event] => ConfbridgeJoin
[channel] => SIP/peer_voip301confbridge-0000001c
[uniqueid] => 1437940260.66
[conference] => 0090000156
[calleridnum] => 0090000158
[calleridname] => UID2
[__v] => 0
[sipSetting] => Array
(
[accountcode] =>
[accountcode_naisen] => 203
[extentype] => 0
[extenrealname] =>
[name] => 0090000158
[secret] => Myojyo42f!
[username] => 0090000158
[context] => innercall_xdigit
[gid] => 101
[cid] => 0090000007
)
)
)
i want to merge array with the same value for example :
first array has = [uid] => 0090000157
second array has = [calleridnum] => 0090000157
is it possible to merge them?
this is my code
{foreach from=$participants item=participant key=p }
{foreach from=$conference_participants item=conference_participant key=c}
{if$participants.calleridnum == $conference_participants.uid}
//how to get data here ?
{/if}
{/foreach}
{/foreach}

Could this be what you're looking for?
Sorry for all the changes, I should have tested it... grr
(I've put this in PHPFiddle:
<pre>
<?php
$participants = [
[ 'calleridnum' => 1,
'test' => 'yay'
]
];
$conferance_participants = [
[ 'uid' => 1,
'test' => 'yay2',
'dit' => 'deze'
]
];
foreach ($participants as $participant=>$p) {
foreach ($conferance_participants as $conferance_participant=>$c) {
if ($p['calleridnum'] == $c['uid']) {
// ID's match do the magic here
foreach ( $c as $key=>$val ) {
if (!isset($p[$key])) {
// Value is new, copy the conferance_participant to the participant
$participants[$participant][$key] = $val;
}
} // Merging data
} // If: Match
}
}
print_r( $participants );
?>
)

Related

Get data from 2 foreach array from 1st array i want to get name and from second Indexes

I have two Arrays 1st Array give me a role form Roles table and second array give me the data using this role_id key.
When i pass the array to view how can i get Role that is from first foreach().
This is my
SettingController.php
public function index()
{
$users = User::with('roles')->get()->all();
$access_names = AccessName::all();
$modules = Module::all();
$entries = Entry::all();
$general_settings = Setting::all()->first();
$module_permissions = ModulePermission::get()->toArray();
$roles = Role::get();
$num_roles = count($roles);
$data = 'roles';
foreach ($roles as $role){
foreach ($module_permissions as $permissions){
if($role['id'] == $permissions['role_id']){
$roles_data[$role['name']][] = $permissions;
}
}
}
return view('backend.setting.index', compact(['general_settings', 'roles', 'users', 'access_names', 'roles_data','modules','entries']));
}
My output of this code is like this
Array
(
[Admin] => Array
(
[1] => Array
(
[id] => 179
[user_id] => 1
[role_id] => 1
[access_name_id] => 2
[module_permission_id] => 1
[entry_type] => own
[status] => 0
[created_at] => 2019-08-22 04:45:14
[updated_at] => 2019-08-22 04:45:14
)
)
[User] => Array
(
[0] => Array
(
[id] => 206
[user_id] => 2
[role_id] => 2
[access_name_id] => 1
[module_permission_id] => 1
[entry_type] => own
[status] => 1
[created_at] => 2019-08-22 05:09:40
[updated_at] => 2019-08-22 05:09:40
)
)
)
If i can access data from this then very well other wise i make a new array like this from i can access this by a new array that is data so it is easy to check that
I also tried this but this is not working properly
foreach ($roles as $role){
foreach ($module_permissions as $permissions){
if($role['id'] == $permissions['role_id']){
$roles_data[$role['name']][] = $permissions;
$array['data'] = $roles_data;
}
}
}
But If output come like this that is also very good
Array
(
[data]=>array
(
[Admin] => Array
(
[0] => Array
(
[id] => 179
[user_id] => 1
[role_id] => 1
[access_name_id] => 2
[module_permission_id] => 1
[entry_type] => own
[status] => 0
[created_at] => 2019-08-22 04:45:14
[updated_at] => 2019-08-22 04:45:14
)
)
[User] => Array
(
[0] => Array
(
[id] => 206
[user_id] => 2
[role_id] => 2
[access_name_id] => 1
[module_permission_id] => 1
[entry_type] => own
[status] => 1
[created_at] => 2019-08-22 05:09:40
[updated_at] => 2019-08-22 05:09:40
)
)
)
)
In view i want to do like this.
view.blade.php
if($role->name == 'data['Admin']'){
//then do this
}
elseif($role->name == 'data['User']'){
// do this
}
else{
// do this
}
And i also want to this dynamic what can i do to make dynamic i not use 'admin' and 'user' because if i have many roles it is difficult to handle.
Thanks.................

Using a variable outside PHP Foreach Loop

I am trying to output a username and user email outside of a foreach loop. I am trying to send an email to all WordPress users that are within a certain User Role.
Here is my code:
// Get users and their roles
$user_args = array(
'role__in' => 'new_role',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($user_args);
foreach ( $users as $user ) :
$user_name = $user->user_email;
$user_email = $user->display_name;
endforeach;
So I can then use them in other areas of the page, ie:.
echo 'Hello, ' . $user_name;
echo 'Send to: ' .$user_email;
I've printed out the $users array which contains the following:
Array
(
[0] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 46
[user_login] => huw
[user_pass] => u7CqxbuQDvApUAF6tT.
[user_nicename] => huw
[user_email] => huw#xxxx.co.uk
[user_url] =>
[user_registered] => 2017-02-06 11:13:09
[user_activation_key] => 1486379590:$P$BkisA4T5j1S/ZjRageafNYHfsdin1S0
[user_status] => 0
[display_name] => Huw Daniel Rowlands
)
[ID] => 46
[caps] => Array
(
[sssg] => 1
[new_role] => 1
[site_member] => 1
[test_role] => 1
)
[cap_key] => jciw_capabilities
[roles] => Array
(
[0] => sssg
[1] => new_role
[2] => site_member
[3] => test_role
)
[allcaps] => Array
(
[read] => 1
[sssg] => 1
[new_role] => 1
[site_member] => 1
[test_role] => 1
)
[filter] =>
)
[1] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 308
[user_login] => jeremy
[user_pass] => LLOKbkPOWQsBKUIk2qL1
[user_nicename] => magnus
[user_email] => jeremy#gmail.com
[user_url] =>
[user_registered] => 2017-05-03 19:24:42
[user_activation_key] => 1493839482:$P$BI/IYldCzsXZowLEiNfxiUkIwVdDKV0
[user_status] => 0
[display_name] => Jeremy
)
[ID] => 308
[caps] => Array
(
[new_role] => 1
[sssg] => 1
[site_member] => 1
)
[cap_key] => jciw_capabilities
[roles] => Array
(
[0] => new_role
[1] => sssg
[2] => site_member
)
[allcaps] => Array
(
[read] => 1
[new_role] => 1
[sssg] => 1
[site_member] => 1
)
[filter] =>
)
)
If you want to send an email to all users with a certain role, try something like this:
// Get users and their roles
$user_args = array(
'role__in' => 'new_role',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($user_args);
foreach ( $users as $user ) :
$user_email = $user->user_email;
$user_name = $user->display_name;
send_email($user_name, $user_email);
endforeach;
And then write your email function:
function send_email($name, $email) {
// Do email sending stuff here with $name & $email
}

combine two php array for json encode

Presently m working with CakePhp..i got some problem with combining two array and prepare for a json encoded array.. i used array_merge() property but its not working.. how can i encoded both of these two array..
M doing like this :
return json_encode(array_merge ($product_list,$price_list));
i have two php array as follows :
array 1:
Array
(
[0] => Array
(
[PriceList] => Array
(
[price_id] => 2
[price_name] => abc
[date_time] => 2015-07-06 16:22:56
[dealer_type] => Dealer
[purpose] => dealer
[status] => ACTIVE
)
)
[1] => Array
(
[PriceList] => Array
(
[price_id] => 3
[price_name] => xyz
[date_time] => 2015-07-06 16:22:56
[dealer_type] => Dealer
[purpose] => dealer
[status] => ACTIVE
)
)
)
array 2:
Array
(
[0] => Array
(
[Product] => Array
(
[cat_id] => 1
[subcat_id] => 3
[brand_id] => 1
[p_code] => PP12567
[name] => akai
[model_no] =>
[specification] => color tv
[color] =>
[quality] =>
[size] =>
[p_unavail] => 1
[demo_avail] => 0
[brochure] =>
[status] => active
)
[ProductPrice] => Array
(
[id] => 154
[p_code] => PP12567
[price_id] => 1
[quantity] => 233
[purchase_price] => 344.00
[selling_price] => 44.00
[discount_price] => 33.00
[tax] => 5.00
[datetime] => 2015-07-23 15:47:11
)
[ProductSubCategory] => Array
(
[subcat_id] => 3
[cat_id] => 1
[subcat_name] => samsung
[status] => active
)
[ProductCategory] => Array
(
[cat_id] => 1
[cat_name] => Electronics
[cat_type] => Product
[status] => active
)
)
[1] => Array
(
[Product] => Array
(
[cat_id] => 1
[subcat_id] => 4
[brand_id] => 1
[p_code] => PBC-676767
[name] => music
[model_no] => 33
[specification] =>
[color] =>
[quality] =>
[size] =>
[p_unavail] => 0
[demo_avail] => 0
[brochure] =>
[status] => active
)
[ProductPrice] => Array
(
[id] => 156
[p_code] => PBC-676767
[price_id] => 1
[quantity] => 767
[purchase_price] => 54.00
[selling_price] => 55.00
[discount_price] => 22.00
[tax] => 3.00
[datetime] => 2015-07-23 15:47:11
)
[ProductSubCategory] => Array
(
[subcat_id] => 4
[cat_id] => 1
[subcat_name] => sony
[status] => active
)
[ProductCategory] => Array
(
[cat_id] => 1
[cat_name] => Electronics
[cat_type] => Product
[status] => active
)
)
)
how to combine these two array into one and encoded into a json array..
You have to use array_merge but not directly on array1 and array2:
$toEncodeArray = [array_merge ($array1[0], $array2[0])] ;
If you only want the associative array (without the wrapping array), simply do:
$toEncodeArray = array_merge ($array1[0], $array2[0]) ;

How to use pagination with custom query in cakephp

I am working on Reporting of the Postal service Project and i wanted to display data with the help of pagination . i know we can overwrite pagination function in model . i have tried it but didn't work for me.
I have BillReport Model where i have created function. Below is my custom query which is showing Multiple Bills against one payment means Payment hasMany Bills. I can paginate Payment but i wanted to paginate Bill wise.
I have mapped the bills and payments in Billpaymentmap table and from that table i am getting array of below query.
I want to paginate related model which is associated with Payment . Payment Hasmany Bill.
$reversal = $this->query("SELECT Pay.*,C.id,C.name,U.id,U.username,Bill.account_no,Bill.id, Pay.*,PMap.data,PMap.paid_amount FROM dbo_payments As Pay INNER JOIN dbo_reverse_payments As RPay ON RPay.payment_id = Pay.id INNER JOIN dbo_payment_bill_maps As PMap ON PMap.payment_id = RPay.payment_id INNER JOIN dbo_users U ON U.id = Pay.cashier_id INNER JOIN dbo_clients As C ON C.id = U.client_id INNER JOIN dbo_bills As Bill ON Bill.id = PMap.bill_id WHERE Pay.reversal = 1 and $conditions ");
Array
(
[0] => Array
(
[Pay] => Array
(
[id] => 10
[location_id] => 133
[cashier_id] => 5
[client_id] => 2
[dealer_id] => 31
[receipt_no] => 58130
[pay_date] => 2015-02-18 12:42:28
[amount_paid] => 3715
[total_paid] => 3715
[change_due] => 0
[pay_method] => CA
[bank] =>
[ref_no] =>
[payment_type] => Bill
[reversal_fee] =>
[reversal_code] =>
[reversal] => 1
[reversal_date] =>
[reversed_by_id] =>
)
[C] => Array
(
[id] => 31
[name] => GAMPOST
)
[U] => Array
(
[id] => 5
[username] => admin
)
[Bill] => Array
(
[account_no] => 1910009
[id] => 1037079
)
[RPay] => Array
(
[id] => 1
[payment_id] => 10
[user_id] => 5
[reason] => LIC
[reversal_fee] =>
[created] => 2015-02-18 13:06:28
)
[PMap] => Array
(
[data] => a:3:{s:4:"name";s:6:"Ganesh";s:7:"address";s:17:"Test address 1sdk";s:11:"paymentType";s:4:"BILL";}
[paid_amount] => 3215.00
)
)
[1] => Array
(
[Pay] => Array
(
[id] => 10
[location_id] => 133
[cashier_id] => 5
[client_id] => 2
[dealer_id] => 31
[receipt_no] => 58130
[pay_date] => 2015-02-18 12:42:28
[amount_paid] => 3715
[total_paid] => 3715
[change_due] => 0
[pay_method] => CA
[bank] =>
[ref_no] =>
[payment_type] => Bill
[reversal_fee] =>
[reversal_code] =>
[reversal] => 1
[reversal_date] =>
[reversed_by_id] =>
)
[C] => Array
(
[id] => 31
[name] => GAMPOST
)
[U] => Array
(
[id] => 5
[username] => admin
)
[Bill] => Array
(
[account_no] => 546865468
[id] => 1037162
)
[RPay] => Array
(
[id] => 1
[payment_id] => 10
[user_id] => 5
[reason] => LIC
[reversal_fee] =>
[created] => 2015-02-18 13:06:28
)
[PMap] => Array
(
[data] => a:3:{s:4:"name";s:38:"Amit Trivedi Trivedi Trivedi Trivedi";s:7:"address";s:6:"Mamura";s:11:"paymentType";s:4:"BILL";}
[paid_amount] => 500.00
)
)
[2] => Array
(
[Pay] => Array
(
[id] => 7
[location_id] => 133
[cashier_id] => 5
[client_id] => 2
[dealer_id] => 31
[receipt_no] => 82053
[pay_date] => 2015-02-18 12:29:21
[amount_paid] => 8380
[total_paid] => 8380
[change_due] => 0
[pay_method] => CA
[bank] =>
[ref_no] =>
[payment_type] => Bill
[reversal_fee] =>
[reversal_code] =>
[reversal] => 1
[reversal_date] =>
[reversed_by_id] =>
)
[C] => Array
(
[id] => 31
[name] => GAMPOST
)
[U] => Array
(
[id] => 5
[username] => admin
)
[Bill] => Array
(
[account_no] => 1910001
[id] => 1037071
)
[RPay] => Array
(
[id] => 2
[payment_id] => 7
[user_id] => 5
[reason] => LF
[reversal_fee] =>
[created] => 2015-02-18 13:06:43
)
[PMap] => Array
(
[data] => a:3:{s:4:"name";s:6:"Tushar";s:7:"address";s:17:"Test address 1sdk";s:11:"paymentType";s:4:"BILL";}
[paid_amount] => 4665.00
)
)
[3] => Array
(
[Pay] => Array
(
[id] => 7
[location_id] => 133
[cashier_id] => 5
[client_id] => 2
[dealer_id] => 31
[receipt_no] => 82053
[pay_date] => 2015-02-18 12:29:21
[amount_paid] => 8380
[total_paid] => 8380
[change_due] => 0
[pay_method] => CA
[bank] =>
[ref_no] =>
[payment_type] => Bill
[reversal_fee] =>
[reversal_code] =>
[reversal] => 1
[reversal_date] =>
[reversed_by_id] =>
)
[C] => Array
(
[id] => 31
[name] => GAMPOST
)
[U] => Array
(
[id] => 5
[username] => admin
)
[Bill] => Array
(
[account_no] => 1910009
[id] => 1037079
)
[RPay] => Array
(
[id] => 2
[payment_id] => 7
[user_id] => 5
[reason] => LF
[reversal_fee] =>
[created] => 2015-02-18 13:06:43
)
[PMap] => Array
(
[data] => a:3:{s:4:"name";s:6:"Ganesh";s:7:"address";s:17:"Test address 1sdk";s:11:"paymentType";s:4:"BILL";}
[paid_amount] => 3215.00
)
)
[4] => Array
(
[Pay] => Array
(
[id] => 7
[location_id] => 133
[cashier_id] => 5
[client_id] => 2
[dealer_id] => 31
[receipt_no] => 82053
[pay_date] => 2015-02-18 12:29:21
[amount_paid] => 8380
[total_paid] => 8380
[change_due] => 0
[pay_method] => CA
[bank] =>
[ref_no] =>
[payment_type] => Bill
[reversal_fee] =>
[reversal_code] =>
[reversal] => 1
[reversal_date] =>
[reversed_by_id] =>
)
[C] => Array
(
[id] => 31
[name] => GAMPOST
)
[U] => Array
(
[id] => 5
[username] => admin
)
[Bill] => Array
(
[account_no] => 546865468
[id] => 1037162
)
[RPay] => Array
(
[id] => 2
[payment_id] => 7
[user_id] => 5
[reason] => LF
[reversal_fee] =>
[created] => 2015-02-18 13:06:43
)
[PMap] => Array
(
[data] => a:3:{s:4:"name";s:38:"Amit Trivedi Trivedi Trivedi Trivedi";s:7:"address";s:6:"Mamura";s:11:"paymentType";s:4:"BILL";}
[paid_amount] => 500.00
)
)
)
I have googled but i am not getting my answer. Can anyone please help me on this.
Thanks for reply.
I went through below link . i have given limit => 3 . it is giving me data upto 3 for Payments Not for Bills which are multiple against one Payment.
I need to paginate 'PaymentBillMap' data .
public $paginate = array('limit'=>'3',
'order'=>'Payment.pay_date',
'fields'=>array('Payment.id','Payment.location_id','Payment.dealer_id'));
public function trans_list_service(){
$params = $this->__commonopr();
$this->loadModel('Payment');
$this->loadModel('PaymentBillMap');
$conditions = array('Payment.location_id ' => 133);
// apply the custom conditions to the pagination
$payments = $this->paginate('Payment',$conditions);
$this->set('payments', $payments);
pr($payments);
}
Custom Query Pagination Cakephp
Array
(
[0] => Array
(
[Payment] => Array
(
[id] => 1
[location_id] => 133
[dealer_id] => 31
)
[PaymentBillMap] => Array
(
[0] => Array
(
[payment_id] => 1
[bill_id] => 1037153
[paid_amount] => 4656.00
[payment_type] => BILL
[trans_no] => 24862
[data] => a:3:{s:4:"name";s:6:"Ganesh";s:7:"address";s:7:"Dholpur";s:11:"paymentType";s:4:"BILL";}
)
[1] => Array
(
[payment_id] => 1
[bill_id] => 1037154
[paid_amount] => 4665.00
[payment_type] => BILL
[trans_no] => 48280
[data] => a:3:{s:4:"name";s:5:"AMITA";s:7:"address";s:10:"Ganganagar";s:11:"paymentType";s:4:"BILL";}
)
[2] => Array
(
[payment_id] => 1
[bill_id] => 1037155
[paid_amount] => 5787.00
[payment_type] => BILL
[trans_no] => 45996
[data] => a:3:{s:4:"name";s:9:"duryodhan";s:7:"address";s:5:"Jalor";s:11:"paymentType";s:4:"BILL";}
)
[3] => Array
(
[payment_id] => 1
[bill_id] => 1037156
[paid_amount] => 6688.00
[payment_type] => BILL
[trans_no] => 64019
[data] => a:3:{s:4:"name";s:6:"Dharaj";s:7:"address";s:8:"Jhalawar";s:11:"paymentType";s:4:"BILL";}
)
[4] => Array
(
[payment_id] => 1
[bill_id] => 1037157
[paid_amount] => 8795.00
[payment_type] => BILL
[trans_no] => 21308
[data] => a:3:{s:4:"name";s:4:"Amit";s:7:"address";s:9:"Jhunjhunu";s:11:"paymentType";s:4:"BILL";}
)
[5] => Array
(
[payment_id] => 1
[bill_id] => 1037158
[paid_amount] => 5654.00
[payment_type] => BILL
[trans_no] => 10980
[data] => a:3:{s:4:"name";s:5:"Gaesh";s:7:"address";s:4:"Pali";s:11:"paymentType";s:4:"BILL";}
)
[6] => Array
(
[payment_id] => 1
[bill_id] => 1037159
[paid_amount] => 3456.00
[payment_type] => BILL
[trans_no] => 79988
[data] => a:3:{s:4:"name";s:4:"Jitu";s:7:"address";s:14:"Sawai Madhopur";s:11:"paymentType";s:4:"BILL";}
)
[7] => Array
(
[payment_id] => 1
[bill_id] => 1037160
[paid_amount] => 4664.00
[payment_type] => BILL
[trans_no] => 26314
[data] => a:3:{s:4:"name";s:6:"Rajesh";s:7:"address";s:6:"Sirohi";s:11:"paymentType";s:4:"BILL";}
)
[8] => Array
(
[payment_id] => 1
[bill_id] => 1037161
[paid_amount] => 3565.00
[payment_type] => BILL
[trans_no] => 40148
[data] => a:3:{s:4:"name";s:4:"Gani";s:7:"address";s:5:"Baran";s:11:"paymentType";s:4:"BILL";}
)
[9] => Array
(
[payment_id] => 1
[bill_id] => 1037148
[paid_amount] => 3442.00
[payment_type] => BILL
[trans_no] => 48235
[data] => a:3:{s:4:"name";s:4:"Jitu";s:7:"address";s:5:"Ajmer";s:11:"paymentType";s:4:"BILL";}
)
[10] => Array
(
[payment_id] => 1
[bill_id] => 1037149
[paid_amount] => 4335.00
[payment_type] => BILL
[trans_no] => 71496
[data] => a:3:{s:4:"name";s:4:"amer";s:7:"address";s:5:"Alwar";s:11:"paymentType";s:4:"BILL";}
)
[11] => Array
(
[payment_id] => 1
[bill_id] => 1037150
[paid_amount] => 3535.00
[payment_type] => BILL
[trans_no] => 93135
[data] => a:3:{s:4:"name";s:6:"Dilip[";s:7:"address";s:9:"Bharatpur";s:11:"paymentType";s:4:"BILL";}
)
[12] => Array
(
[payment_id] => 1
[bill_id] => 1037151
[paid_amount] => 6453.00
[payment_type] => BILL
[trans_no] => 46714
[data] => a:3:{s:4:"name";s:7:"Suvarna";s:7:"address";s:11:"Chittorgarh";s:11:"paymentType";s:4:"BILL";}
)
[13] => Array
(
[payment_id] => 1
[bill_id] => 1037152
[paid_amount] => 5323.00
[payment_type] => BILL
[trans_no] => 43007
[data] => a:3:{s:4:"name";s:5:"Datta";s:7:"address";s:5:"Churu";s:11:"paymentType";s:4:"BILL";}
)
[14] => Array
(
[payment_id] => 1
[bill_id] => 1037136
[paid_amount] => 6456.00
[payment_type] => BILL
[trans_no] => 27701
[data] => a:3:{s:4:"name";s:6:"Faizan";s:7:"address";s:9:"Dungarpur";s:11:"paymentType";s:4:"BILL";}
)
[15] => Array
(
[payment_id] => 1
[bill_id] => 1037137
[paid_amount] => 7657.00
[payment_type] => BILL
[trans_no] => 29162
[data] => a:3:{s:4:"name";s:6:"Shamli";s:7:"address";s:7:"Jodhpur";s:11:"paymentType";s:4:"BILL";}
)
[16] => Array
(
[payment_id] => 1
[bill_id] => 1037138
[paid_amount] => 8688.00
[payment_type] => BILL
[trans_no] => 77181
[data] => a:3:{s:4:"name";s:6:"Tushat";s:7:"address";s:4:"Kota";s:11:"paymentType";s:4:"BILL";}
)
[17] => Array
(
[payment_id] => 1
[bill_id] => 1037139
[paid_amount] => 4657.00
[payment_type] => BILL
[trans_no] => 75049
[data] => a:3:{s:4:"name";s:5:"Danav";s:7:"address";s:7:"Udaipur";s:11:"paymentType";s:4:"BILL";}
)
[18] => Array
(
[payment_id] => 1
[bill_id] => 1037140
[paid_amount] => 6886.00
[payment_type] => BILL
[trans_no] => 48850
[data] => a:3:{s:4:"name";s:6:"Kailas";s:7:"address";s:7:"Bikaner";s:11:"paymentType";s:4:"BILL";}
)
[19] => Array
(
[payment_id] => 1
[bill_id] => 1037141
[paid_amount] => 7965.00
[payment_type] => BILL
[trans_no] => 11684
[data] => a:3:{s:4:"name";s:7:"Paravat";s:7:"address";s:5:"Dausa";s:11:"paymentType";s:4:"BILL";}
)
)
)
[1] => Array
(
[Payment] => Array
(
[id] => 2
[location_id] => 133
[dealer_id] => 31
)
[PaymentBillMap] => Array
(
[0] => Array
(
[payment_id] => 2
[bill_id] => 1037162
[paid_amount] => 500.00
[payment_type] => BILL
[trans_no] => 62066
[data] => a:3:{s:4:"name";s:37:"Amit Trivedi Trivedi Trivedi Trivedi";s:7:"address";s:6:"Mamura";s:11:"paymentType";s:4:"BILL";}
)
[1] => Array
(
[payment_id] => 2
[bill_id] => 1037079
[paid_amount] => 3215.00
[payment_type] => BILL
[trans_no] => 41810
[data] => a:3:{s:4:"name";s:6:"Ganesh";s:7:"address";s:17:"Test address 1sdk";s:11:"paymentType";s:4:"BILL";}
)
)
)
[2] => Array
(
[Payment] => Array
(
[id] => 3
[location_id] => 133
[dealer_id] => 31
)
[PaymentBillMap] => Array
(
[0] => Array
(
[payment_id] => 3
[bill_id] => 1037162
[paid_amount] => 500.00
[payment_type] => BILL
[trans_no] => 74439
[data] => a:3:{s:4:"name";s:38:"Amit Trivedi Trivedi Trivedi Trivedi";s:7:"address";s:6:"Mamura";s:11:"paymentType";s:4:"BILL";}
)
)
)
)

Cakephp : Two arrays in foreach loop?

I have 2 arrays. 1 fetching from database and other one is from view page. I am developing a online test exam website so i have to check if the answer entered by user is correct or not.Here is the 1st one.
Array
(
[0] => Array
(
[Question] => Array
(
[id] => 51f92e34-c5a8-4de3-b264-0ff0d0483c4c
[aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[paper_id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
[qus] => What is ur name?
[slug] => name-find
[image] =>
[opt1] => x
[opt2] => y
[opt3] => a
[opt4] => b
[opt5] => c
[answer_id] => 4
[description] =>
[ansimage] =>
)
[Aptitude] => Array
(
[id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[name] => php
[slug] => php
)
[Paper] => Array
(
[id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
[aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[name] => aptitude1
[slug] => aptitude1
)
[Answer] => Array
(
[id] => 4
[name] => D
)
)
[1] => Array
(
[Question] => Array
(
[id] => 51fe4098-c344-4790-9e46-0fb4d0483c4c
[aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[paper_id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
[qus] => Place?
[slug] => place
[image] =>
[opt1] => ss
[opt2] => sss
[opt3] => ss
[opt4] => ss
[opt5] => ss
[answer_id] => 3
[description] =>
[ansimage] =>
)
[Aptitude] => Array
(
[id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[name] => php
[slug] => php
)
[Paper] => Array
(
[id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
[aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[name] => aptitude1
[slug] => aptitude1
)
[Answer] => Array
(
[id] => 3
[name] => C
)
)
[2] => Array
(
[Question] => Array
(
[id] => 51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c
[aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[paper_id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
[qus] => hayywep?
[slug] => dada
[image] =>
[opt1] => a
[opt2] => a
[opt3] => a
[opt4] => a
[opt5] => a
[answer_id] => 3
[description] =>
[ansimage] =>
)
[Aptitude] => Array
(
[id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[name] => php
[slug] => php
)
[Paper] => Array
(
[id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
[aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[name] => aptitude1
[slug] => aptitude1
)
[Answer] => Array
(
[id] => 3
[name] => C
)
)
)
and
Array
(
[51f92e34-c5a8-4de3-b264-0ff0d0483c4c] => 3
[51fe4098-c344-4790-9e46-0fb4d0483c4c] => 3
[51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c] => 3
)
i wrote this code
foreach($res as $res1):
foreach($ans as $ans1):
if($res1['Question']['answer_id']==$ans1)
{
print_r($res1['Question']['id']);
}
endforeach;
endforeach;
Output:
51f92e34-c5a8-4de3-b264-0ff0d0483c4c
51f92e34-c5a8-4de3-b264-0ff0d0483c4c
51f92e34-c5a8-4de3-b264-0ff0d0483c4c
51fe4098-c344-4790-9e46-0fb4d0483c4c
51fe4098-c344-4790-9e46-0fb4d0483c4c
51fe4098-c344-4790-9e46-0fb4d0483c4c
51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c
51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c
51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c
how to remove the duplication?
Try array_unique
$_results = array();
foreach($res as $res1):
foreach($ans as $ans1):
if($res1['Question']['answer_id']==$ans1)
{
$_results[] = $res1['Question']['id'];
}
endforeach;
endforeach;
$results = array_unique($_results);
pr($results);

Resources