$array1 = array(1=>"Band",2=>"Song", 3=>"Release",4=>"Year");
$array2 = array(1=>"Band");
print_r(array_intersect($array1,$array2));
Expected : array(1=>"Band") but i got an empty array();
#Barmar has already mentioned the cause in his comment. The differences cannot be shown with print_r. var_dump shows a different length for the first element.
$array1 = array(1=>"Band",2=>"Song", 3=>"Release",4=>"Year");
$array2 = array(1=>"Band");
var_dump($array1,$array2);
Output:
array(4) {
[1]=>
string(7) "Band"
[2]=>
string(4) "Song"
[3]=>
string(7) "Release"
[4]=>
string(4) "Year"
}
array(1) {
[1]=>
string(4) "Band"
}
Removing the BOM before the 'Band' solves the problem. The following line only removes a BOM only if it is at the beginning of the string.
$array1[key($array1)] = preg_replace("~^\xef\xbb\xbf~u","",$array1[key($array1)]);
Try it yourself: https://3v4l.org/NYnPG
I find it commendable that the editor of 3v4l.org also shows the difference.
However, a BOM should always be removed when importing (CSV?)data .
Related
I have a string, which I convert into an array using an explode, but when I consult it, it does not come out in the proper order and I have even tried to make it an asort.
$clasipun = explode(",","0.11111111111111,0.22222222222222,0.33333333333333");
echo $clasipun[1]; //Receive 0.33333333333333
asort($clasipun);
echo $clasipun[1]; //Receive 0.33333333333333
$stringclasi = implode ("," $clasipun);
echo $stringclasi; //"0.11111111111111,0.22222222222222,0.33333333333333"
I have tried with some tricks, but dont works any...
foreach ($clasipun as $inde=>$valor){
//at $inde=1 I receive a $valor=0.33333333333333
}
Any suggestions?
Ok, first of all, thanks for yours time.
Yesterday it must have been obfuscated, but the problem was in the "asort", which orders the content but keeps the keys. fix.
$clasipun = explode(",","0.11111111111111,0.33333333333333,0.22222222222222");
echo $clasipun[1]; //Receive 0.33333333333333
asort($clasipun);
echo $clasipun[1];//Receive 0.33333333333333
sort($clasipun);
echo $clasipun[1];//Receive 0.22222222222222 !fine
Take a look at this:
$clasipun = explode(",","0.11111111111111,0.33333333333333,0.22222222222222");
var_dump($clasipun);
array(3) {
[0]=>
string(16) "0.11111111111111"
[1]=>
string(16) "0.33333333333333"
[2]=>
string(16) "0.22222222222222"
}
asort($clasipun);
var_dump($clasipun);
array(3) {
[0]=>
string(16) "0.11111111111111"
[2]=> <-!!!!!!!!!!!
string(16) "0.22222222222222"
[1]=> <-!!!!!!!!!!
string(16) "0.33333333333333"
}
sort($clasipun);
var_dump($clasipun);
array(3) {
[0]=>
string(16) "0.11111111111111"
[1]=> <-!!!!!!!!!!!
string(16) "0.22222222222222"
[2]=> <-!!!!!!!!!!
string(16) "0.33333333333333"
}
The problem is that not to make the explode of a direct string, it was a variable that contained the string, and to make the implode, as ordered by the asort, the new string if it came out in the correct order. What a mistake.
Solved...
I am having a strange problem with defining and accessing array values. The strings I've created return empty. When I do a var_dump, I see that the array values have the correct string lengths, but result in "". When I attempt to access the variable, I receive "". Can someone explain this behavior?
//define the case combinations
$cases = array(
"<CO><VO><CO><VOP>",
"<VO><CLOSED><VO>",
"<OPEN><CO><VO>",
"<CO><VOP><ESCB>",
"<COP><VO><CO><CVN>",
"<STVE><ESCB>"
);
The var dump:
array(6) { [0]=> string(17) "" [1]=> string(16) "" [2]=> string(14) "" [3]=> string(15) "" [4]=> string(18) "" [5]=> string(12) "" }
A similar problem happens with other arrays:
$siteSuffix = array(
"data" => array('com','org','net','edu'),
"probabilities" => array(1,1,1,1), //not actually used
"name" => "siteSuffix"
);
The array under 'data' prints strings in the same manner.
If the problem is external of this, here is a github link where the rest of the code is: link
It looks like because you have htmlentities in your actual data (array) < and > PHP will not interpret these as actual values/strings.
What you can do is convert the characters using htmlspecialchars(), for example:
$cases = array(
"<CO><VO><CO><VOP>",
"<VO><CLOSED><VO>",
"<OPEN><CO><VO>",
"<CO><VOP><ESCB>",
"<COP><VO><CO><CVN>",
"<STVE><ESCB>"
);
foreach ($cases as $case){
var_dump(htmlspecialchars($case));
}
Output of var_dump:
string(41) "<CO><VO><CO><VOP>" string(34) "<VO><CLOSED><VO>" string(32) "<OPEN><CO><VO>" string(33) "<CO><VOP><ESCB>" string(42) "<COP><VO><CO><CVN>" string(24) "<STVE><ESCB>"
The problem is with the angle brackets < and > to print them in browser you can simply use echo htmlentities($str) or htmlspecialchars($str)for each array item.
If you are using php command line, then you don't need such encoding.
Refer: https://www.w3schools.com/php/func_string_htmlentities.asp
I simply want to compare two strings
$result = array_diff($original, $new);
var_dump $original:
array(4) {
[0]=>
string(4) "8344"
[1]=>
string(4) "7076"
[2]=>
string(7) "6220940"
[3]=>
string(7) "6220940"
}
var_dump $new:
array(4) {
[0]=>
string(4) "8344"
[1]=>
string(4) "7076"
[2]=>
string(14) "6220940mistake"
[3]=>
string(7) "6220940"
}
var_dump $result:
array(0) {
}
But what I actually expect would be var_dump $result:
array(1) {
[2]=>
string(7) "6220940"
}
I found out that this is happening because I have two strings that are similar. So if every string is unique, there is no problem. But I do sometimes have also similar strings inside my array. Can you help me with this problem?
<?php
$a = array("8344", "7076", "6220940", "6220940");
$b = array("8344", "7076", "6220940mistake", "6220940");
var_export(array_diff_assoc($a,$b));
prints
array (
2 => '6220940',
)
see array_diff_assoc
You have empty result because all of the elements in $orginal array are present in array you are comparing against ($new) - value "6220940" is present at index 3.
You should use array_diff_assoc instead of array_diff so you will be comparing array elements with their index assignment.
I'm having trouble converting string to int.
The array $input holds following values:
array(3) { [0]=> string(6) "30" [1]=> string(2) "01" [2]=> string(9) "2013" }
First I remove the leading zeros, because of the octa trap. So this is my code:
foreach ($input as $key => $var) {
$input[$key] = trim($var,"0");
$input[$key] = (int)$var;
}
But unfortunately the result is not statisfying.
array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(2013) }
30 is now zero? How is this possible?
[0]=> string(6) "30"
This string has some non-printable characters in the beginning. It looks like 2 characters long, but in reality there are 6 of them. Such characters would cause the string to be converted to 0 as is documented.
You should try bin2hex on the string to see what byte values we are talking about, then use this knowledge to determine where they come from.
The "2013" string also has extra characters, although by the looks of it they are trailing in that case.
I've just been running some simple debug tests against arrays, and noticed that when I do a var_dump() of an array, the output is flagging any element in the array that is referenced by another variable. As a simple experiment, I ran the following code:
$array = range(1,4);
var_dump($array);
echo '<br />';
foreach($array as &$value) {
}
var_dump($array);
echo '<br />';
$value2 = &$array[1];
var_dump($array);
echo '<br />';
which gives the following output:
array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) }
array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> ∫(4) }
array(4) { [0]=> int(1) [1]=> ∫(2) [2]=> int(3) [3]=> ∫(4) }
Note the ∫ symbol alongside element 3 and subsequently element 1. Note also that those entries don't show the entry's datatype.
Following some experimentation, I don't see this if I var_dump a scalar type, or with objects or resources. If the array contains string data, the symbol is a & (and it does still show the datatype), likewise with float, boolean and object entries.
This is running against PHP 5.2.8
First question: When did this behaviour start, or is it something that I simply haven't noticed before now?
Second question: If referenced elements can be flagged in this way by var_dump(), is there any function in core PHP that will allow me to identify if an array element is referenced by another variable; or that will return the refcount or ref flag from a _zval_struct?
Not sure if this answers your question, but you can use
debug_zval_dump($array);
to get the refcount:
array(4) refcount(2){
[0]=> long(1) refcount(1)
[1]=> &long(2) refcount(2)
[2]=> long(3) refcount(1)
[3]=> &long(4) refcount(2)
}
Also see this Article by Derick Rethans (PHP Core Dev) about Refcounting.