I have a associative array like this
(
[0] => Array
(
[userId] => 4785
[courseId] => 1774
[EnterpriseId] => 13
[lbaseid] => 1697
[progress] => 100
[milestone] => 5
)
[1] => Array
(
[userId] => 4786
[courseId] => 1775
[EnterpriseId] => 13
[lbaseid] => 1698
[progress] => 100
[milestone] => 5
)
[2] => Array
(
[userId] => 4786
[courseId] => 1776
[EnterpriseId] => 13
[lbaseid] => 1699
[progress] => 100
[milestone] => 5
)
[3] => Array
(
[userId] => 4786
[courseId] => 1777
[EnterpriseId] => 13
[lbaseid] => 1700
[progress] => 100
[milestone] => 5
)
[4] => Array
(
[userId] => 4786
[courseId] => 1778
[EnterpriseId] => 13
[lbaseid] => 1701
[progress] => 100
[milestone] => 5
)
)
I want to convert this associative array like
(
4785_1774_13[0] => Array
(
[userId] => 4785
[courseId] => 1774
[EnterpriseId] => 13
[lbaseid] => 1697
[progress] => 100
[milestone] => 5
)
4786_1775_13[1] => Array
(
[userId] => 4786
[courseId] => 1775
[EnterpriseId] => 13
[lbaseid] => 1698
[progress] => 100
[milestone] => 5
)
4786_1776_13[2] => Array
(
[userId] => 4786
[courseId] => 1776
[EnterpriseId] => 13
[lbaseid] => 1699
[progress] => 100
[milestone] => 5
)
4786_1777_13[3] => Array
(
[userId] => 4786
[courseId] => 1777
[EnterpriseId] => 13
[lbaseid] => 1700
[progress] => 100
[milestone] => 5
)
4786_1778_13[4] => Array
(
[userId] => 4786
[courseId] => 1778
[EnterpriseId] => 13
[lbaseid] => 1701
[progress] => 100
[milestone] => 5
)
)
If you want to have a new array with keys constructed with the values from the original array, you can iterate through the initial array and construct a new array with the desired key format.
$array = [
[
"userId" => 11,
"courseId" => 22,
"EnterpriseId" => 33
],
[
"userId" => 44,
"courseId" => 55,
"EnterpriseId" => 66
]
];
var_dump($array);
$new_array = [];
foreach($array as $key => $value) {
$new_key = implode("_",[
$value["userId"],
$value["courseId"],
$value["EnterpriseId"]
]);
$new_array[$new_key] = $value;
}
var_dump($new_array);
Live example here: https://3v4l.org/1ibMl
Related
I have a query
SELECT bs.i_description,col.Column_name,bs.Tm_id,ad.UserName as name,
a.fk_sprint_id as spid,b.fk_back_id,b.u_pos_is,b.s_id,b.color,b._left,b._top,
b.wiptime as wiptime,b.dodtime as dodtime,b.Dep_status
FROM backToSprint a
JOIN backToSprint b ON a.`s_id` = b.`fk_f_id`
join backlog bs on bs.b_id=b.fk_back_id
left join admin ad on bs.Tm_id=ad.adminID
left join admin_column col on col.column_id=b.u_pos_is
where a.fk_sprint_id=3 and a.teamid=1
ORDER BY a.`fk_f_id`
Where I got output like
Array
(
[0] => Array
(
[i_description] => Story 1
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 408
[u_pos_is] => 1
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[1] => Array
(
[i_description] => Story 2
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 409
[u_pos_is] => 1
[s_id] => 6
[color] => 3
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
Here Column name is same so I expect output like
Array
(
['c'] => Array
(
[Column_name] => Backlogs
)
[0] => Array
(
[i_description] => Story 1
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 408
[u_pos_is] => 1
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[1] => Array
(
[i_description] => Story 2
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 409
[u_pos_is] => 1
[s_id] => 6
[color] => 3
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
Then i tried
$this->view->getstories['c'] = ['Column_name' => array_unique(array_column( $this->view->getstories, 'Column_name'))];
which didn't get actual output.i got output like
Array
(
[0] => Array
(
[i_description] => Story 1
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 408
[u_pos_is] => 1
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[1] => Array
(
[i_description] => Story 2
[Column_name] => WIP
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 409
[u_pos_is] => 2
[s_id] => 6
[color] => 3
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[c] => Array
(
[Column_name] => Array
(
[0] => Backlogs
[1] => WIP
)
)
)
(Here i have another column name WIP)
But i want a nested array like
Array
(
[c1] => Array
(
[Column_name] => Array
(
[0] => Backlogs
)
[0] => Array
(
[i_description] => Story 1
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 408
[u_pos_is] => 1
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[c2] => Array
(
[Column_name] => Array
(
[0] => WIP
)
[0] => Array
(
[i_description] => Story 2
[Column_name] => WIP
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 409
[u_pos_is] => 2
[s_id] => 6
[color] => 3
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
)
Please help me to solve this issue
Any help would be appreciated.
What you want is nested arrays. Do it while fetching the rows from the query, using $row['Column_name'] as the key of the main array, and pushing each row onto that nested array.
$results = [];
while ($row = $stmt->fetch()) {
$results[$row['Column_name']][] = $row;
}
This will create a result like:
Array
(
[Backlogs] => Array
(
[0] => Array
(
[i_description] => Story 1
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 408
[u_pos_is] => 1
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
[WIP] => Array
(
[0] => Array
(
[i_description] => Story 2
[Column_name] => WIP
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 409
[u_pos_is] => 2
[s_id] => 6
[color] => 3
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
)
)
There's no need for the c1 and c2 keys, just use the Column_name values as the keys of the main array.
I have an array:
Array (
[0] => Array
( [points] => 10
[id] => 58
[nazwa] => auser1 )
[1] => Array
( [points] => 15
[id] => 36
[nazwa] => cuser2 )
[2] => Array
( [points] => 15
[id] => 57
[nazwa] => buser3 )
[3] => Array
( [points] => 20
[id] => 56
[nazwa] => duser4 )
[4] => Array
( [points] => 20
[id] => 54
[nazwa] => euser5 ))
I would like to sort this array by points and then alphabetically by nazwa.
How can I do this?
I would like to create final points table for Russia Cup!
if you want to sort your multidimensional array in sequence first points then with name then you have to create your multidimensional array in same sequence format
example: first element should be points, second name, last id. Refer following sequence.
$array = [ [ 'points' => 10, 'nazwa' => 'auser1', 'id' => 58 ],
[ 'points' => 15, 'nazwa' => 'cuser2', 'id' => 36 ],
[ 'points' => 15, 'nazwa' => 'buser3', 'id' => 57 ],
[ 'points' => 20, 'nazwa' => 'duser4', 'id' => 56 ],
[ 'points' => 20, 'nazwa' => 'euser5', 'id' => 54 ]];
array_multisort( $array );
print_r(($array));
Output:
Array
(
[0] => Array
(
[points] => 10
[nazwa] => auser1
[id] => 58
)
[1] => Array
(
[points] => 15
[nazwa] => buser3
[id] => 57
)
[2] => Array
(
[points] => 15
[nazwa] => cuser2
[id] => 36
)
[3] => Array
(
[points] => 20
[nazwa] => duser4
[id] => 56
)
[4] => Array
(
[points] => 20
[nazwa] => euser5
[id] => 54
)
)
[traffic] => Array
(
[0] => Array
(
[id] => 1
[visitors] => 310
[pageviews] => 1333
[created_date] => 2016-03-09
)
[1] => Array
(
[id] => 2
[visitors] => 374
[pageviews] => 1010
[created_date] => 2016-03-10
)
[2] => Array
(
[id] => 3
[visitors] => 143
[pageviews] => 617
[created_date] => 2016-03-11
)
)
[source] => Array
(
[0] => Array
(
[created_date] => 2016-03-09
[scount] => 368
)
[1] => Array
(
[created_date] => 2016-03-10
[scount] => 550
)
[2] => Array
(
[created_date] => 2016-03-11
[scount] => 238
)
)
I have two multidimensional arrays, I want to combine both arrays into one with matching created_date value, the result should be like this,
Array
(
[0] => Array
(
[created_date] => 2016-03-09
[id] => 1
[visitors] => 310
[pageviews] => 1333
[scount] => 368
)
[1] => Array
(
[created_date] => 2016-03-10
[id] => 2
[visitors] => 374
[pageviews] => 1010
[scount] => 550
)
[2] => Array
(
[created_date] => 2016-03-11
[id] => 3
[visitors] => 143
[pageviews] => 617
[scount] => 238
)
)
$traffic = []; //...
$source = []; // ...
foreach($traffic as $key => $value)
{
if(isset($source[$key]))
{
$token = $source[$key];
foreach($token as $keyy => $valuee)
{
if(isset($traffic[$key][$keyy]))
{
// Collision handling, if any ...
$traffic[$key][$keyy] = $valuee;
}
else $traffic[$key][$keyy] = $valuee;
}
}
}
The following code should do the trick.
The solution:
# I split your array into 2 parts ($traffic = $your_array['traffic'])
$traffic = array(
array(
id => 1,
visitors => 310,
pageviews => 1333,
created_date => '2016-03-09'
),
array(
id => 2,
visitors => 374,
pageviews => 1010,
created_date => '2016-03-10'
),
array(
id => 3,
visitors => 143,
pageviews => 617,
created_date => '2016-03-11'
)
);
# I split your array into 2 parts ($source = $your_array['source'])
$source = array(
array (
created_date => '2016-03-09',
scount => 368
),
array (
created_date => '2016-03-10',
scount => 550
),
array (
created_date => '2016-03-11',
scount => 238
)
);
# copy the traffic array cause we want to merge the new data into it
$result = $traffic;
# loop over the traffic array
foreach ($traffic as $k => $t) {
# loop over the source
foreach ($source as $s) {
# try to find a match
if ($t['created_date'] === $s['created_date']) {
# add data to result
$result[$k]['scount'] = $s['scount'];
# we exit the inner foreach-loop here as there is only 1 match
break;
}
}
}
# print the result
echo '<pre>'; print_r($result); echo '</pre>';
The result:
Array
(
[0] => Array
(
[id] => 1
[visitors] => 310
[pageviews] => 1333
[created_date] => 2016-03-09
[scount] => 368
)
[1] => Array
(
[id] => 2
[visitors] => 374
[pageviews] => 1010
[created_date] => 2016-03-10
[scount] => 550
)
[2] => Array
(
[id] => 3
[visitors] => 143
[pageviews] => 617
[created_date] => 2016-03-11
[scount] => 238
)
)
you can test it here: http://www.writephponline.com
Here is your solution:-
$arr1 = $arr1['traffic']; // assign key traffic record to array1
$arr2 = $arr2['source']; // assign key source record to array2
$result = [];
foreach($arr1 as $key=>$value){
$result[$key] = $value;
// find created_date in second array
$keyOfSecondArr = array_search($value['created_date'], array_column($arr2, 'created_date'));
$result[$key]['scount'] = $arr2[$keyOfSecondArr]['scount'];
}
echo '<pre>'; print_r($result);
output:-
Array
(
[0] => Array
(
[id] => 1
[visitors] => 310
[pageviews] => 1333
[created_date] => 2016-03-09
[scount] => 368
)
[1] => Array
(
[id] => 2
[visitors] => 374
[pageviews] => 1010
[created_date] => 2016-03-10
[scount] => 550
)
[2] => Array
(
[id] => 3
[visitors] => 143
[pageviews] => 617
[created_date] => 2016-03-11
[scount] => 238
)
)
I want to sort my multidimensional array with two index key.
Let me give you example,
here, what I am getting by query.
Array
(
[0] => Array
(
[cmp_id] => 33
[cmp_name] => CONF
[mod_pin] => 6811
[provider_name] => test2
[meeting_count] => 68
[total_participant] => 123
[total_dur] => 353:25:00
[web_dur] => 575.19
[did_dur] => 1.78
)
[1] => Array
(
[cmp_id] => 33
[cmp_name] => MCONF
[mod_pin] => 1908
[provider_name] => test2
[meeting_count] => 4
[total_participant] => 7
[total_dur] => 10:19:00
[web_dur] => 17.7
[did_dur] => 0
)
[2] => Array
(
[cmp_id] => 1
[cmp_name] => MT0001
[mod_pin] => 1662
[provider_name] => mob_test
[meeting_count] => 3
[total_participant] => 5
[total_dur] => 20:18:00
[web_dur] => 22.05
[did_dur] => 0
)
[3] => Array
(
[cmp_id] => 1
[cmp_name] => MT0001
[mod_pin] => 1234
[provider_name] => mob_test
[meeting_count] => 4
[total_participant] => 10
[total_dur] => 16:40:00
[web_dur] => 17.77
[did_dur] => 0
)
)
I want to sort it by provider_name and than mod_pin.
It want like this
Array
(
[0] => Array
(
[cmp_id] => 33
[cmp_name] => CONF
[mod_pin] => 1908
[provider_name] => test2
[meeting_count] => 4
[total_participant] => 7
[total_dur] => 10:19:00
[web_dur] => 17.7
[did_dur] => 0
)
[1] => Array
(
[cmp_id] => 33
[cmp_name] => CONF
[mod_pin] => 6811
[provider_name] => test2
[meeting_count] => 68
[total_participant] => 123
[total_dur] => 353:25:00
[web_dur] => 575.19
[did_dur] => 1.78
)
[2] => Array
(
[cmp_id] => 1
[cmp_name] => MT0001
[mod_pin] => 1234
[provider_name] => mob_test
[meeting_count] => 4
[total_participant] => 10
[total_dur] => 16:40:00
[web_dur] => 17.77
[did_dur] => 0
)
[3] => Array
(
[cmp_id] => 1
[cmp_name] => MT0001
[mod_pin] => 1662
[provider_name] => mob_test
[meeting_count] => 3
[total_participant] => 5
[total_dur] => 20:18:00
[web_dur] => 22.05
[did_dur] => 0
)
)
Thanks in advance.
You need array_multisort
# get a list of sort columns and their data to pass to array_multisort
$sort = array();
foreach($myArray as $k=>$v) {
$sort['provider_name'][$k] = $v['provider_name'];
$sort['mod_pin'][$k] = $v['mod_pin'];
}
# sort by provider_name and then title mod_pin
array_multisort($sort['provider_name'], SORT_ASC, $sort['mod_pin'], SORT_ASC,$mylist);
i am new here...I have two dynamic arrays..their columns are different
First Array:
Array
(
[0] => Array
(
[id] => 102
[parent_id] => 94
[dir_name] => games
[size] =>
[owner_id] => 10
[shared_link] => http://creativevisionintl.com/dev/fwingo/usersdata/saqlain/Home/games
[shared] => 0
[comment] =>
[add_date] => 07/30/2013 02:43 AM
[update_date] => 07/30/2013 02:43 AM
[path] => /home1/creatkj8/public_html/dev/fwingo/usersdata/saqlain/Home/games
[trash_status] => 1
[password] =>
[expires_after] => 0
[allow_uploading] => 0
[share_date] =>
)
[1] => Array
(
[id] => 122
[parent_id] => 94
[dir_name] => New Folder
[size] => 777835
[owner_id] => 10
[shared_link] => http://creativevisionintl.com/dev/fwingo/usersdata/saqlain/Home/New Folder
[shared] => 0
[comment] =>
[add_date] => 08/16/2013 08:31 AM
[update_date] => 08/16/2013 08:31 AM
[path] => /home1/creatkj8/public_html/dev/fwingo/usersdata/saqlain/Home/New Folder
[trash_status] => 1
[password] =>
[expires_after] => 0
[allow_uploading] => 0
[share_date] =>
)
[2] => Array
(
[id] => 98
[parent_id] => 94
[dir_name] => Videos
[size] =>
[owner_id] => 10
[shared_link] => http://creativevisionintl.com/dev/fwingo/usersdata/saqlain/Home/Videos/
[shared] => 0
[comment] =>
[add_date] => 07/30/2013 02:38 AM
[update_date] => 07/30/2013 02:38 AM
[path] => /home1/creatkj8/public_html/dev/fwingo/usersdata/saqlain/Home/Videos/
[trash_status] => 1
[password] =>
[expires_after] => 0
[allow_uploading] => 0
[share_date] =>
)
[3] => Array
(
[id] => 97
[parent_id] => 94
[dir_name] => Pictures
[size] =>
[owner_id] => 10
[shared_link] => http://creativevisionintl.com/dev/fwingo/usersdata/saqlain/Home/Pictures/
[shared] => 0
[comment] =>
[add_date] => 07/30/2013 02:38 AM
[update_date] => 07/30/2013 02:38 AM
[path] => /home1/creatkj8/public_html/dev/fwingo/usersdata/saqlain/Home/Pictures/
[trash_status] => 1
[password] =>
[expires_after] => 0
[allow_uploading] => 0
[share_date] =>
)
[4] => Array
(
[id] => 95
[parent_id] => 94
[dir_name] => Documents
[size] =>
[owner_id] => 10
[shared_link] => http://creativevisionintl.com/dev/fwingo/usersdata/saqlain/Home/Documents/
[shared] => 1
[comment] =>
[add_date] => 07/30/2013 02:38 AM
[update_date] => 07/30/2013 02:38 AM
[path] => /home1/creatkj8/public_html/dev/fwingo/usersdata/saqlain/Home/Documents/
[trash_status] => 1
[password] => 123456
[expires_after] => 5
[allow_uploading] => 0
[share_date] => 13-09-2013
)
[5] => Array
(
[id] => 96
[parent_id] => 94
[dir_name] => Music
[size] =>
[owner_id] => 10
[shared_link] => http://creativevisionintl.com/dev/fwingo/usersdata/saqlain/Home/Music/
[shared] => 0
[comment] => dfdf
[add_date] => 07/30/2013 02:38 AM
[update_date] => 07/30/2013 02:38 AM
[path] => /home1/creatkj8/public_html/dev/fwingo/usersdata/saqlain/Home/Music/
[trash_status] => 1
[password] =>
[expires_after] => 0
[allow_uploading] => 0
[share_date] =>
)
[6] => Array
(
[id] => 233
[parent_id] => 94
[dir_name] => test
[size] => 3945040
[owner_id] => 10
[shared_link] => http://creativevisionintl.com/dev/fwingo/usersdata/saqlain/Home/test
[shared] => 0
[comment] =>
[add_date] => 09/06/2013 09:16 AM
[update_date] => 09/06/2013 09:16 AM
[path] => /home1/creatkj8/public_html/dev/fwingo/usersdata/saqlain/Home/test
[trash_status] => 1
[password] =>
[expires_after] => 0
[allow_uploading] => 0
[share_date] =>
)
)
2nd array:
Array
(
[0] => Array
(
[id] => 118
[name] => Desert.jpg
[size] => 845941
[type] => image/jpeg
[ext] => jpg
[shared] => 1
[shared_link] => http://creativevisionintl.com/dev/fwingo/usersdata/saqlain/Home//Desert.jpg
[comment] =>
[owner_id] => 10
[dir_id] => 94
[path] => /home1/creatkj8/public_html/dev/fwingo/usersdata/saqlain/Home/Desert.jpg
[del_path] => /home1/creatkj8/public_html/dev/fwingo/usersdata/saqlain/HomeDesert.jpg
[dated] => 09/05/2013 07:15 AM
[trash_status] => 1
[password] =>
[expires_after] => 2
[share_date] => 06-09-2013
)
[1] => Array
(
[id] => 120
[name] => test.zip
[size] => 4096
[type] => Zip Archeive
[ext] => zip
[shared] => 0
[shared_link] => /Documentstest.zip
[comment] =>
[owner_id] => 10
[dir_id] => 94
[path] => /Documentstest.zip
[del_path] => /Documentstest.zip
[dated] => 09/06/2013 09:21 AM
[trash_status] => 1
[password] =>
[expires_after] => 0
[share_date] =>
)
)
Note: i want to merge both arrays and after that display them using foreach loop both arrays have different columns.
Thanks. plz urgent replay.
let say $a is your first array and $b is your 2nd array, then for merging them you can do it by below code.
$master = array_merge($a,$b);
then use foreach loop for getting their content like below
foreach($master as $item){
echo $item;
}
First, to merge the arrays into one :
//Merge the arrays
$arrayEnd = array(); // define an array that will contain all the rows
//iterate on the first array assuming its name is $array1
foreach($array1 as $val) {
//add the current line named $val to the resulting array
$arrayEnd[] = $val;
}
//iterate on the first array assuming its name is $array1
foreach($array1 as $val) {
//add the current line named $val to the resulting array
$arrayEnd[] = $val;
}
Then you just need to iterate through the resulting array:
//Display the resulting array
foreach($arrayEnd as $val) {
echo $val['id']." - ".$val['name'];
}
For example merge two arrays
$a = array("red","green","yellow");
$b = array("a","b","c");
$c = array_merge($a,$b);
print_r($c);
foreach ($c as $key => $value) {
echo $value."<br>";
}