convert 3 array into one bidimensional array with array_merge? php - array-merge

current output is: 3 array
Array ( [pf_id] => 5 [pf_title] => Gender [pf_icon] => [pf_key] => gender )
Array ( [pf_id] => 6 [pf_title] => Location [pf_icon] => [pf_key] => location )
Array ( [pf_id] => 7 [pf_title] => Interests [pf_icon] => [pf_key] => interests )
the result must be this: 1 array including 1 subarray for each pf_id
Array (
5 => array ([pf_title] => Gender [pf_icon] => [pf_key] => gender)
6 => array ([pf_title] => Location [pf_icon] => [pf_key] => location)
7 => array ([pf_title] => Interests [pf_icon] => [pf_key] => interests)
)
the code for to fetch data:
$cfields = array();
$this->DB->build( array(
'select' => 'pf_d.pf_id, pf_d.pf_title, pf_d.pf_icon, pf_d.pf_key',
'from' => array( 'pfields_data' => 'pf_d' ),
'where' => 'pf_d.pf_id = 5 or pf_d.pf_id = 6 or pf_d.pf_id = 7',
) );
// if ($this->settings['b_multiplayer_f_id'] && in_array( $this->request['f'], explode(',', $this->settings['b_multiplayer_f_id'] ) ) ) {
print $this->DB->fetchSqlString();
// }
$this->DB->execute();
while ( $row = $this->DB->fetch() ) {
// array correction starts please help!!
# $extract = array();
# $extract = implode/explode??
# $extract[] = $row['pf_id'].' => '.array( 'pf_title' => $row['pf_title'], 'pf_icon' => $row['pf_icon'], 'pf_key' => $row['pf_key']);
// array correction finish
$cfields[] = $extract;
print_r($row);
}
i am using invisionpower board (i am customer but it not require a their response... to solve)
and how I fetch data array( n => array()) into template?
exemple code:
<foreach loop="$cfields as $key=>$value">
{$value[pf_title]} => {$this->memberData['field_'.$key]}
</foreach>
is correct?

i have solved... I edit while() into
while ( $row = $this->DB->fetch() ) {
$b_array = array_slice($row, 1);
$cfields[ $row['pf_id'] ] = $b_array;
}
result
Array
(
[5] => Array
(
[pf_title] => Gender
[pf_icon] =>
[pf_key] => gender
)
[6] => Array
(
[pf_title] => Location
[pf_icon] =>
[pf_key] => location
)
[7] => Array
(
[pf_title] => Interests
[pf_icon] =>
[pf_key] => interests
)
)

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.................

How to get Count of Lessons in a course in cakephp?

Thanks in advance!
I am new to CakePHP, and I am trying to get just count of lessons of course. I am using CakePHP with friendsofcake's crud.
$this->Crud->on('beforePaginate', function(\Cake\Event\Event $event) {
$query = $event->getSubject()->query;
$query->contain(['Lessons']);
});
with this code, I am getting all the lessons of the course, but I need only counts, because if a course having 50 lessons, why I should get all the lessons?
Output:-
App\Model\Entity\Course Object
(
[id] => 7
[language_id] => 2
[course_title] => test
[course_desc] =>
[audio_path] =>
[img_path] =>
[inappiosid] =>
[inappandroidid] =>
[sOrder] => 1
[lessons] => Array
(
[0] => App\Model\Entity\Lesson Object
(
[id] => 4
[course_id] => 7
[lesson_title] => test
[lesson_description] => test
[file_path] => test
[sOrder] => 1
[[new]] =>
[[accessible]] => Array
(
[course_id] => 1
[lesson_title] => 1
[lesson_description] => 1
[file_path] => 1
[sOrder] => 1
[course] => 1
)
)
)
[[new]] =>
[[accessible]] => Array
(
[language_id] => 1
[course_title] => 1
[course_desc] => 1
[audio_path] => 1
[img_path] => 1
[inappiosid] => 1
[inappandroidid] => 1
[sOrder] => 1
[language] => 1
[customer_orders] => 1
[lessons] => 1
)
)

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
}

SUM query in cakephp 3 not working

I am trying to add the data of same field and want to return a result i used the following query:
$total = $this->Details->find('all', array(
'fields' => array('sum(Details.total_downtime+ Details.total_downtime)'),
'conditions' => array('Details.site_id' => $id)
));
print_r($total->toArray());
exit;
And I am getting the following result:
Array (
[0] => App\Model\Entity\Detail Object (
[displayField] => username
[_accessible:protected] => Array (
[*] => 1
[id] => 1
[site_id] => 1
[uptime] => 1
[downtime] => 1
)
[_properties:protected] => Array (
[sum(Details] => Array ( [total_downtime+ Details] => 4 )
)
[_original:protected] => Array ( )
[_hidden:protected] => Array ( )
[_virtual:protected] => Array ( )
[_className:protected] => App\Model\Entity\Detail [_dirty:protected] => Array ( )
[_new:protected] =>
[_errors:protected] => Array ( )
[_registryAlias:protected] => Details
)
)
Where can I find my sum?
I guess you are trying to achieve something like this?
$query = $Details->find();
$query
->select(['sum' => $query->func()->sum('Details.total_downtime')])
->where(['Details.site_id' => $id])
->toArray();
You can do this collection object like below:
$Details = $this->Details->find();
$Details = $Details->where(['Details.site_id' => $id]);
$sumOftotal_downtime = $Details->sumOf('Details.total_downtime');
Cookbook Reference
Try this:
$Details = this->Details->find()->select(['sum'=>'SUM(Details.total_downtime)'])->where(['Details.site_id' => $id])->toArray();

how to merge array with the same value in smarty?

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 );
?>
)

Resources