How to copy single array from multidimensional array? - php

I have one multidimensional array as shown below(PHP) :
Array
(
[144320] => Array
(
[3568728] => Array
(
[30832] => 30832
)
[3568884] => Array
(
[30827] => 30827
[30828] => 30828
[30830] => 30830
[30831] => 30831
[30832] => 30832
[30837] => 30837
[30838] => 30838
[30839] => 30839
[30826] => 30826
[30808] => 30808
[30806] => 30806
[30807] => 30807
[30698] => 30698
[30601] => 30601
[30697] => 30697
)
)
[144330] => Array
(
[3568731] => Array
(
[30827] => 30827
[30839] => 30839
[30838] => 30838
[30837] => 30837
[30832] => 30832
[30831] => 30831
[30828] => 30828
[30830] => 30830
[30826] => 30826
[30806] => 30806
[30808] => 30808
[30807] => 30807
[30698] => 30698
[30697] => 30697
[30601] => 30601
)
)
[144218] => Array
(
[3568753] => Array
(
[30808] => 30808
)
)
[144216] => Array
(
[3568732] => Array
(
[30808] => 30808
)
)
)
This array is populated by following code:
$sql = "SELECT * FROM `bf_alert_stack` WHERE `type` = 'immediately' order by created desc";
$q = db_query($sql);
$user_alerts = array();
while ($row = db_fetch_array($q))
{
$user_alerts [$row['uid']] [$row['alert_id']] [$row['nid']] = $row['nid'];
}
From the above user_alerts array I want to rearrange the [$row['nid']] array and for rearrangement I want capture [$row['nid']] array and after capturing it into another array I want to re-arrange $row['nid'] array I want to update this $row['nid'] array into original user_alerts array.
How I can do this? I am not getting any search for this on google so just placed this on appropriate place.

The best you can do is to get it ordered from the query with something like:
SELECT * FROM `bf_alert_stack` WHERE `type` = 'immediately'
ORDER BY created desc, nid
But if you don't have access, or need the original sort for other reason, you need to iterate recursively over the array and reassign the last sortered level to the original array with PHP ksort method:
http://www.php.net/manual/en/function.ksort.php
foreach($user_alerts as $uid=>$user_alert) {
foreach($alert as $alert_id=>$nids) {
$user_alerts[$uid][$alert_id] = ksort($nids);
}
}
Hope it helps!

Related

How to combine multiple arrays that share a common value

I am creating a web page that will allow comparisons of MySQL global variables for two or more instances. The user will select the instance's and a php function with use mysqli to get the results.
The results will come in the below format:
Array (
[0] => Array ( [VARIABLE_NAME] => activate_all_roles_on_login [host1:3306] => OFF )
[1] => Array ( [VARIABLE_NAME] => admin_address [host1:3306] => 0.0.0.1 )
)
Array (
[0] => Array ( [VARIABLE_NAME] => activate_all_roles_on_login [host1:3307] => OFF )
[1] => Array ( [VARIABLE_NAME] => admin_address [host1:3307] => 0.0.0.1 )
)
Array (
[0] => Array ( [VARIABLE_NAME] => activate_all_roles_on_login [host3:3308] => OFF )
[1] => Array ( [VARIABLE_NAME] => admin_address [host3:3308] => 0.0.0.2 )
)
I am wanting to combine these on the variable name value, like below:
[0] => Array ( [VARIABLE_NAME] => activate_all_roles_on_login [host1:3306] => OFF [host1:3307] => OFF [host3:3308] => OFF )
I've tried using array_combine, but that only made more of a mess.
The end goal of this is to make an array that can be easily turned into a table like below:
I would recommend you to try to implement it with pure SQL, however if you must to do it with PHP, here are some solutions.
Your array:
$queryResult = [
[
['VARIABLE_NAME' => 'activate_all_roles_on_login', 'host1:3306' => 'OFF'] ,
['VARIABLE_NAME' => 'admin_address', 'host1:3306' => '0.0.0.1'] ,
],
[
['VARIABLE_NAME' => 'activate_all_roles_on_login', 'host1:3307' => 'OFF'],
['VARIABLE_NAME' => 'admin_address', 'host1:3307' => '0.0.0.2'] ,
],
[
['VARIABLE_NAME' => 'activate_all_roles_on_login', 'host1:3308' => 'OFF'] ,
['VARIABLE_NAME' => 'admin_address', 'host1:3308' => '0.0.0.3'] ,
]
];
If you are sure that 'activate_all_roles_on_login' would always be on the first place in result, your task can be done in one line:
var_dump(array_merge(...array_column($queryResult, 0)));
However, if your query result may vary, you can try something like this:
$result = [];
foreach ($queryResult as $row) {
$subresult = array_column($row, null, 'VARIABLE_NAME');
if (isset($subresult['activate_all_roles_on_login'])) {
$result[] = $subresult['activate_all_roles_on_login'];
}
}
var_dump(array_merge(...$result));
Edit:
Also it can be done with two loops (which is kind of slower I guess):
foreach ($queryResult as $row) {
foreach ($row as $entry) {
if (isset($entry['VARIABLE_NAME']) && $entry['VARIABLE_NAME'] === 'activate_all_roles_on_login') {
$result[] = $entry;
}
}
}
var_dump(array_merge(...$result));
I believe the answer from MERGE-ARRAY-WITH-COMMON-VALUE will allow me to accomplish what I am after.

How to get the array value using loop and pass the value to the function?

Sorry for the title of my question. It is hard for me to explain this so to make my problem short.
I just need to pass the values from my table to my function. I put the information from my table in an array and I need to use that array because it will serve it as the parameter for my function.
Here's the sample code.
$dragonpay = "SELECT * FROM dragon_pay";
$resultDragonPay = $this->db->query($dragonpay);
foreach($resultDragonPay->result_array() as $dragonpay_value){
$dragon[] = array(
'transaction_id' => $dragonpay_value['transaction_id'],
'SC_REF' => $dragonpay_value['SC_REF']
);
}
This is the sample output using print_r($dragon)
Array
(
[0] => Array
(
[transaction_id] => 122451
[SC_REF] => LL877KG4
)
[1] => Array
(
[transaction_id] => 122563
[SC_REF] => ERQKX2A0
)
[2] => Array
(
[transaction_id] => 122696
[SC_REF] => AM383D62
)
[3] => Array
(
[transaction_id] => 123549
[SC_REF] => E88JNWB6
)
[4] => Array
(
[transaction_id] => 122407
[SC_REF] => 734T3AK3
)
[5] => Array
(
[transaction_id] => 123352
[SC_REF] => QFL45SM2
)
Now my problem is the values from my array. Each of the index values should be use as a parameter.
Now I have this function. The use of this function is for data encryption.
$info_data = #serialize($array_here);
$encrypt_data = fn_encrypt_text($info_data);
Example scenario:
$array_to_enrypt = array(
'name' => 'myname',
'gender' => 'mygender'
)
$info_data = #serialize($array_to_enrypt);
$encrypt_data = fn_encrypt_text($info_data);
But I included all the values from my array. How can I get every value and use this as a single array? Do I need to include this in the loop? That's all guys I hope you understand what I mean. Thanks.
Is this what you need?
function dragon_pay(){
$data = $this->db->get('dragon_pay')->result_array();
if( is_array( $data ) && count( $data ) > 0 ){
foreach( $data as $key => $each ){
$dragon = array(
'transaction_id' => $dragonpay_value['transaction_id'],
'SC_REF' => $dragonpay_value['SC_REF']
);
$info_data = #serialize($dragon);
$encrypt_data = fn_encrypt_text($info_data);
call_to_another_function( $encrypt_data ); #this is the function you want to call with the encrypted text?
}
}
print_r( $data );
}

populate php multidimenssional array with foreach loop

I need to create an array like the following:
$va_body=array(
"bundles" => array(
"$table.$elem" => array("convertCodesToDisplayText" => true),
"$table.$elem" => array("convertCodesToDisplayText" => true),
)
);
$table is a string that does not change and $elem is extracted from an array.
I got close with the following code, but it ends up with only the last value from $bund, $bund is an array with two values. I guess the array is redeclared in each loop?
$va_body=array(); // declare the array outside the loop
foreach ($bund as $elem ) {
$va_body['bundles'] = array($table.".".$elem=>array("convertCodesToDisplayText" => true));
}
$bund array has two elements "description" and "type_id".
$va_body['bundles'][] // Adding [] doesn't work as it modifies the expected outcome.
print_r($va_body) looks like this:
Array (
[bundles] => Array (
[ca_objects.type_id] => Array (
[convertCodesToDisplayText] => 1
)
)
)
I need it to be:
Array (
[bundles] => Array (
[ca_objects.description] => Array (
[convertCodesToDisplayText] => 1
)
[ca_objects.type_id] => Array (
[convertCodesToDisplayText] => 1
)
)
)
Thanks in advance.
#phpisuber01
Using:
$va_body['bundles'][] = array($table.".".$elem=>array("convertCodesToDisplayText" => true));
print_r($va_body); looks like this:
Array (
[bundles] => Array (
[0] => Array (
[ca_objects.description] => Array (
[convertCodesToDisplayText] => 1
)
)
[1] => Array (
[ca_objects.type_id] => Array (
[convertCodesToDisplayText] => 1
)
)
)
)
And I need it to be like this:
Array (
[bundles] => Array (
[ca_objects.description] => Array (
[convertCodesToDisplayText] => 1
)
[ca_objects.type_id] => Array (
[convertCodesToDisplayText] => 1
)
)
)
Answer by #phpisuber01:
$va_body['bundles'][$table.".".$elem] = array("convertCodesToDisplayText" => true);
Thank you very much!
You need to make an array of arrays. In your loop change the following line:
$va_body['bundles'][$table.".".$elem] = array("convertCodesToDisplayText" => true);
Added [] after $va_body['bundles'].
All this does is keep adding the new bundles into the array. Your original code is overwriting bundles each iteration. That's why you only get the last one.
Updated to get closer to OP's exact needs.
$va_body = array();
$va_body['bundles'] = array();
foreach ($bund AS $elem)
{
$va_body['bundles']["{$table}.{$elem}"] = array("convertCodesToDisplayText" => true);
}

How to sort three dimenisonal array based on the specific index?

Even After lots of search on google I am not able to understand that how I can sort the associative array of 3 dimensional.
Actually, I want to sort each user's alert's jobs according to the posted date of job.
Following is the code:
$sql = "SELECT * FROM `bf_alert_stack` WHERE `type` = 'immediately' order by created desc";
$q = db_query($sql);
$user_alerts = array();
while ($row = db_fetch_array($q))
{
$user_alerts [$row['uid']] [$row['alert_id']] [$row['nid']] = $row['nid'];
}
Here in $user_alerts array, $row['nid'] contains jobs of particular user's alert.
when user gets email alert at that time it should show the job in sorted order according to date.
Following is the sample data from $user_alerts array
Array
(
[144320] => Array
(
[3568728] => Array
(
[30832] => 30832
)
[3568884] => Array
(
[30837] => 30837
[30827] => 30827
[30828] => 30828
[30830] => 30830
[30831] => 30831
[30832] => 30832
[30838] => 30838
[30839] => 30839
[30826] => 30826
[30806] => 30806
[30808] => 30808
[30807] => 30807
[30698] => 30698
[30697] => 30697
[30601] => 30601
)
)
[144330] => Array
(
[3568731] => Array
(
[30827] => 30827
[30839] => 30839
[30838] => 30838
[30837] => 30837
[30832] => 30832
[30831] => 30831
[30830] => 30830
[30828] => 30828
[30826] => 30826
[30806] => 30806
[30808] => 30808
[30807] => 30807
[30698] => 30698
[30697] => 30697
[30601] => 30601
)
)
[144218] => Array
(
[3568753] => Array
(
[30808] => 30808
)
)
[144216] => Array
(
[3568732] => Array
(
[30808] => 30808
)
)
)
I want to sort this above user_alerts array based on row['nid'] but row['nid']'s details are in database table.all nid should be rearranged according to created date of nid which is present in table.
Little confusing this but perhaps by chance this will work.. ?
$sql = "SELECT * FROM `bf_alert_stack` WHERE `type` = 'immediately' ORDER BY `created` DESC, `uid` ASC, `alert_id` DESC, `nid` DESC";

PHP/MySQL: Recreate multidimensional array from existing array

I've got an multi-array (currently with objects) that I want to reorder based on a specific key/value.
Array
(
[0] => stdClass Object
(
[task_id] => 1
[task_title] => Title
[users_username] => John
)
[1] => stdClass Object
(
[task_id] => 2
[task_title] => Title
[users_username] => John
)
[2] => stdClass Object
(
[task_id] => 3
[task_title] => Title
[users_username] => Mike
)
)
I'd like to reorder it to get multi-arrays by user_name, so I can cycle through the task by username.
Array
(
[John] => Array
(
[0] => Array
(
[task_id] => 1
[title] => Title
)
[1] => Array
(
[task_id] => 2
[title] => Title
)
)
[Mike] => Array
(
[0] => Array
(
[task_id] => 3
[title] => Title
)
)
)
Is it possible to recreate my array to an array like that above?
Updated version of the code
<?php
$it0 = (object) array('task_id' => 1,'task_title' => 'Title','users_username' => 'John');
$it1 = (object) array('task_id' => 2,'task_title' => 'Title','users_username' => 'John');
$it2 = (object) array('task_id' => 3,'task_title' => 'Title','users_username' => 'Mike');
$array = array($it0,$it1,$it2);
$return = array();
foreach($array as $id => $value){
$return[$value->users_username][] = array('task_id' => $value->task_id,'title' => $value->task_title);
}
var_dump($return);
Yes, it is possible.
You'll have to loop through your current array and create a new array to do it.
example:
$new_array = array();
foreach ($array as $row)
{
$new_row = array(
'task_id' => $row->task_id,
'title' => $row->task_title,
);
$name = $row->users_username;
if (isset($new_array[$name]))
{
$new_array[$name][] = $new_row;
}
else
{
$new_array[$name] = array($new_row);
}
}
Now $new_array contains the new array exactly like the one you're asking for.
Then you can sort it with
ksort($new_array);
There may be another way to do this, with some built-in function, but sometimes I'd rather just do it myself, and know how it is working, without having to look up the documentation.
The approach:
Iterate through all of the first array, looking at [users_username] and putting them into a new array.
Code:
$dst_array = array();
foreach ($src_array as $val)
{
$user = $val->users_username;
// TODO: This check may be unnecessary. Have to test to find out.
// If this username doesn't already have an array in the destination...
if (!array_key_exists($user, $dst_array))
$dst_array[$user] = array(); // Create a new array for that username
// Now add a new task_id and title entry in that username's array
$dst_array[$user][] = array(
'task_id' => $val->task_id
'title' => $val->title
);
}
Just something like this (maybe not 100% PHP code):
foreach ( $obj : $oldArray ) {
$newTask['task_id'] = $obj->task_id;
$newTask['title'] = $obj->title;
$newArray[$oldName][] = $newTask;
}
If you want to order it; you can just call a order function afterwards.

Categories