Dragosvr92 Posted October 5, 2012 Share Posted October 5, 2012 I have this array which has double contents. $YearArr = array( 1 => '19', 2 => '19', 3 => '18', 4 => '18', 5 => '20', 6 => '20' ); Would it be possible to remove the repeating data, and make it look like something like this? $YearArr = array( 1/2 => '19', 3/4 => '18', 5/6 => '20' ); Thanks Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/ Share on other sites More sharing options...
ManiacDan Posted October 5, 2012 Share Posted October 5, 2012 Would you honestly like the keys to be a string list of all the old keys which pointed to the data? Or do you just want to remove the duplicates? Which of these do you want? $YearArr = array( '1/2' => '19', '3/4' => '18', '5/6' => '20' ); $YearArr = array( 0 => '19', 1 => '18', 2 => '20' ); Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382902 Share on other sites More sharing options...
Dragosvr92 Posted October 5, 2012 Author Share Posted October 5, 2012 (edited) I just want to remove the duplicates. Number 1 or 2 means/echoes 19. Same for the others. Thanks for the fast answer ! Edited October 5, 2012 by TheKiller Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382904 Share on other sites More sharing options...
ManiacDan Posted October 5, 2012 Share Posted October 5, 2012 Please answer the question. I don't know what "number 1 or 2 means/echoes 19" means. Your original code is probably the fastest way even though there are "duplicates." Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382912 Share on other sites More sharing options...
Jessica Posted October 5, 2012 Share Posted October 5, 2012 Your original code is probably the fastest way even though there are "duplicates." Agreed. Unless you want: $YearArr = array( 19=>array(1,2), 18=>array(3,4) ); etc. Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382914 Share on other sites More sharing options...
Dragosvr92 Posted October 5, 2012 Author Share Posted October 5, 2012 Sorry but i havent understood your question right. $YearArr = array( 1 => '19', 2 => '19', 3 => '18', 4 => '18', 5 => '20', 6 => '20' ); I have the array above. The keys 1 and 2 must output 19, if selected. I want to remove the duplicates. I think it is easy to understand. Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382916 Share on other sites More sharing options...
Dragosvr92 Posted October 5, 2012 Author Share Posted October 5, 2012 Agreed. Unless you want: $YearArr = array( 19=>array(1,2), 18=>array(3,4) ); etc. That wont be possible as 1, 2 keys generate 19. Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382917 Share on other sites More sharing options...
Jessica Posted October 5, 2012 Share Posted October 5, 2012 I have the array above. The keys 1 and 2 must output 19, if selected. I want to remove the duplicates. I think it is easy to understand. If the keys 1 and 2 must output 19, you CANNOT remove duplicates. Your own specs require duplicates. Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382919 Share on other sites More sharing options...
Dragosvr92 Posted October 5, 2012 Author Share Posted October 5, 2012 If the keys 1 and 2 must output 19, you CANNOT remove duplicates. Your own specs require duplicates. Well, i was hoping that it may be possible to do something like this: $YearArr = array( 1/2 => '19', 3/4 => '18', 5/6 => '20' ); :-\ Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382921 Share on other sites More sharing options...
Jessica Posted October 5, 2012 Share Posted October 5, 2012 Why? How would you then call the keys? It makes no sense, and it has no benefit. Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382924 Share on other sites More sharing options...
ManiacDan Posted October 5, 2012 Share Posted October 5, 2012 What problem are you trying to solve here? Why is this so important to you? 1/2 is math. It means 0.5. The way you write your code, $YearArr[.5] is the only thing which will output 19. Keep your code the way it is. It's the fastest implementation of whatever it is you're talking about, unless you tell us what the actual problem is. Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382925 Share on other sites More sharing options...
Dragosvr92 Posted October 5, 2012 Author Share Posted October 5, 2012 @Jessica, My benefit is that i wont have keys that output duplicate contents. @Dan, I have said what i am trying to solve, Not important, i'd just like it not output duplicate contents.................. I havent wrote it as math. I could have used a comma or and "OR" as example, to explain that 1 and 2 are to output the same crap. I explained what the actual problem is. I guess i'll keep the code as it is. Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382929 Share on other sites More sharing options...
ManiacDan Posted October 5, 2012 Share Posted October 5, 2012 You haven't explained the problem, you've just said you want to do something. There's no WHY. I could ask you how to fill my shower stall with licorice, that doesn't answer "why are you trying to do that?" There is no reason to do what you're trying to do, that's what we're saying. Whether or not it's possible is irrelevant, because all the ways to do it slow down the normal use for arrays. If you'd really like to do it and can't say why, here: $yearArr = array( 1 => '19', 2 => '19', 3 => '18', 4 => '18', 5 => '20', 6 => '20' ); $temp = array(); foreach ( $yearArr AS $k => $v ) { if (($a = array_search($v, $temp)) !== false) { $temp[$a.'/'.$k] = $v; unset($temp[$a]); } else { $temp[$k] = $v; } } $yearArr = $temp; print_r($yearArr); Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382945 Share on other sites More sharing options...
Barand Posted October 5, 2012 Share Posted October 5, 2012 Now you have your array viz. Array ( [1/2] => 19 [3/4] => 18 [5/6] => 20 ) we'll be very interested to know how you intend to use it. Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382953 Share on other sites More sharing options...
Christian F. Posted October 5, 2012 Share Posted October 5, 2012 (edited) As per your requirement: Index 1 and 2 should both contain the value 19, but no value should be duplicated? Or, to put it in another way: You want to be able to enter your house from the front and the back, but you only want one entrance (doors or windows) into your house. You see the paradox? You still haven't explained why you think it's a benefit of not "have[ing] keys that output duplicate contents." Also, that statement isn't a reason why as much as another way of saying what you want. If this is all simply just to save some memory/space, then you're so far off into the wrong field. A field called "premature micro-optimization", which only causes lots of bugs, overly complicated software, inefficiencies and other problems. The correct way is to make your code work, while making sure it's as simple and readable as possible. Once that's done, and you're certain that you've covered all of the logic, then you can profile it. That's the only proper way to find out what you need to do to optimize the code. Edited October 5, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382954 Share on other sites More sharing options...
Dragosvr92 Posted October 6, 2012 Author Share Posted October 6, 2012 @ManicDan: Are you really serious !? I have said WHY ! The thing has duplicate contents, and i wanted to set the keys one and two, to answer the same content, and not specify the content for each key, by repeating it. What dont you understand !? I have said why. Theres no secret. The question wasnt really, "why" but how to make that possible, if it is possible. Now you have your array viz. Array ( [1/2] => 19 [3/4] => 18 [5/6] => 20 ) we'll be very interested to know how you intend to use it. Is that piece of code working....? It isnt here. What are you trying to say :| @Christian F: Your paradox example has nothing to do with scripting. Would you put the problem the same way if you use a short string to output the samecontents tens of times in a file? What real benefit could i have from this? I would rather set keys 1 and 2 to generate the same thing. End. Its that simple. It isnt to save memory space. I was just asking if it is possible to specify more than one array key to display the same contents. $YearArr = array( 1,2 => '19', 3,4 => '18', 5,6 => '20' ); If array key 1 and 2, echo 19....... It doesnt sound like something impossible to do. The way i currently do it looks messy to me. I only use this piece of script in an offline tool i use, and it does the job well, but i want it to be better. Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383122 Share on other sites More sharing options...
Christian F. Posted October 6, 2012 Share Posted October 6, 2012 (edited) Displaying the same contents != removing duplicates, which I believe is the crux of the misunderstandings here. This conversation is a great example why it is important to be accurate, and descriptive, when trying to communicate with people over technical stuff. Anyway, I think I've gotten the gist of what you hope to accomplish with your question, and (more importantly) how the system you're going to use it in looks like. If there's one thing that explains to us the "why's" of you wanting to do something, it's the context of the surrounding system. Since you gave us no context, we've had to guess or ask. We did the latter, without getting any more details than "I want to do this apparent paradox" written in a number of different ways. How, if I'm correct, and that your system replicates a half-year plan of some sorts. Plus that you have some source data you want to group by the year, instead of the ID of the selected term, then you've attacked the problem from the wrong end. What you should have done, is something similar to this: $yearArr = array (1=> 19, 18, 20); $yearIndex = (int) ($termID / 2); echo $yearArr[$yearIndex]; There's another lesson to be learned here: The solution you've thought about doesn't necessarily need to be the right one, especially not if it begs the question "there's got to be a simpler way". Whenever you find yourself in that situation, don't ask how you can make your solution work the way you want it. Rather, show the prerequisites, and ask for a better solution. Edited October 6, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383123 Share on other sites More sharing options...
Pikachu2000 Posted October 6, 2012 Share Posted October 6, 2012 No. You can't do what you're trying to do, and there is no real reason to even bother trying. That's the most accurate answer I can give you based on the information you've given. Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383124 Share on other sites More sharing options...
Dragosvr92 Posted October 6, 2012 Author Share Posted October 6, 2012 Thanks for your fast answer. I consider i was explaining quite accurately. I answered to what you asked. Which was, why am i trying to do this. You havent asked how my system works like. I am using this for a social security number convertor. Here, our SSN's have contents about the person assigned to the SSN, and its just not a random number. Numbers 1 and 2 stand for persons that were born between 1990 and 1999. 1 stands for male, and two for female. Would have been interesting if the array system would let you assign a single answer to multiple keys. $YearArr = array( 1,2 => '19', 3,4 => '18', 5,6 => '20' ); This is my entire page code: http://www.rising-dead.com/code.txt Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383133 Share on other sites More sharing options...
Christian F. Posted October 6, 2012 Share Posted October 6, 2012 I refer you to my code example, it should do exactly what you want to do. For the gender, you can use modulus to achieve the same effect. However, do keep in mind that it obscures the logic of your script, and will make it harder to maintain. So I do not recommend doing it. PS: You really should be using UTF-8, instead of relying upon ISO HTML entities. Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383135 Share on other sites More sharing options...
ManiacDan Posted October 6, 2012 Share Posted October 6, 2012 yes, you have to explain why. by explaining your actual problem (the why) rather than whatever crazy tactic you've decided is the answer (the how), we can actually solve your problem. You don't seem to have a problem. Especially since I gave you the answer. Go look at the fully functioning example I gave you. That provides the exact format you requested. Now you have two questions: 1) How are you going to use that format? 2) Why did you want it in the first place? Unless the values of your arrays are megabytes in size, there is absolutely no reason to do what you're doing. It doesn't matter if it's social security numbers or license plates or street fighter characters. Keep the array as an array. Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383137 Share on other sites More sharing options...
Philip Posted October 6, 2012 Share Posted October 6, 2012 Would have been interesting if the array system would let you assign a single answer to multiple keys. $YearArr = array( 1,2 => '19', 3,4 => '18', 5,6 => '20' ); $foo = array(); $foo[1] = $foo[2] = '19'; I could ask you how to fill my shower stall with licorice, that doesn't answer "why are you trying to do that?" Why couldn't it be something tastier... Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383138 Share on other sites More sharing options...
ManiacDan Posted October 6, 2012 Share Posted October 6, 2012 (edited) Because if it was tasty, there's an obvious answer. If I wanted 60 ft^3 of skittles, it's obviously because I'm going to eat them all. nomnomnomnomnom Edited October 6, 2012 by ManiacDan Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383140 Share on other sites More sharing options...
Dragosvr92 Posted October 6, 2012 Author Share Posted October 6, 2012 @ChristianF: I havent understood how to use your example. Whats modulus... My original code doesnt have html entities. It has normal diacritic letters, and they are parsed well using the utf 8 html carset and the file encoding carset to utf 8. When i pasted the code in the hosted file, it replaced the diacritics with the html entities... @ManicDan: Yeah, i dont really have a serious problem. I will look around your code.... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383142 Share on other sites More sharing options...
Jessica Posted October 6, 2012 Share Posted October 6, 2012 I don't ... Even ... Quote Link to comment https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383145 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.