Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/269122-array-issue/
Share on other sites

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'
);

Link to comment
https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382902
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382916
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382925
Share on other sites

@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.

Link to comment
https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382929
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382945
Share on other sites

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 by Christian F.
Link to comment
https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1382954
Share on other sites

@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.

Link to comment
https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383122
Share on other sites

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 by Christian F.
Link to comment
https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383123
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383133
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383135
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383137
Share on other sites

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...

Link to comment
https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383138
Share on other sites

@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

Link to comment
https://forums.phpfreaks.com/topic/269122-array-issue/#findComment-1383142
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.