kucing Posted February 18, 2007 Share Posted February 18, 2007 Dear friends and fellows, I am stuck with this array combine stuff which I have tried all my skils but still no hope so I would appreciate any help if someone could Ok here is what I am facing. $array=array ( 'UserIds' => array ( 0 => '32', 1 => '33', 2 => '34', ), 'UserAge' => array ( 0 => '14', 1 => '27', 2 => '32', ), 'Income' => array ( 0 => '1100', 1 => '900', 2 => '1350', ), 'Status' => array ( 0 => '01', 1 => '00', 2 => '00', ), ); and when I do print_r($array); It show me result like this Array ( [userIds] => Array ( [0] => 32 [1] => 33 [2] => 34 ) [userAge] => Array ( [0] => 14 [1] => 27 [2] => 32 ) [income] => Array ( [0] => 1100 [1] => 900 [2] => 1350 ) [status] => Array ( [0] => 01 [1] => 00 [2] => 00 ) ) Also about the username, userage, income and status could be random as users also allowed to make new things which stores in one array. And my problem start right here After wasting my 2 days I have no choice but to ask help I want result to be like this Array ( [0] => 32 [1] => 33 [2] => 34 [3] => 14 [4] => 27 [5] => 32 [6] => 1100 [7] => 900 [8] => 1350 [9] => 01 [10] => 00 [11] => 00 ) Here i do not want to display any username, userage, income or Status but want all the subarrays in one array.. Thank you for you help I really appreciate it K Quote Link to comment Share on other sites More sharing options...
trq Posted February 18, 2007 Share Posted February 18, 2007 You can't do that. All indexes in a numeric array must be unique. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 18, 2007 Share Posted February 18, 2007 You can't have an array like that since you have non-unique index values. Why do you want to arrange the array like that? Ken Quote Link to comment Share on other sites More sharing options...
kucing Posted February 18, 2007 Author Share Posted February 18, 2007 sorry:( my bad i wrote that by mistake actually i want it like this Array ( [0] => 32 [1] => 33 [2] => 34 [3] => 14 [4] => 27 [5] => 32 [6] => 1100 [7] => 900 [8] => 1350 [9] => 01 [10] => 00 [11] => 00 ) Also about the username, userage, income and status could be random as users also allowed to make new things which stores in one array. I really appreciate your kind help Thanks Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 18, 2007 Share Posted February 18, 2007 If you combine the arrays like this, how are you going to be able to use the values? You won't have any clue as to what value is associated with which instance. Ken Quote Link to comment Share on other sites More sharing options...
kucing Posted February 18, 2007 Author Share Posted February 18, 2007 Actually that was just an example to make this post easy to understand and in real every user has its own array which is using the same way of the array shown above. Why I want it to be like that because I want to use array_slice Example: $arraybreak = array_slice($array, 0, 1); And when I do that in this $array It give me result like this which I don't want Array ( [userIds] => Array ( [0] => 32 ) [userAge] => Array ( [0] => 14 ) [income] => Array ( [0] => 1100 ) [status] => Array ( [0] => 01 ) ) But I only want it to show me Array ( [userIds] => Array ( [0] => 32 ) ) I am totally confuse.. Thanks Quote Link to comment Share on other sites More sharing options...
kucing Posted February 18, 2007 Author Share Posted February 18, 2007 Any clue? please Quote Link to comment Share on other sites More sharing options...
tllewellyn Posted February 18, 2007 Share Posted February 18, 2007 I'm not entirely sure I grasp what you're trying to do, but if I'm reading you correctly you might try using foreach to go through the array, specifing which keys you want to extract your data from: foreach($array as $key=>$value) { if($key == 'UserIds') { foreach($array[$key] as $userid) { //do what you are going to do with the var $userid } } } Good luck! Trev Quote Link to comment Share on other sites More sharing options...
kucing Posted February 18, 2007 Author Share Posted February 18, 2007 I'm not entirely sure I grasp what you're trying to do, but if I'm reading you correctly you might try using foreach to go through the array, specifing which keys you want to extract your data from: foreach($array as $key=>$value) { if($key == 'UserIds') { foreach($array[$key] as $userid) { //do what you are going to do with the var $userid } } } Good luck! Trev I appreciate your help thanks but as I was being doing that before this problem started only after I added a feature for my users to make their own questions and when it comes to user define array keys my foreach doesnt work there since they making their own questions and answers and I got no clue what the question would be. An example: Array ( [abcdxyz] => Array ( [0] => xyz ) ) so that loop was good if i had default values.. Array ( [userID] => Array ( [0] => 200 ) ) So I was looking for a way to make those subarrays combine with all the arrays if there is a way I would really love to know Thanks in advance Quote Link to comment Share on other sites More sharing options...
kucing Posted February 18, 2007 Author Share Posted February 18, 2007 tllewellyn thank you very much and also those who tried their level best to help me:) My problem is solved Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 18, 2007 Share Posted February 18, 2007 What was your solution? Ken Quote Link to comment Share on other sites More sharing options...
kucing Posted February 18, 2007 Author Share Posted February 18, 2007 I wanted to combine all the subarrays in one array and I solved it using tllewellyn given codes and added $myTestArray which now contain all the subarrays data foreach($array as $key=>$value) { foreach($array[$key] as $userid) { //do what you are going to do with the var $userid $myTestArray["test"][]=$userid; } } Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.