Build recursive array with multi level elements - php
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?
Related
Convert php array to select box options
I have an input category array with childs as follows I want to convert it using a recursive function to another array like OUTPUT NEEDED ['1'=>'fashion', '10' => 'fashion > women','23'=> 'fashion > women > clothing','29'=> 'fashion > women > clothing > dresses' ... and so on ] My purpose is to use output array to populate select box options to adda category. INPUT ARRAY Array ( [1] => Array ( [id] => 1 [name] => fashion [parent_id] => 0 [childs] => Array ( [10] => Array ( [id] => 10 [name] => women [parent_id] => 1 [childs] => Array ( [23] => Array ( [id] => 23 [name] => clothing [parent_id] => 10 [childs] => Array ( [29] => Array ( [id] => 29 [name] => dresses [parent_id] => 23 [childs] => Array ( ) ) [30] => Array ( [id] => 30 [name] => jumpsuits [parent_id] => 23 [childs] => Array ( ) ) ) ) [24] => Array ( [id] => 24 [name] => bags & accessories [parent_id] => 10 [childs] => Array ( ) ) [25] => Array ( [id] => 25 [name] => shoes [parent_id] => 10 [childs] => Array ( ) ) [26] => Array ( [id] => 26 [name] => watches [parent_id] => 10 [childs] => Array ( ) ) [27] => Array ( [id] => 27 [name] => jewelery [parent_id] => 10 [childs] => Array ( ) ) [28] => Array ( [id] => 28 [name] => eye-wear [parent_id] => 10 [childs] => Array ( ) ) ) ) [11] => Array ( [id] => 11 [name] => men [parent_id] => 1 [childs] => Array ( ) ) [12] => Array ( [id] => 12 [name] => kids [parent_id] => 1 [childs] => Array ( ) ) [13] => Array ( [id] => 13 [name] => sports [parent_id] => 1 [childs] => Array ( ) ) [14] => Array ( [id] => 14 [name] => bags [parent_id] => 1 [childs] => Array ( ) ) [15] => Array ( [id] => 15 [name] => eyewear [parent_id] => 1 [childs] => Array ( ) ) [16] => Array ( [id] => 16 [name] => watches & jewelery [parent_id] => 1 [childs] => Array ( ) ) ) ) [2] => Array ( [id] => 2 [name] => supermarket [parent_id] => 0 [childs] => Array ( [17] => Array ( [id] => 17 [name] => food & beverages [parent_id] => 2 [childs] => Array ( [31] => Array ( [id] => 31 [name] => breakfast [parent_id] => 17 [childs] => Array ( ) ) [32] => Array ( [id] => 32 [name] => snacks [parent_id] => 17 [childs] => Array ( ) ) ) ) [18] => Array ( [id] => 18 [name] => dairy products [parent_id] => 2 [childs] => Array ( ) ) [19] => Array ( [id] => 19 [name] => beauty [parent_id] => 2 [childs] => Array ( ) ) [20] => Array ( [id] => 20 [name] => homecare [parent_id] => 2 [childs] => Array ( ) ) [21] => Array ( [id] => 21 [name] => baby world [parent_id] => 2 [childs] => Array ( ) ) [22] => Array ( [id] => 22 [name] => pet world [parent_id] => 2 [childs] => Array ( ) ) ) ) [3] => Array ( [id] => 3 [name] => electronics [parent_id] => 0 [childs] => Array ( [33] => Array ( [id] => 33 [name] => laptops [parent_id] => 3 [childs] => Array ( ) ) [34] => Array ( [id] => 34 [name] => television [parent_id] => 3 [childs] => Array ( ) ) ) ) [4] => Array ( [id] => 4 [name] => mobiles & tablets [parent_id] => 0 [childs] => Array ( [35] => Array ( [id] => 35 [name] => mobiles [parent_id] => 4 [childs] => Array ( ) ) [36] => Array ( [id] => 36 [name] => tablets [parent_id] => 4 [childs] => Array ( ) ) ) ) [5] => Array ( [id] => 5 [name] => baby & toys [parent_id] => 0 [childs] => Array ( ) ) [6] => Array ( [id] => 6 [name] => home [parent_id] => 0 [childs] => Array ( ) ) [7] => Array ( [id] => 7 [name] => perfumes & beauty [parent_id] => 0 [childs] => Array ( ) ) [8] => Array ( [id] => 8 [name] => sports & fitness [parent_id] => 0 [childs] => Array ( ) ) [9] => Array ( [id] => 9 [name] => automotive [parent_id] => 0 [childs] => Array ( ) ) ) I need an output array with key,the category id
You can create array by this way: $array = array( 'category_1' => 'value', 'category_2' => array( 'subcategory_1' => 'value', 'subcategory_2' => 'value', ), );
Transversing a materialized view by path
I get an array as shown below using this query: SELECT id, person_id, description, path FROM comment WHERE path <# '0001' ORDER by path My question is how am I suppose to use the to display the output as threaded? Do I check the first four numbers and increment to see if they are in the same level or is there something that I'm failing to understand? [0] => Array ( [id] => 1 [person_id] => 1 [description] => a75e62536ca2687ae5817acb739e61f3 [path] => 0001 ) [1] => Array ( [id] => 2 [person_id] => 2 [description] => 010242f78cd5d41abb16a8ba488957a1 [path] => 0001.0001.0001 ) [2] => Array ( [id] => 3 [person_id] => 2 [description] => cdde38b2b13ee808ce5423cdf8d14bab [path] => 0001.0001.0001.0001 ) [3] => Array ( [id] => 4 [person_id] => 1 [description] => 6b837ce73896fbd080bd0b93837ad537 [path] => 0001.0001.0001.0002 ) [4] => Array ( [id] => 5 [person_id] => 5 [description] => e5694a273ed95d36934cff48cd8fb12a [path] => 0001.0001.0001.0003 ) [5] => Array ( [id] => 6 [person_id] => 6 [description] => 15b2ad371934352c0b16c5c1585f3d7d [path] => 0001.0002 ) [6] => Array ( [id] => 7 [person_id] => 6 [description] => 2d1595f0c4ec527c601f404c7a272407 [path] => 0001.0002.0001 ) [7] => Array ( [id] => 8 [person_id] => 6 [description] => 8286c3bbe511d866e4e2ef4321a429d7 [path] => 0001.0003 ) [8] => Array ( [id] => 9 [person_id] => 8 [description] => dc455dd484220176d1d2198f84bdbc90 [path] => 0001.0003.0001 ) [9] => Array ( [id] => 10 [person_id] => 9 [description] => b8a8f41b224f900f6f5de0e591ba13b8 [path] => 0001.0003.0002 ) [10] => Array ( [id] => 11 [person_id] => 11 [description] => ef132c4663961ff09ca61f40eec72697 [path] => 0001.0003.0002.0001 )
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]);
PHP: Sort an array
I've got an array with data from a MySQL table in nested set model I'd like to get sorted, not only alphabetical but also with the child nodes directly after the parent node. Example - array to be sorted (before the sorting): Array ( [0] => Array ( [id] => 1 [name] => Kompetenser [parent] => 0 [depth] => 0 ) [1] => Array ( [id] => 2 [name] => Administration [parent] => 1 [depth] => 1 ) [2] => Array ( [id] => 11 [name] => Organisation [parent] => 2 [depth] => 2 ) [3] => Array ( [id] => 4 [name] => Arbetsledning [parent] => 2 [depth] => 2 ) [4] => Array ( [id] => 17 [name] => Planering [parent] => 2 [depth] => 2 ) [5] => Array ( [id] => 9 [name] => Hantverke [parent] => 1 [depth] => 1 ) [6] => Array ( [id] => 10 [name] => Snickeri [parent] => 9 [depth] => 2 ) [7] => Array ( [id] => 12 [name] => Språk [parent] => 1 [depth] => 1 ) [8] => Array ( [id] => 13 [name] => Tolk [parent] => 12 [depth] => 2 ) [9] => Array ( [id] => 15 [name] => Arabiska [parent] => 13 [depth] => 3 ) [10] => Array ( [id] => 14 [name] => Persiska [parent] => 13 [depth] => 3 ) [11] => Array ( [id] => 16 [name] => Polska [parent] => 13 [depth] => 3 ) [12] => Array ( [id] => 18 [name] => Apotekare [parent] => 1 [depth] => 1 ) [13] => Array ( [id] => 19 [name] => Dotkorand [parent] => 1 [depth] => 1 ) [14] => Array ( [id] => 21 [name] => Atomfysik [parent] => 19 [depth] => 2 ) [15] => Array ( [id] => 20 [name] => Fysik [parent] => 19 [depth] => 2 ) [16] => Array ( [id] => 22 [name] => Ekonom [parent] => 1 [depth] => 1 ) [17] => Array ( [id] => 23 [name] => Industriell ekonomi [parent] => 22 [depth] => 2 ) [18] => Array ( [id] => 24 [name] => Filosofi [parent] => 1 [depth] => 1 ) ) I want the array this way (after the sorting): Array ( [0] => Array ( [id] => 1 [name] => Kompetenser [parent] => 0 [depth] => 0 ) [1] => Array ( [id] => 2 [name] => Administration [parent] => 1 [depth] => 1 ) [3] => Array ( [id] => 4 [name] => Arbetsledning [parent] => 2 [depth] => 2 ) [2] => Array ( [id] => 11 [name] => Organisation [parent] => 2 [depth] => 2 ) [4] => Array ( [id] => 17 [name] => Planering [parent] => 2 [depth] => 2 ) [12] => Array ( [id] => 18 [name] => Apotekare [parent] => 1 [depth] => 1 ) [13] => Array ( [id] => 19 [name] => Dotkorand [parent] => 1 [depth] => 1 ) [14] => Array ( [id] => 21 [name] => Atomfysik [parent] => 19 [depth] => 2 ) [15] => Array ( [id] => 20 [name] => Fysik [parent] => 19 [depth] => 2 ) [16] => Array ( [id] => 22 [name] => Ekonom [parent] => 1 [depth] => 1 ) [17] => Array ( [id] => 23 [name] => Industriell ekonomi [parent] => 22 [depth] => 2 ) [18] => Array ( [id] => 24 [name] => Filosofi [parent] => 1 [depth] => 1 ) [5] => Array ( [id] => 9 [name] => Hantverke [parent] => 1 [depth] => 1 ) [6] => Array ( [id] => 10 [name] => Snickeri [parent] => 9 [depth] => 2 ) [7] => Array ( [id] => 12 [name] => Språk [parent] => 1 [depth] => 1 ) [8] => Array ( [id] => 13 [name] => Tolk [parent] => 12 [depth] => 2 ) [9] => Array ( [id] => 15 [name] => Arabiska [parent] => 13 [depth] => 3 ) [10] => Array ( [id] => 14 [name] => Persiska [parent] => 13 [depth] => 3 ) [11] => Array ( [id] => 16 [name] => Polska [parent] => 13 [depth] => 3 ) ) As you might see, I want all posts with parent 2 directly after the post with id 2, and so on. Any help would be highly appreciated. Thank you in advance.
Don't do this in PHP! The MySQL server is specificly designed to query AND sort data, read up on the MySQL "ORDER BY" syntax. Doing this on the MySQL server will save run-time, CPU Load and Memory Consumption.
Use php's uasort() function to define your own comparison function. But using MySQL's sorting capabilities would be more appropriate, if it's possible in your case.
You want to sort it like it is in the DB, multilayered. I don't think uasort can help with this problem because you want to attach the children to the parent. foreach ($arr as &$val) { $arr2[$val['id']] = &$val; } ksort($arr2); foreach ($arr2 as $id => &$val) { $parent = $val['parent']; if ($parent == 0) { continue; } $arr2[$parent]['children'][$id] = &$val; } function flattenArrayByChildren($arr) { foreach ($arr as $id => $val) { if (isset($val['children'])) { $temp = flattenArrayByChildren($val['children']); unset($val['children']); $out[$id] = $val; $out = $out + $temp; } else { $out[$id] = $val; } } return $out; } $arr2 = array(1 => $arr2[1]); $out = flattenArrayByChildren($arr2); var_dump($out); If you absolutely want to save the key, you can just add that to the $val in the first foreach, and retrieve it in the recursive function flattenArrayByChildren and use as key.
Problem solved - I made two simple functions. I hope other people could have use of this as well: class data_comp { var $fetched_tree = array(); function tree_fetch($parent = 0) { $query = 'SELECT node.id, node.name, node.parent, (COUNT(parent.name) - 1) AS depth FROM test_competence AS node, test_competence AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt GROUP BY node.name ORDER BY node.name'; $result = mysql_query($query) or die(mysql_error()); $tree = array(); while($data = mysql_fetch_assoc($result)) { $tree[$data['parent']][$data['id']] = array('name' => $data['name'], 'depth' => $data['depth']); } $this->tree_print($tree, $parent); } function tree_print($tree, $parent) { foreach($tree[$parent] as $id => $value) { $this->fetched_tree[] = array('id' => $id, 'name' => $value['name'], 'depth' => $value['depth']); if(isset($tree[$id]) && is_array($tree[$id])) { $this->tree_print($tree, $id); } } } } Thank you for your time. Any improvements are more than welcome.