I have a list of letters. I compare this list of letters against an array of the alphabet and get the difference. Then they're put together into one array so I can output it as one list.
//start the array
$alphabet = array();
//the letters I'm using
$letters = array('A','B','C','D','E','F','G','H','I','J','L','M','N','O','P','R','S','T','V');
//place into true array
foreach($letters as $l)
$alphabet['true'][] = $l;
//alphabet array, place into false array
foreach (range('A','Z') as $char)
$alphabet['false'][] = $char;
//the not used array by getting the difference of true and false arrays
$alphabet['actual'] = array_diff($alphabet['false'], $alphabet['true']);
//merge the arrays into one array
$new = array_merge($alphabet['true'],$alphabet['actual']);
//sort them naturally
natsort($new);
//list the results
echo "All together now: <pre>"; print_r($new); echo "</pre>";
Is there a way to style each of the different arrays key-values before placing them into the big array? Something along the lines of the not used letters are a different color? Or am I going about this the wrong way? Thank you for any insight.
If it where me I would do something like this.
//start the array
$alphabet = array();
//the letters I'm using
$used = array('A','B','C','D','E','F','G','H','I','J','L','M','N','O','P','R','S','T','V');
$alphabet = range('A','Z');
echo "<ul>";
foreach($alphabet as $letter){
if (in_array($letter, $used)){
echo "<li class='used'>".$letter."</li>";
} else {
echo "<li class='unused'>".$letter."</li>";
}
}
echo "</ul>";
and make a couple of css rules
li.used { color:green; }
li.unused { color:red; }
Related
I have a $test array which contains strings. I also have two functions, replaceOverlap that merges two overlapped strings and findNested that finds if one of the two strings is a substring from the other. The code is in PHP
What I would like to do is to iterate over the $test array and take each element ($epitope) and compare it with the rest of the $epitopes in the array one at a time, as there are some more rules in the algorithm. The issue is that if I only want to print the biggest overlapped-nested string found across the rest of the $test array, and then delete from the array those string that have been merged. This is the code I have so far. Any help will be very appreciated.
$test=array(AVNIVGYSNAQGVDY,DIKYTWNVPKI,DIKYTWNVPKIA,DIKYTWNVPKIAPKSEN,GCHGSEPCIIHRGK,IDGLEVDVPGIDPNAC,IGIKDLRAFQHYDGRTI,IIHRGKPFQLEAV,IKYTWNVPKIAPKSEN,KPFQLEAVFEANQNT,LRQMRTVTPIRMQGG,NFLESLKYVEANKGAIN,PCIIHRGKPFQLEAV,PLVKGQQYDIKYTWNVP,QQYDIKYTWNVPKI,QQYDIKYTWNVPKIA,QQYDIKYTWNVPKIAP,QQYDIKYTWNVPKIAPK,QQYDIKYTWNVPKIAPKS,QQYDIKYTWNVPKIAPKSE,QQYDIKYTWNVPKIAPKSEN,QYDIKYTWNVPK,QYDIKYTWNVPKI,QYDIKYTWNVPKIAPKS,QYDIKYTWNVPKIAPKSEN,RFGISNYCQIYPPNV,SAYLAHRNQSLDLAEQELVDCAS,TAIAVIIGIKDLRAFQH,TWNVPKIAPKSENVVVT,YAYVAREQSCR,YDIKYTWNVPK,YDIKYTWNVPKI,YDIKYTWNVPKIA,YDIKYTWNVPKIAPKSEN);
$i=0;
foreach ($test as $epitope){
$str1=$epitope;
for ($j = $i; $j < count($test); ++$j) {
$str2=$test[$j];
$value1 = replaceOverlap($str1,$str2);
$value2 = replaceOverlap($str2,$str1);
$value3 = findNested($str1,$str2);
if (replaceOverlap($str1,$str2)!=false){
array_splice($test[$j]);
print $value1;echo"</br>";
}
elseif (replaceOverlap($str2,$str1)!=false){
array_splice($test[$j]);
print $value2;echo"</br>";
}
elseif (findNested($str1,$str2)!=false){
array_splice($test[$j]);
print $value3;echo"</br>";
}
}
$i=$i+1;
}
I have an array that looks like this:
$elm = 'a,b,c';
I need the values of the array so I use explode to get to them:
$q = explode(",",$elm);
I then would like to echo every single item into a span, so I make an array:
$arr = array();
foreach($html->find($q[0]) as $a) {
$arr[] = $a->outertext;
}
$arr2 = array();
foreach($html->find($q[1]) as $b) {
$arr2[] = $b->outertext;
}
$arr3 = array();
foreach($html->find($q[2]) as $c) {
$arr3[] = $c->outertext;
}
And then finally I output like this:
echo "<ul>";
for($i=0; $i<sizeof($arr + $arr2 + $arr3); $i++)
{
echo "<li>";
echo "<span>".$arr[$i]."</span>";
echo "<span>".$arr2[$i]."</span>";
echo "<span>".$arr3[$i]."</span>";
echo "</li>";
}
echo "</ul>";
The problem is that I have to write all the items ($q[0] + $q[1] + $q[2]) and the corresponding span (<span>".$arr[$i]."</span>) This is a problem because in reality I don't know what and how long the first array ($elm) is. Therefore I don't want to 'physically' write down all the span elements but rather create them on the fly depending on the array $elm. I tried many things but I can't figure it out.
The basic issue here is that you don't know how many elements $elm will contain. foreach is the best choice here, as it doesn't require the length of the array to loop through it.
Use a nested foreach loop to store all the outertexts in an array:
foreach (explode(",", $elm) as $elem) {
foreach ($html->find($elem) as $a) {
$arr[$elem][] = $a->outertext;
}
}
$arr[$elem][] is the important bit here. On each iteration of the outer loop, the value of $elem will be a, b and c. On each iteration of the inner loop, it will create a new index in the array: $arr['a'], $arr['b'] and $arr['c'] and add the outertext values to the respective index.
Once you've stored all the required values in the array, it's only a matter of looping through it. Since we have a multi-dimensional array here, you will need to use a nested loop again:
echo "<ul>";
foreach ($arr as $sub) {
echo "<li>";
foreach ($sub as $span) {
echo "<span>".$span."</span>";
}
echo "</li>";
}
echo "</ul>";
$find=array('or','and','not');
$text=array('jasvjasvor','asmasnand','tekjbdkcjbdsnot');
I have to check if text array contains any of the elements find has. I'm able to do this for single text but don't know how to do it for all texts
$counter=0;
foreach($find as $txt){
if (strstr($text[0], $txt)) {
$counter++;
}
If i use this technique i'll have to run foreach number of times. Is there any other way to do this?
NOTE if array value contains or,and ,not not the whole word match
http://codepad.viper-7.com/VKBMtP
Input
$find=array('or','and','not');
$text=array('jasvjasvor','asmasn','tekjbdkcjbdsnot');
// array values "jasvjasvor" and "tekjbdkcjbdsnot" contains words `or,not`
Output
2 -> as two words from find array are contained in text array values
Use array_intersect():
if (count(array_intersect($find, $text)) >= 1) {
// both arrays have at least one common element
}
Demo.
UPDATE: If you're trying to find how many elements in $text array contain any of the values (partial match or whole-word match) in $find array, you can use the following solution:
$counter = 0;
foreach($find as $needle) {
foreach ($text as $haystack) {
if(strpos($haystack, $needle) !== false) $counter++;
}
}
echo $counter; // => 2
Demo.
$counter=0;
foreach($find as $txt){
foreach($txt as $value){
if (strstr($value, $txt)) {
$counter++;
}
}
Suppose that I start with an array that looks like:
$array_1 = array(array(1,2,3), array(2,4,5), array(3,6,7));
For simplicity, assume that I have a rule that says: delete the first subarray and then delete the first elements of the remaining subarrays. This would yield the result:
$new_array = array(array(4,5), array(6,7))
Then assume I expand the problem to larger arrays like:
$array_2 = array(array(1,2,3,4), array(2,3,4,5), array(3,4,5,6), array(4,5,6,7));
I have the same rule here - delete first subarray and then delete first elements of the remaining subarrays. BUT this rule must be continued until the smallest subarray contains only two elements (as in the first example). So that in stage one of the process, my new array would look like:
$new_array_s1 = array(array(3,4,5), array(4,5,6), array(5,6,7));
But in the final stage, the completed array would look like:
$new_array_s2 = array(array(5,6), array(6,7));
For context, here is my code for the $array_1 example:
<?php
$array_1 = array(array(1,2,3), array(2,4,5), array(3,6,7));
$array_shell = $array_1;
unset($array_shell[0]);
$array_size = count($array_shell);
$i = 0;
$cofactor = array();
while($i < $array_size) {
$el_part_[$i] = $array_1[$i];
unset($el_part_[$i][0]);
$el_part_[$i] = array_values($el_part_[$i]);
array_push($cofactor, $el_part_[$i]);
++$i;
}
echo '<pre>',print_r($cofactor,1),'</pre>';
?>
My Question: How can I generalise this code to work for N sized arrays?
You don't need a complicated code .. Just loop and use array_shift
Example:
print_r(cleanUp($array_1));
Function
function cleanUp($array) {
array_shift($array);
foreach($array as $k => $var) {
is_array($var) && array_shift($array[$k]);
}
return $array;
}
See Live DEMO
$num = count($array_1);
for($i=0;$i<=$num;$i++)
{
if($i==0)
unset($array_1[$i]);
else unset($array_1[$i][0]);
}
Building off of Baba's answer, to work with N element arrays (assuming each array contains the same number of elements):
<?php
$array_1 = array(array(1,2,3,4), array(2,4,5,6), array(3,6,7,8));
$array = $array_1;
while(count($array[0]) > 2)
$array = cleanUp($array);
print_r($array);
function cleanUp($array) {
array_shift($array);
foreach($array as $k => $var) {
is_array($var) && array_shift($array[$k]);
}
return $array;
}
This will keep reducing until the sub-arrays have only 2 elements.
-Ken
I have a problem with sorting of an array.
$infoGroup is the result of a 'ldap_get_entries' call earlier. As I step through this array I put the result in the array $names.
Then I want to sort $names in alfabetical order, I have tried a number of different methods but to no avail. The array always stays in the same order it was constructed.
What have I missed?
foreach($infoGroup[$i]['member'] as $member) {
//echo "<li>".$member;
$go = stripos($member, "n");
unset($names);
$ai++;
if ( $go == 1 ) {
// extract member name from string
$temp = substr($member, 0, stripos($member, ","));
// Strip the CN= and change to lowercase for easy handling
$temp = str_replace("cn=", "", $temp);
$names[$ai] = ($temp);
}
if (natsort($names)){
foreach ($names as $key => $val) {
echo "<li>";
echo "$key $val";
}
}
}
$ai = 0;
This is the result however I try to sort the $names array:
Henrik Lindbom
Klaus Rödel
Admin
Bernd Brandstetter
proxyuser
Patrik Löfström
Andreas Galic
Martin Stalder
Hmmm.. a bit hard to explain, but the issue is because you are sorting your array inside that foreach() loop. Essentially, since you are creating the array element in the iteration of the first loop, the natsort() only has 1 element to sort and your nested foreach() loop is only outputting that 1 element, which is then unset() at the second and further iterations...
Extract that second foreach() that sorts and outputs and remove the unset() from the top of the first loop. This should output your desired results.
Something like this...
foreach($infoGroup[$i]['member'] as $member) {
//echo "<li>".$member;
$go = stripos($member, "n");
$ai++;
if ( $go == 1 ) {
// extract member name from string
$temp = substr($member, 0, stripos($member, ","));
// Strip the CN= and change to lowercase for easy handling
$temp = str_replace("cn=", "", $temp);
$names[$ai] = ($temp);
}
}
if (natsort($names)){
foreach ($names as $key => $val) {
echo "<li>";
echo "$key $val";
}
}
$ai = 0;