Sort Parent array by child value - php

How can I sort the below array by any of the child values (i.e: post_id, username, etc...)?
Array(
[0] => Array
(
[id] => 10
[post_id] => 398
[user_id] => 0
[view_date] => 2010-11-20 15:26:00
[views] => 4
[username] => <em>guest</em>
)
[1] => Array
(
[id] => 9
[post_id] => 397
[user_id] => 1
[view_date] => 2010-11-19 23:35:39
[views] => 1
[username] => Fire G
)
[2] => Array
(
[id] => 8
[post_id] => 398
[user_id] => 1
[view_date] => 2010-11-19 23:35:26
[views] => 4
[username] => Fire G
)
[3] => Array
(
[id] => 7
[post_id] => 396
[user_id] => 1
[view_date] => 2010-11-19 23:07:57
[views] => 3
[username] => Fire G
)
[4] => Array
(
[id] => 6
[post_id] => 396
[user_id] => 1
[view_date] => 2010-11-19 23:07:55
[views] => 3
[username] => Fire G
)
[5] => Array
(
[id] => 5
[post_id] => 396
[user_id] => 1
[view_date] => 2010-11-19 23:07:52
[views] => 3
[username] => Fire G
)
[6] => Array
(
[id] => 4
[post_id] => 13
[user_id] => 1
[view_date] => 2010-11-19 22:59:35
[views] => 1
[username] => Fire G
)
[7] => Array
(
[id] => 3
[post_id] => 3
[user_id] => 1
[view_date] => 2010-11-19 22:59:28
[views] => 1
[username] => Fire G
)
[8] => Array
(
[id] => 2
[post_id] => 398
[user_id] => 1
[view_date] => 2010-11-18 11:37:20
[views] => 4
[username] => Fire G
)
[9] => Array
(
[id] => 1
[post_id] => 398
[user_id] => 1
[view_date] => 2010-11-18 11:37:16
[views] => 4
[username] => Fire G
)
)

Use uasort, user-defined sorting that lets you provide your own function to define whether one element should come before another.
http://www.php.net/manual/en/function.uasort.php
Your comparator function would be something like
function cmp($a, $b) {
return $a['post_id'] > $b['post_id'] ? 1 : -1;
}

Related

Group multidimensional arrays by key

I'm stuck on this problem and I hope someone can help me on that.
I have an array which I want to group by a specific key. The only problem is that I want to have a new array whenever the value of the key changes during the loop. Here is an example of the array.
Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[2] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
[3] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[4] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[5] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[6] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
I did
foreach($report_items as $item)
{
$groupedItems[$item['grouped_by']][] = $item;
}
and I got
Array
(
[1] => Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[2] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[3] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[4] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
[4] => Array
(
[0] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[1] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
)
)
What I'm actually looking for is something like this, where the arrays are separated during the loop and a suffix is added to the key whenever the value changes.
Array
(
[1.1] => Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
[4.1] => Array
(
[0] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[1] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
)
[1.2] => Array
(
[0] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[2] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[3] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
)
I'm not really aware of a simple way to solve this, so I would appreciate any help. Thank you!
Not going to bother to recreate your full array here, I am using a reduced version with just the id and the grouped_by value, but the principle is of course exactly the same if you do it with your "full" array.
I am using a counter array to keep track of which "iteration" of a specific group we are currently dealing with. In classic control break implementation logic, I am comparing the grouped_by of the current record, with that of the previous one - if those differ, then the counter for this grouped_by value has to be incremented by 1. And then, I am simply creating the array key to use, by combining the grouped_by value, and the current count for it.
$data = [['id' => 972, 'grouped_by' => 1], ['id' => 664, 'grouped_by' => 4], ['id' => 631, 'grouped_by' => 4], ['id' => 630, 'grouped_by' => 1], ['id' => 971, 'grouped_by' => 1], ['id' => 973, 'grouped_by' => 1], ['id' => 974, 'grouped_by' => 1]];
$grouped_result = $grouped_by_counts = [];
$previous_grouped_by = null; // a value that will never occur in the data
foreach($data as $datum) {
if($datum['grouped_by'] !== $previous_grouped_by) {
$grouped_by_counts[$datum['grouped_by']] =
isset($grouped_by_counts[$datum['grouped_by']]) ?
$grouped_by_counts[$datum['grouped_by']] + 1 : 1;
}
$grouped_result[
$datum['grouped_by'].'.'.$grouped_by_counts[$datum['grouped_by']]
][] = $datum;
$previous_grouped_by = $datum['grouped_by'];
}
print_r($grouped_result);
Live example: https://3v4l.org/odtie

Build recursive array with multi level elements

I need to build multi nested array with recursive parent => child structure. This is structure of my base array from database:
Array
(
[0] => Array
(
[id] => 1
[alias] => 1_$6MEORB741t1Gx9VlkUbg_0
[title] => 1_$6MEORB741t1Gx9VlkUbg_0
[parent] => 0
)
[1] => Array
(
[id] => 2
[alias] => 2_49QaOoUw77f1z2I45SyzolKFHtAr_0
[title] => 2_49QaOoUw77f1z2I45SyzolKFHtAr_0
[parent] => 0
)
[2] => Array
(
[id] => 3
[alias] => 3_PReQ%9EKu--0mTzQX#ZR_pFdq3_0
[title] => 3_PReQ%9EKu--0mTzQX#ZR_pFdq3_0
[parent] => 0
)
[3] => Array
(
[id] => 6
[alias] => 6_%#e1ho_9fl6#aK$/E$HE6_0
[title] => 6_%#e1ho_9fl6#aK$/E$HE6_0
[parent] => 0
)
[4] => Array
(
[id] => 41
[alias] => 41_6k5-f4FGy$LT+B21MD#d1qh_0
[title] => 41_6k5-f4FGy$LT+B21MD#d1qh_0
[parent] => 0
)
[5] => Array
(
[id] => 44
[alias] => 44_kOL/-FYVMx4Od+kDx*7_0
[title] => 44_kOL/-FYVMx4Od+kDx*7_0
[parent] => 0
)
[6] => Array
(
[id] => 46
[alias] => 46_kh53wZMheD5cK%Kqb$oyEwBA5N%s8JI_0
[title] => 46_kh53wZMheD5cK%Kqb$oyEwBA5N%s8JI_0
[parent] => 0
)
[7] => Array
(
[id] => 31
[alias] => 31_6sOlnp#MncryS0-Q6Syh#%+NkUzywrC_1
[title] => 31_6sOlnp#MncryS0-Q6Syh#%+NkUzywrC_1
[parent] => 1
)
[8] => Array
(
[id] => 32
[alias] => 32__hH#L#HUL6uB+65zKsIa0XxA+lA4s/R_1
[title] => 32__hH#L#HUL6uB+65zKsIa0XxA+lA4s/R_1
[parent] => 1
)
[9] => Array
(
[id] => 33
[alias] => 33_Anq2U#2R1zVqi4qHD1kVePol_1
[title] => 33_Anq2U#2R1zVqi4qHD1kVePol_1
[parent] => 1
)
[10] => Array
(
[id] => 34
[alias] => 34_xwffzuToahNDaiOgFPc#l-lKvE19VJ#_1
[title] => 34_xwffzuToahNDaiOgFPc#l-lKvE19VJ#_1
[parent] => 1
)
[11] => Array
(
[id] => 35
[alias] => 35_+3uIEXWM8PQxDR+y*+*pfh*8eJvaHi_1
[title] => 35_+3uIEXWM8PQxDR+y*+*pfh*8eJvaHi_1
[parent] => 1
)
[12] => Array
(
[id] => 36
[alias] => 36_SeLju-iXc#8YqqxhUysQ_1
[title] => 36_SeLju-iXc#8YqqxhUysQ_1
[parent] => 1
)
[13] => Array
(
[id] => 37
[alias] => 37_NP8uJC$oiFMr70Hb9Q_1
[title] => 37_NP8uJC$oiFMr70Hb9Q_1
[parent] => 1
)
[14] => Array
(
[id] => 38
[alias] => 38_roIR1kTFM*HO5I2YfIXco_1
[title] => 38_roIR1kTFM*HO5I2YfIXco_1
[parent] => 1
)
[15] => Array
(
[id] => 39
[alias] => 39_8#p7WoRatc2I4k_Fc9iiAJZspgz_1
[title] => 39_8#p7WoRatc2I4k_Fc9iiAJZspgz_1
[parent] => 1
)
[16] => Array
(
[id] => 8
[alias] => 8_UmlPj$b0PUPE_IjL4Yo+*S_2
[title] => 8_UmlPj$b0PUPE_IjL4Yo+*S_2
[parent] => 2
)
[17] => Array
(
[id] => 9
[alias] => 9_EjG-LeftUra0V_Hwwk/j/CXhR_2
[title] => 9_EjG-LeftUra0V_Hwwk/j/CXhR_2
[parent] => 2
)
[18] => Array
(
[id] => 10
[alias] => 10_5M71NmD#nfgnzhaI-PnDI-*2sib_2
[title] => 10_5M71NmD#nfgnzhaI-PnDI-*2sib_2
[parent] => 2
)
[19] => Array
(
[id] => 11
[alias] => 11_Q/x1ORAONjvx/Q#c+sR$XS0_2
[title] => 11_Q/x1ORAONjvx/Q#c+sR$XS0_2
[parent] => 2
)
[20] => Array
(
[id] => 12
[alias] => 12_uf0VO8aE_7#GRWnYvLnIce9%O8J5M1o_2
[title] => 12_uf0VO8aE_7#GRWnYvLnIce9%O8J5M1o_2
[parent] => 2
)
[21] => Array
(
[id] => 13
[alias] => 13_LIWs_prfH#O/A$yr_2
[title] => 13_LIWs_prfH#O/A$yr_2
[parent] => 2
)
[22] => Array
(
[id] => 14
[alias] => 14_3oB%%$4FbPYLgsbiTiQy8fo#j90vT8S_2
[title] => 14_3oB%%$4FbPYLgsbiTiQy8fo#j90vT8S_2
[parent] => 2
)
[23] => Array
(
[id] => 15
[alias] => 15_18DynIH%bFwo%*nvogdQZ1Bm*rfs_2
[title] => 15_18DynIH%bFwo%*nvogdQZ1Bm*rfs_2
[parent] => 2
)
[24] => Array
(
[id] => 16
[alias] => 16_AI#kwK4e/AFla-s_6mKKQM_2
[title] => 16_AI#kwK4e/AFla-s_6mKKQM_2
[parent] => 2
)
[25] => Array
(
[id] => 17
[alias] => 17_Rtf7obmbv-SClvy#l#x*MEtl$A7_2
[title] => 17_Rtf7obmbv-SClvy#l#x*MEtl$A7_2
[parent] => 2
)
[26] => Array
(
[id] => 18
[alias] => 18_G3tba8ZDbZnSfrfJ3-$BnVdxg_q_2
[title] => 18_G3tba8ZDbZnSfrfJ3-$BnVdxg_q_2
[parent] => 2
)
[27] => Array
(
[id] => 23
[alias] => 23_ig1zkz8w/J#SD2TZ_UYU9_8
[title] => 23_ig1zkz8w/J#SD2TZ_UYU9_8
[parent] => 8
)
[28] => Array
(
[id] => 24
[alias] => 24_MjCMYvRE+7$kiCLmYj8opHsTqV0_8
[title] => 24_MjCMYvRE+7$kiCLmYj8opHsTqV0_8
[parent] => 8
)
[29] => Array
(
[id] => 25
[alias] => 25_Jn94#JheMQRg$9h#HD1JN-k%W5_8
[title] => 25_Jn94#JheMQRg$9h#HD1JN-k%W5_8
[parent] => 8
)
[30] => Array
(
[id] => 26
[alias] => 26_l_+_Q3bGBHF3JUer_8
[title] => 26_l_+_Q3bGBHF3JUer_8
[parent] => 8
)
[31] => Array
(
[id] => 27
[alias] => 27_/ybSUUr++TLAKm9N8xG8FQCrWW*-_8
[title] => 27_/ybSUUr++TLAKm9N8xG8FQCrWW*-_8
[parent] => 8
)
[32] => Array
(
[id] => 28
[alias] => 28_EiRh83le0C8Etr5JxVgG4V/0$q_8
[title] => 28_EiRh83le0C8Etr5JxVgG4V/0$q_8
[parent] => 8
)
[33] => Array
(
[id] => 29
[alias] => 29_Fc-k7Bo8Py8M$_N#oAXZ_8
[title] => 29_Fc-k7Bo8Py8M$_N#oAXZ_8
[parent] => 8
)
[34] => Array
(
[id] => 30
[alias] => 30_6mr1uGtk2BBiyH-r5NgKh90udT1_8
[title] => 30_6mr1uGtk2BBiyH-r5NgKh90udT1_8
[parent] => 8
)
[35] => Array
(
[id] => 19
[alias] => 19_8UhvJLSYjUXERKC#ui_15
[title] => 19_8UhvJLSYjUXERKC#ui_15
[parent] => 15
)
[36] => Array
(
[id] => 20
[alias] => 20_zPSW#YdHV2YpT##b_15
[title] => 20_zPSW#YdHV2YpT##b_15
[parent] => 15
)
[37] => Array
(
[id] => 21
[alias] => 21_2-8n54Uk_#3V4pC2rd%0Z4bxYJHzHiDS_15
[title] => 21_2-8n54Uk_#3V4pC2rd%0Z4bxYJHzHiDS_15
[parent] => 15
)
[38] => Array
(
[id] => 22
[alias] => 22_487ALLt8%$KaQHu8_uEQcXd_15
[title] => 22_487ALLt8%$KaQHu8_uEQcXd_15
[parent] => 15
)
[39] => Array
(
[id] => 42
[alias] => 42_JILU6%CeT-*oH*VvAExEXfI_41
[title] => 42_JILU6%CeT-*oH*VvAExEXfI_41
[parent] => 41
)
)
I am trying to make it with needable structure, but it have only one nested level. And last array elements don't have any ['title'] or ['alias'] anymore, for some reason they are empty.
Array
(
[0] => Array
(
[id] => 1
[alias] => 1_NhTYh1gUeq+aPgITUe#Ue3*_0
[title] => 1_NhTYh1gUeq+aPgITUe#Ue3*_0
[parent] => 0
[depth] => 0
)
[1] => Array
(
[id] => 2
[alias] => 2_5bs1UwWeTqE-USEdP27dyl_0
[title] => 2_5bs1UwWeTqE-USEdP27dyl_0
[parent] => 0
[depth] => 0
[child] => Array
(
[31] => Array
(
[id] => 31
[alias] => 31_#iV2wXID3z$A-#4-whrhqMCWn$_JS37T_1
[title] => 31_#iV2wXID3z$A-#4-whrhqMCWn$_JS37T_1
[parent] => 1
[depth] => 1
)
[32] => Array
(
[id] => 32
[alias] => 32_%lBpA5s-CMr2-_y/0XygzsT1je_1
[title] => 32_%lBpA5s-CMr2-_y/0XygzsT1je_1
[parent] => 1
[depth] => 1
)
[33] => Array
(
[id] => 33
[alias] => 33_$8WmYjxwhKmWJPVKr3n-T6lOtJ-%O/T_1
[title] => 33_$8WmYjxwhKmWJPVKr3n-T6lOtJ-%O/T_1
[parent] => 1
[depth] => 1
)
[34] => Array
(
[id] => 34
[alias] => 34_-NrDtGtSTs6QQ%C8D1qQVomYGtrA3KR_1
[title] => 34_-NrDtGtSTs6QQ%C8D1qQVomYGtrA3KR_1
[parent] => 1
[depth] => 1
)
[35] => Array
(
[id] => 35
[alias] => 35_M-3Z8$RjDnEAX%/d_1
[title] => 35_M-3Z8$RjDnEAX%/d_1
[parent] => 1
[depth] => 1
)
[36] => Array
(
[id] => 36
[alias] => 36_3mG2ZZ8E6/xqafYi#yH/_btGNN_1
[title] => 36_3mG2ZZ8E6/xqafYi#yH/_btGNN_1
[parent] => 1
[depth] => 1
)
[37] => Array
(
[id] => 37
[alias] => 37_TgZPOGOn/q#5z%%uF#-xJxD-%eB9_1
[title] => 37_TgZPOGOn/q#5z%%uF#-xJxD-%eB9_1
[parent] => 1
[depth] => 1
)
[38] => Array
(
[id] => 38
[alias] => 38_aAL3XY073HAF-LOCiCrUX5BK-36_1
[title] => 38_aAL3XY073HAF-LOCiCrUX5BK-36_1
[parent] => 1
[depth] => 1
)
[39] => Array
(
[id] => 39
[alias] => 39_V4m14DvYH+CYAujhG$_1
[title] => 39_V4m14DvYH+CYAujhG$_1
[parent] => 1
[depth] => 1
)
)
)
[2] => Array
(
[id] => 3
[alias] => 3_Vf-H5jJqh8BI7_rdyQ4*T+d#3W3_0
[title] => 3_Vf-H5jJqh8BI7_rdyQ4*T+d#3W3_0
[parent] => 0
[depth] => 0
[child] => Array
(
[8] => Array
(
[id] => 8
[alias] => 8_d%aNC6+UrXC-h-RKMVnyToi5a2q1C_2
[title] => 8_d%aNC6+UrXC-h-RKMVnyToi5a2q1C_2
[parent] => 2
[depth] => 1
)
[9] => Array
(
[id] => 9
[alias] => 9_Lv_VdDX13CR25c9#0r_2
[title] => 9_Lv_VdDX13CR25c9#0r_2
[parent] => 2
[depth] => 1
)
[10] => Array
(
[id] => 10
[alias] => 10_M017XoFg8/R0C1BnPxAG5QFr9_2
[title] => 10_M017XoFg8/R0C1BnPxAG5QFr9_2
[parent] => 2
[depth] => 1
)
[11] => Array
(
[id] => 11
[alias] => 11_c1%#p4oPN03T#-l9jR$Xs%LwT_2
[title] => 11_c1%#p4oPN03T#-l9jR$Xs%LwT_2
[parent] => 2
[depth] => 1
)
[12] => Array
(
[id] => 12
[alias] => 12_N3b3q%#+z_zA4yOz21xQJP+m-%Cf_2
[title] => 12_N3b3q%#+z_zA4yOz21xQJP+m-%Cf_2
[parent] => 2
[depth] => 1
)
[13] => Array
(
[id] => 13
[alias] => 13_V0dT%mxNCtvo$vIeKS-LLZ-_2
[title] => 13_V0dT%mxNCtvo$vIeKS-LLZ-_2
[parent] => 2
[depth] => 1
)
[14] => Array
(
[id] => 14
[alias] => 14_#*HZghpHwL446h%EAh4i/V_2
[title] => 14_#*HZghpHwL446h%EAh4i/V_2
[parent] => 2
[depth] => 1
)
[15] => Array
(
[id] => 15
[alias] => 15_dljC2PL1DwlA89aXMvv+-%#6tOW09-4_2
[title] => 15_dljC2PL1DwlA89aXMvv+-%#6tOW09-4_2
[parent] => 2
[depth] => 1
)
[16] => Array
(
[id] => 16
[alias] => 16_AFZQSkFnWVPLT*/Bj_2
[title] => 16_AFZQSkFnWVPLT*/Bj_2
[parent] => 2
[depth] => 1
)
[17] => Array
(
[id] => 17
[alias] => 17_YVoiXtCToZ-rSzr3+ph#_2
[title] => 17_YVoiXtCToZ-rSzr3+ph#_2
[parent] => 2
[depth] => 1
)
[18] => Array
(
[id] => 18
[alias] => 18_2KP2yEro#a/j/C#tvWu%FC9_2
[title] => 18_2KP2yEro#a/j/C#tvWu%FC9_2
[parent] => 2
[depth] => 1
)
)
)
[3] => Array
(
[id] => 6
[alias] => 6_b-yfo3qYQy-zuy_8GKDcf68G3P$_0
[title] => 6_b-yfo3qYQy-zuy_8GKDcf68G3P$_0
[parent] => 0
[depth] => 0
)
[4] => Array
(
[id] => 41
[alias] => 41_C5eS+rZF#CfaSjZ#k98zNt_0
[title] => 41_C5eS+rZF#CfaSjZ#k98zNt_0
[parent] => 0
[depth] => 0
)
[5] => Array
(
[id] => 44
[alias] => 44_i$plAaN1J6L-M*De#j/7P_pjWBmP_0
[title] => 44_i$plAaN1J6L-M*De#j/7P_pjWBmP_0
[parent] => 0
[depth] => 0
)
[6] => Array
(
[id] => 46
[alias] => 46_qzfW+xg%T1O9F4K_+%Ef#TT0mO2u_0
[title] => 46_qzfW+xg%T1O9F4K_+%Ef#TT0mO2u_0
[parent] => 0
[depth] => 0
)
[8] => Array
(
[child] => Array
(
[23] => Array
(
[id] => 23
[alias] => 23_%q3#gkGhD_R2msKavVG_8
[title] => 23_%q3#gkGhD_R2msKavVG_8
[parent] => 8
[depth] => 1
)
[24] => Array
(
[id] => 24
[alias] => 24_ilxGSzJU#%VIdiEaQ_8
[title] => 24_ilxGSzJU#%VIdiEaQ_8
[parent] => 8
[depth] => 1
)
[25] => Array
(
[id] => 25
[alias] => 25_NUyuJ/LH*ag4DdKb2Dv9/I2m_8
[title] => 25_NUyuJ/LH*ag4DdKb2Dv9/I2m_8
[parent] => 8
[depth] => 1
)
[26] => Array
(
[id] => 26
[alias] => 26_Q7gKTrq#akhlecg9i-UX_8
[title] => 26_Q7gKTrq#akhlecg9i-UX_8
[parent] => 8
[depth] => 1
)
[27] => Array
(
[id] => 27
[alias] => 27_r%dUQ0ceNL8SpjxbnO7U6V%dZIHzN_jx_8
[title] => 27_r%dUQ0ceNL8SpjxbnO7U6V%dZIHzN_jx_8
[parent] => 8
[depth] => 1
)
[28] => Array
(
[id] => 28
[alias] => 28_Hzu9w*5%o#Nk7ZblSB4t$Xp_8
[title] => 28_Hzu9w*5%o#Nk7ZblSB4t$Xp_8
[parent] => 8
[depth] => 1
)
[29] => Array
(
[id] => 29
[alias] => 29_O2EecAUe$tdOQ+/Zbp0SH#x0bHxV_8
[title] => 29_O2EecAUe$tdOQ+/Zbp0SH#x0bHxV_8
[parent] => 8
[depth] => 1
)
[30] => Array
(
[id] => 30
[alias] => 30_plqN4QwSdUJpEe4E-4Pqy4A_8
[title] => 30_plqN4QwSdUJpEe4E-4Pqy4A_8
[parent] => 8
[depth] => 1
)
)
)
[15] => Array
(
[child] => Array
(
[19] => Array
(
[id] => 19
[alias] => 19_q1$-DC4agbkN9m2$_15
[title] => 19_q1$-DC4agbkN9m2$_15
[parent] => 15
[depth] => 1
)
[20] => Array
(
[id] => 20
[alias] => 20_lkrWn*RlkV5fJTZdB9J4D/y$S8_15
[title] => 20_lkrWn*RlkV5fJTZdB9J4D/y$S8_15
[parent] => 15
[depth] => 1
)
[21] => Array
(
[id] => 21
[alias] => 21_-zUwInzm*tZ-P0vk3$sg54LLh6Dn_15
[title] => 21_-zUwInzm*tZ-P0vk3$sg54LLh6Dn_15
[parent] => 15
[depth] => 1
)
[22] => Array
(
[id] => 22
[alias] => 22_R6YJQLjqp-lmozYSM4uhGBL4_15
[title] => 22_R6YJQLjqp-lmozYSM4uhGBL4_15
[parent] => 15
[depth] => 1
)
)
)
[41] => Array
(
[child] => Array
(
[42] => Array
(
[id] => 42
[alias] => 42_cMScoiEeD1#bwSI$S_41
[title] => 42_cMScoiEeD1#bwSI$S_41
[parent] => 41
[depth] => 1
)
)
)
)
My code.
foreach ($data as $key => $item) {
if ($item['parent'] != 0) {
$item['depth'] = 1;
$temp = $item;
unset($data[$key]);
$data[$temp['parent']]['child'][$temp['id']] = $temp;
} else {
$data[$key]['depth'] = 0;
}
}
return $data;
How fix it and make this array multilevel?

How to compare Associative Array in CodeIgniter

Array
(
[records] => Array
(
[0] => stdClass Object
(
[id] => 6
[fname] => hardik
[type] => Active
[rid] => 1
[rname] => Principle
[uid] => 48
)
[1] => stdClass Object
(
[id] => 12
[fname] => gautam
[type] => De-Active
[rid] => 1
[rname] => Principle
[uid] => 54
)
[2] => stdClass Object
(
[id] => 10
[fname] => burhan
[type] => Active
[rid] => 2
[rname] => Teacher
[uid] => 52
)
[3] => stdClass Object
(
[id] => 13
[fname] => gautam
[type] => De-Active
[rid] => 2
[rname] => Teacher
[uid] => 54
)
[4] => stdClass Object
(
[id] => 7
[fname] => abc
[password] => d41d8cd98f00b204e9800998ecf8427e
[type] => Active
[rid] => 3
[rname] => Parent
[uid] => 49
)
)
)
How can I compare associative array in Codeigniter for remove duplicate user?

Zend paginator not working properly when array format is not as it should be

Hi my zend paginator works well when the $business_list array format is like below.
but when the array format changes it displays only one page instead of many pages.
$paginator = Zend_Paginator::factory($business_list);
Array
(
[0] => Array
(
[id] => 216
[userid] => 141
[title] => first req
[image] =>
[logo] =>
[description] =>
this is the first requirment
[date] => 2012-06-12 10:31:01
[area] => 1
[budget] => 1
[type] => 1
[status] => 1
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 315
[business_id] => 216
[section_id] => 1
[subsection_id] => 13
[description] =>
)
[1] => Array
(
[id] => 316
[business_id] => 216
[section_id] => 3
[subsection_id] => 14
[description] =>
)
[2] => Array
(
[id] => 317
[business_id] => 216
[section_id] => 4
[subsection_id] => 15
[description] =>
)
)
[CmUser] => Array
(
[id] => 141
[username] => venki
[password] =>
[firstname] => venkatesh
[lastname] => abus
[image] => 54winter.jpg
[email] => xxx#xx.com
[phone] => 23423452
[group_id] => 2
[status] =>
[registered_date] => 2012-06-04 06:32:58
[last_visit] => 0000-00-00 00:00:00
[is_active] => 1
[subscribe] => 1
)
)
[1] => Array
(
[id] => 214
[userid] => 98
[title] => gopicontractor
[image] => 54bluehills.jpg
[logo] => 239waterlilies.jpg
[description] =>
TIt uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is
[date] => 2012-06-11 12:18:58
[area] => 1
[budget] => 1
[type] => 3
[status] => 1
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 305
[business_id] => 214
[section_id] => 1
[subsection_id] => 5
[description] =>
)
[1] => Array
(
[id] => 306
[business_id] => 214
[section_id] => 1
[subsection_id] => 6
[description] =>
)
[2] => Array
(
[id] => 307
[business_id] => 214
[section_id] => 3
[subsection_id] => 1
[description] =>
)
[3] => Array
(
[id] => 308
[business_id] => 214
[section_id] => 4
[subsection_id] => 9
[description] =>
)
)
[CmUser] => Array
(
[id] => 98
[username] => gopi.s
[password] =>
[firstname] => venkatesh
[lastname] => franc
[image] =>
[email] => ss#ss.com
[phone] => 23423452
[group_id] => 3
[status] =>
[registered_date] => 2012-05-16 12:36:57
[last_visit] => 0000-00-00 00:00:00
[is_active] => 1
[subscribe] => 1
)
)
)
the above array format displays paginatin correctly.
This is the array that displays only one page in pagination.
Zend_Paginator Object
(
[_cacheEnabled:protected] => 1
[_adapter:protected] => Zend_Paginator_Adapter_Array Object
(
[_array:protected] => Array
(
[0] => Array
(
[id] => 89
[username] => xx
[password] => xx
[firstname] => xx
[lastname] => xx
[image] =>
[email] => xx#ymail.com
[phone] => 2342345
[group_id] => 2
[status] => offline
[registered_date] => 2012-05-15 10:58:53
[last_visit] =>
[is_active] => 1
[subscribe] => 0
[CmBusiness] => Array
(
[0] => Array
(
[id] => 204
[userid] => 89
[title] => xxhousing
[image] => 760067.jpg
[logo] => xx_818f3c97e6_o.jpg
[description] => xx
[date] => 2012-05-31 13:36:17
[area] => 1
[budget] => 1
[type] => 1
[status] => 1
[CmAmount] => Array
(
[id] => 1
[amount] => 1000-1500
[status] => 1
)
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 251
[business_id] => 204
[section_id] => 1
[subsection_id] => 6
[description] => xx
)
[1] => Array
(
[id] => 252
[business_id] => 204
[section_id] => 3
[subsection_id] => 2
[description] => xx
)
[2] => Array
(
[id] => 253
[business_id] => 204
[section_id] => 3
[subsection_id] => 4
[description] => xx
)
[3] => Array
(
[id] => 254
[business_id] => 204
[section_id] => 4
[subsection_id] => 9
[description] => xx
)
)
[CmArea] => Array
(
[id] => 1
[area] => manchester
[status] => 1
)
[CmType] => Array
(
[id] => 1
[type] => Personal business
[status] => 1
)
)
[1] => Array
(
[id] => 205
[userid] => 89
[title] => xx
[image] => 41217850-desktop-wallpapers-new-windows-xp.jpg
[logo] => 356anger_n.jpg
[description] => xx
[date] => 2012-05-31 13:37:15
[area] => 1
[budget] => 1
[type] => 3
[status] => 1
[CmAmount] => Array
(
[id] => 1
[amount] => 1000-1500
[status] => 1
)
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 255
[business_id] => 205
[section_id] => 1
[subsection_id] => 6
[description] =>xx
)
[1] => Array
(
[id] => 256
[business_id] => 205
[section_id] => 3
[subsection_id] => 1
[description] => xx
)
[2] => Array
(
[id] => 257
[business_id] => 205
[section_id] => 3
[subsection_id] => 2
[description] => xx
)
[3] => Array
(
[id] => 258
[business_id] => 205
[section_id] => 4
[subsection_id] => 10
[description] => xx
)
)
[CmArea] => Array
(
[id] => 1
[area] => manchester
[status] => 1
)
[CmType] => Array
(
[id] => 3
[type] => Bilde companies
[status] => 1
)
)
[2] => Array
(
[id] => 206
[userid] => 89
[title] => Nuestros recursos
[image] =>
[logo] =>
[description] => xx
[date] => 2012-05-31 13:38:04
[area] => 1
[budget] => 1
[type] => 4
[status] => 1
[CmAmount] => Array
(
[id] => 1
[amount] => 1000-1500
[status] => 1
)
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 259
[business_id] => 206
[section_id] => 1
[subsection_id] => 5
[description] =>
)
[1] => Array
(
[id] => 260
[business_id] => 206
[section_id] => 1
[subsection_id] => 6
[description] =>
)
[2] => Array
(
[id] => 261
[business_id] => 206
[section_id] => 3
[subsection_id] => 2
[description] =>
)
[3] => Array
(
[id] => 262
[business_id] => 206
[section_id] => 4
[subsection_id] => 10
[description] =>
)
)
[CmArea] => Array
(
[id] => 1
[area] => manchester
[status] => 1
)
[CmType] => Array
(
[id] => 4
[type] => Designer
[status] => 1
)
)
)
)
)
[_count:protected] => 1
)
[_currentItemCount:protected] =>
[_currentItems:protected] =>
[_currentPageNumber:protected] => 1
[_filter:protected] =>
[_itemCountPerPage:protected] => 2
[_pageCount:protected] => 1
[_pageRange:protected] =>
[_pages:protected] =>
[_view:protected] =>
)

Removing an array result

[5] => Array
(
[id] => 29372
[product_id] => Array
(
[0] => stdClass Object
(
[id] => 1469
[type_id] => 1
[title] => Hearth 2 Hearth
[cover] => 21cf9d7d09d403251ba5d01ff33cd089.jpg
[coverid] => 1178
[inserted_by] => 0
[inserted_date] => 2011-02-11 13:55:23
[status_id] => 0
)
)
[variable_id] => 9
[variable_value] => 2011-02-11
[master_value] =>
[released_date] => 2011-02-11
[price] => Array
(
[0] => Array
(
[product_id] => 1469
[media_format] => VCD
[price] => 29000
[discount] => 5
)
)
[media_format] => VCD
)
[6] => Array
(
[id] => 30074
[product_id] => Array
(
[0] => stdClass Object
(
[id] => 1470
[type_id] => 1
[title] => Hearth 2 Hearth
[cover] => 149ddd4d1d5e567c1300d4831323e1c5.jpg
[coverid] => 1177
[inserted_by] => 6
[inserted_date] => 2011-02-16 15:18:58
[status_id] => 0
)
)
[variable_id] => 9
[variable_value] => 2011-02-11
[master_value] =>
[released_date] => 2011-02-11
[price] => Array
(
[0] => Array
(
[product_id] => 1470
[media_format] => DVD
[price] => 39000
[discount] => 0
)
)
[media_format] => DVD
)
I want the result by media_format = DVD so the whole array is gone, is there anyway to delete an array or remove it?
The same way you "delete" any variable:
unset($array[$index]);

Categories