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
)
Related
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?
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',
),
);
Am trying to loop through an array,with print_r I get the array below but am getting
"Notice: Undefined index: name"
error, when I ran my loop, dunno what am doing wrong?
Kindly help
Array
(
[0] => Array
(
[0] => Array
(
[ID] => 1
[name] => Paul
[email] => 32
)
[1] => Array
(
[ID] => 2
[name] => Allen
[email] => 25
)
[2] => Array
(
[ID] => 3
[name] => Teddy
[email] => 23
)
[3] => Array
(
[ID] => 4
[name] => 4
[email] => Mark
)
[4] => Array
(
[ID] => 5
[name] => Paul
[email] => 32
)
[5] => Array
(
[ID] => 6
[name] => Allen
[email] => 25
)
[6] => Array
(
[ID] => 7
[name] => Teddy
[email] => 23
)
[7] => Array
(
[ID] => 8
[name] => 4
[email] => Mark
)
[8] => Array
(
[ID] => 9
[name] => Paul
[email] => 32
)
[9] => Array
(
[ID] => 10
[name] => Allen
[email] => 25
)
[10] => Array
(
[ID] => 11
[name] => Teddy
[email] => 23
)
[11] => Array
(
[ID] => 12
[name] => 4
[email] => Mark
)
[12] => Array
(
[ID] => 13
[name] => Paul
[email] => 32
)
[13] => Array
(
[ID] => 14
[name] => Allen
[email] => 25
)
[14] => Array
(
[ID] => 15
[name] => Teddy
[email] => 23
)
[15] => Array
(
[ID] => 16
[name] => 4
[email] => Mark
)
[16] => Array
(
[ID] => 17
[name] => Paul
[email] => ibraq22#yahooo.com
)
[17] => Array
(
[ID] => 18
[name] => Opeyemi Adam
[email] => resfvr#yahoo.com
)
[18] => Array
(
[ID] => 19
[name] => Opeyemi Adam
[email] => info#ckdigital.net
)
[19] => Array
(
[ID] => 20
[name] => Paul
[email] => ibraq22#yahooo.com
)
)
)
My PHP Loop is below
foreach ($retr as $page) {
echo $page['name'];
//var_dump($page);
}
Regards
Your array is multidimentional, being a single element array containing a further 19 child elements.
If you just want to itterate the child elements, then run the loop over the top level elelemt, not the parent array:
foreach ($retr[0] as $page) {...}
I have an array of categories where id is the id of the category, parent denotes the parent id of the category (id 0 denotes the top most parent node) and value is the title of the array. The path is initially set to the id of the category. The array is as follows:
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[value] => Corporate Files
[path] => 1
)
[1] => Array
(
[id] => 2
[parent] => 0
[value] => Products Files
[path] => 2
)
[2] => Array
(
[id] => 3
[parent] => 1
[value] => Communications Materials
[path] => 3
)
[3] => Array
(
[id] => 4
[parent] => 1
[value] => Group Technical
[path] => 4
)
[4] => Array
(
[id] => 5
[parent] => 1
[value] => New Projects
[path] => 5
)
[5] => Array
(
[id] => 6
[parent] => 2
[value] => Product Range
[path] => 6
)
[6] => Array
(
[id] => 7
[parent] => 2
[value] => WL4
[path] => 7
)
);
I want to generate the paths of categories in the array. so the output should be
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[value] => Corporate Files
[path] => 1
)
[1] => Array
(
[id] => 2
[parent] => 0
[value] => Products Files
[path] => 2
)
[2] => Array
(
[id] => 3
[parent] => 1
[value] => Communications Materials
[path] => 1,3
)
[3] => Array
(
[id] => 4
[parent] => 1
[value] => Group Technical
[path] => 1,4
)
[4] => Array
(
[id] => 5
[parent] => 1
[value] => New Projects
[path] => 1,5
)
[5] => Array
(
[id] => 6
[parent] => 2
[value] => Product Range
[path] => 2,6
)
[6] => Array
(
[id] => 7
[parent] => 2
[value] => WL4
[path] => 2,7
)
);
I wrote the following function.
function findparent($id,$path){
global $categories;
global $catcnt;
if($id==0){
echo $path."<br />"; //this outputs path currently
return $path;
}
for($i=0;$i<$catcnt;$i++){
if($id==$categories[$i]['id']){
$path=$id.",".$path;
findparent($categories[$i]['parent'],$path);
}
}
}
for($i=0;$i<count($categories);$i++){
$categories[$i]['path']=(string)findparent($categories[$i]['parent'],$categories[$i]['id']); //this doesnt assign it currectly
}
and the output is:
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[value] => Corporate Files
[path] =>
)
[1] => Array
(
[id] => 2
[parent] => 0
[value] => Products Files
[path] =>
)
[2] => Array
(
[id] => 3
[parent] => 1
[value] => Communications Materials
[path] =>
)
[3] => Array
(
[id] => 4
[parent] => 1
[value] => Group Technical
[path] =>
)
[4] => Array
(
[id] => 5
[parent] => 1
[value] => New Projects
[path] =>
)
[5] => Array
(
[id] => 6
[parent] => 2
[value] => Product Range
[path] =>
)
[6] => Array
(
[id] => 7
[parent] => 2
[value] => WL4
[path] =>
)
);
Where am I going wrong?
findparent only returns if the id is zero.
You need a second return statement, before the recursive findparent call.
As you don't need to deal with multiple levels, a function of it's own is a bit of an overhead as you could work straight on the array itself with a simple foreach:
foreach ($array as &$node)
{
if ($node['parent'])
{
$node['path'] = $node['parent'] . ',' . $node['path'];
}
}
unset($node);
However, you can put this into a function of it's own as well, but you won't need any global variables as far as I can see.
What you see here is simple string concatenation, this makes the array as you wrote you wanted it to have. My first comment was more meant to manage the structure with an n-depth not a 1-depth. Demo
[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]);