I'm trying to get the difference between two arrays, but with array_diff, array_diff_assoc, or array_diff_key I can't get what I want..
Array 1 :
0 => 424012,
1 => 423000,
2 => 425010,
3 => 431447,
4 => 421001,
5 => 421002,
Array 2 :
0 => 424012,
1 => 423000,
2 => 425010,
3 => 431447,
4 => 431447,
5 => 421001,
6 => 421002,
array_diff = array ()
// empty
jarray_diff_assoc = array (
4 => 431447,
5 => 421001,
6 => 421002,
)
// OK but too much :)
array_diff_key = array(
6 => 421002
)
// nope i don't want that :(
I want 431447, cause it's only one time in the first array and twice in the second.
Regards, Tony
Is that exactly what you want? Only those that occur one time in the first, and two times in the second?
You can basically write your own function for that. Search through the second array, get a list of values that occur two times (or more than once, depending on what it is that you actually want), and then search for those in the first one (this you can do using a built-in PHP function array_intersect).
Related
Hi I need to select a rand value in the array removed and short the array i came out whit this small code but it keeps in an infinite loop but this is the weird look
<?php
$array=array("1","2","3","4","5","6","7","8","9","0");
$count=count($array);
for ($il=1;$il<=$count;$il++){
$array_value=array_rand($array, 1);
$array_value_key = array_search($array_value, $array);
$array_key_last=array_key_last($array);
for($if=0;$if<=$array_key_last;$if++){
if ($if==$array_value_key){
for($ia=$array_value_key;$ia<=$array_key_last;$ia++){
if ($ia<$array_key_last){
$ian=$ia+1;
$array[$ia]=$array[$ian];
}else{
unset($array[$ia]);
}
}
}
}
print_r($array);
}
?>
there the output can be different each time likes this but never ends
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 5
[4] => 6
[5] => 7
[6] => 8
[7] => 9
[8] => 0
)
Array
(
[0] => 2
[1] => 3
[2] => 5
[3] => 6
[4] => 7
[5] => 8
[6] => 9
[7] => 0
)
^C
and i have to break it but as you can see in the first loop work as expected removes the number 4 and in the second loop removes the number 1 but don't finish the third loop
I have reviewed many times and get in to the conclusion of the problem it is on the line
$array[$ia]=$array[$ian];
if i add a echo here get printing a number it self to the infinity why?
$array[$ia]=$array[$ian];
echo $array[$ia],"\n";
The Problem is not in one line. It takes a few commands to interact:
array_rand returns the key of the element not the value
array_search returns (bool)false if it doesn't find the value (because of 1 this can happen)
You use $array_value_key to start a for loop. Because of 2 it can be (bool)false.
When you increment a boolean, it does not change. I.e. your $ia++ does nothing.
That's why $ia<=$array_key_last will never turn false and your loop runs forever.
So I'm learning Php, so as I was messing around with arrays to see how they work, I stumbled into this when I made two arrays.
$TestArray1 = array( 1 => 1, "string" => "string", 24, "other", 2 => 6, 8);
$TestArray2 = array( 6 => 1, "string" => "string", 24, "other", 1 => 6, 8);
But when I print them out with print_r() this is what I get (this also happens with var_dump by the way)
Array ( [1] => 1 [string] => string [2] => 6 [3] => other [4] => 8 )
Array ( [6] => 1 [string] => string [7] => 24 [8] => other [1] => 6 [9] => 8 )
As far as I can tell, by putting the two in the second array it overwrites the next possible spot with no key and then keeps going, shortening the array. So I thought that meant that if I use a 1 it would put it at the start but that does not happen either.
Is this normal or is there something wrong with my php installation?
Im using Ampps in windows 10 with php 7.3.
Thanks in advance
Good question.
What's happening is that when determining automatic numeric indexes, PHP will look to the largest numeric index added and increment it (or use 0 if there are none).
The key is optional. If it is not specified, PHP will use the increment of the largest previously used integer key.
What's happening with your first array is that as it is evaluated left-to-right, 24 is inserted at index 2 because the last numeric index was 1 => 1.
Then when it gets to 2 => 6, it overwrites the previous value at index 2. This is why 24 is missing from your first array.
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.
Here's a breakdown
$TestArray1 = [1 => 6]; // Array( [1] => 6 )
// no index, so use last numeric + 1
$TestArray1[] = 24; // Array( [1] => 6, [2] => 24 )
$TestArray1[2] = 6; // Array( [1] => 6, [2] => 6 )
When you manually add numeric indexes that are lower than previous ones (ie $TestArray2), they will be added as provided but their position will be later.
This is because PHP arrays are really maps that just pretend to be indexed arrays sometimes, depending on what's in them.
References are from the PHP manual page for Arrays
I am attempting to count the number of occurrences of every unique word on a page (think SEO 'word count' that you see on woorank etc. - but not for that purpose!)
I am really struggling on how to set this up:-
At the moment I am thinking of reading each word and then checking if it is unique against an array -> if unique add to array with occurences=>1 - then if I find the same word later just +1.
However this seems really cumbersome and slow for large blocks of text (especially as I will have to strip commas etc, convert all to lower case etc.) -> is there are a better way, has someone got a code snippet or library for this task?
For clarity
The Cat ran away with the hat. The spoon had already run away with another cat, far far away.
Would yield:
the => 3,
away => 3,
cat => 2,
with => 2,
far => 2,
spoon => 1,
hat => 1,
ran => 1,
run => 1,
had => 1,
another => 1,
already => 1
Thanks in advance - if there is no better way then that is fine!
ASIDE
I contemplated do a replace($word,"") on all words once found and counted -> but this seems just as cumbersome.
Use array_count_values() in conjunction with str_word_count():
$wordCounts = array_count_values(str_word_count(strtolower($sentence), 1));
arsort($wordCounts);
Output:
Array
(
[the] => 3
[away] => 3
[cat] => 2
[far] => 2
[with] => 2
[run] => 1
[another] => 1
[already] => 1
[hat] => 1
[ran] => 1
[spoon] => 1
[had] => 1
)
Demo
have a look at this article: http://net.tutsplus.com/tutorials/javascript-ajax/spotlight-jquery-replacetext/
Split all the words (you could use a tokenizer like the ones users in Solr to "clean" them), put then in array, sort it, and array unique count. It really would depend on the language, but it will always be faster to use the language native functions that iterate the text by yourself.
In php:
$array = preg_split('/[\s,\.]+/', strtolower($text));
$unique = array_count_values($array);
print_r($unique);
I'm running a loop basically that will make an array that contains a million numbers between 1 and 10, how do I iterate through it and count how many of each there are?
Like:
1 - 201491 times
2 - 23091 times
There's a native PHP function for that:
$count = array_count_values($array);
print_r($count);
will output:
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
Why this does not work?
$stringhaha =" 1 => General,
2 => Business,
3 => Entertainment,
4 => Health,
5 => Politics,
6 => Sci/Tech,
7 => Sports,
8 => News";
$all_categories = array($stringhaha);
print_r($all_categories);
(will give an array with 1 item.)
While this works:
If I include the variable content like this it will create properly an array with 8 items:
$all_categories = array(1 => General,
2 => Business,
3 => Entertainment,
4 => Health,
5 => Politics,
6 => Sci/Tech,
7 => Sports,
8 => News);
print_r($all_categories);
What is happening is exactly what you should expect: you've declared an array that contains one string.
It doesn't matter that your string looks like an array to us humans, PHP is merely PHP, and can't magically detect that you want it to parse an array from a string.
giorgio79, meet PHP Docs, your new best friend.
It's called language syntax. You cannot do whatever you want. You have to speak the language how it was designed.
This doesn't work either
message = hello
Why? Because it's not syntactically correct. Same applies for your example with array.
This is correct
$message = 'hello';
Every language has rules and you have to respect them. Good luck.
I think the correct syntax is:
$all_categories = array(1 => "General",
2 => "Business",
3 => "Entertainment",
4 => "Health",
5 => "Politics",
6 => "Sci/Tech",
7 => "Sports",
8 => "News");
print_r($all_categories);
You do want an array of strings, right?