Ok, assume that I have 2 arrays.
$myArray = ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10');
$badNumbers = ('3', '6', '10')
What I want to do is compare $myArrays with $badNumbers, then modify $myArrays to remove anything found in the $badNumbers.
So after some code, the end result would be:
$myArray = ('1', '2', '4', '5', 7', '8', '9');
$badNumbers = ('3', '6', '10')
Is there anyway to do this? I've of some things, but nothing seems to work. The comparing part alone I already have some problems.
EDIT: I'm fine with a third array too. Something along the line of for each value, if it doesn't appear in the second array, array_push to the new array. But I'm still not so sure on how to do this.
You can use array_diff to get the result.
$myArray= array('1','2','3','4','5','6','7','8','9','0');
$badNumbers= array('3','6','0');
$available = array_diff($myArray, $badNumbers);
print_r($available);
echo '<br /><br />' . implode(', ', $available);
Hope this helps.
$result = array_diff($myArray, $badNumbers);
echo count($result) ? 'there were differences' : 'there werent';
I suppose, array_diff() function does what you want.
What does "count" do? Is "echo" like "print"?
echo count($result) ? 'there were differences' : 'there werent';
Related
how can i get something randomly from a array for example i have this array I want to grab one of those lines randomly how can I do that using PHP?
$quizes = array(
'3-1' => '2',
'4+4' => '8',
'7-5' => '2',
'4+2' => '6',
'9-3' => '6',
'1+2' => '3',
'9+9' => '18',
'3+2' => '5',
'2*3' => '6',
'5*3' => '15',
'6+6' => '12',
'3+4' => '7',
'7-4' => '3',
'6+2' => '8',
'3*2' => '6',
'7+6' => '13',
'1+1' => '2',
'4*4' => '16',
'10-3' => '7'
);
What i have tried
$rand_keys = array_rand($quizes, 2);
echo $quizes[$rand_keys[0]] . "\n";
echo $quizes[$rand_keys[1]] . "\n";
but this only returns results such as 2 7, 15 2, 3 2 and more
Please help thank you
You can randomize the array order and take the first element. The code would look like this:
shuffle($quizes);
Well, you are getting exactly what you are asking for - value that belongs to randomly chosen key.
To get key => value pair execute:
echo $rand_keys[0] . " => " . $quizes[$rand_keys[0]] . "\n";
Of course you can format output however you'd like to.
Each of the "rows" in your code is actually two parts: a key and its corresponding value. For example, in the first row '3-1' is the key and '2' is its value. The => operator indicates this relationship.
The array_rand function you used will return a random key (or set of keys, if you specify the second parameter) from your array.
$key = array_rand($quizes); // returns a random key e.g. '3-1'
Then you can use that key to get the corresponding value,
$value = $quizes[$key]; // returns the value that matches $key
There are various ways to output them from that point. If you want it to look kind of like it does in the code, you can use
echo "$key => $value";
Is it possible to customize the order_by query in MySQL or in CI ? Such as I want my column to be ordered by ('1', '11', '4', '2', '21', '3', '5', '7') So if I query it as ASC the result will show in the order of my customized order.
If it is not possible, what is the best workaround to get these order ? Hoping for a simple solution just using the MySQL query.
All answers and suggestions are greatly welcomed. Thanks.
Try this one.
$this -> db -> order_by('FIELD ( table.id, 1, 11, 4,2,21,3,5,7 )');
link
Pure Mysql answer is yes you can order a field by a set list with the MYSQL FIELD() function
SELECT *
FROM mytable
WHERE id IN ('1', '11', '4', '2', '21', '3', '5', '7')
ORDER BY FIELD(id, '1', '11', '4', '2', '21', '3', '5', '7')
Error
INSERT INTO PENILAIAN (ID,KURSUSID,QUEST1,QUEST2,QUEST3,QUEST4,QUEST5,QUEST6,QUEST7,QUEST8,QUEST9,QUEST10)VALUES ('951019105851', ''13'', '1', '2', '3', '4', '4', '5', '4', '5', '4', '5')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '13'', '1', '2', '3', '4', '4', '5', '4', '5', '4', '5')' at line 3
code
"INSERT INTO PENILAIAN (ID,KURSUSID,QUEST1,QUEST2,QUEST3,QUEST4,QUEST5,QUEST6,QUEST7,QUEST8,QUEST9,QUEST10)VALUES
('$login_sessionID',
'$kid','$QUEST1','$QUEST2','$QUEST3','$QUEST4','$QUEST5','$QUEST6','$QUEST7','$QUEST8','$QUEST9','$QUEST10')";
Error simply guides you
''13''
^ ^
there is additional single quote
keep only one ' pair for code
Use prepared statements
I think you have double quotes on ''13''.
olso i want to ask, does any of the values contain quotes? like this (I'am x)
if you use quotes you should use mysql_real_escape_string when you POST
Using php, I am looking to find a set of unique combinations of a specified length while making sure that no two identical values are present in more than one combination. For example, if I want to find all unique combinations of 3 values (fallback to combinations of 2 values if 3 are not possible) with this array:
$array = array(
array('1', '2'),
array('3', '4'),
array('5', '6'),
);
One possible set of combinations is 123, 456, 14, 15, 16, 24, 25, 26, 34, 35, 36
Note that each number is always combined once and only once with a different number. No duplicate number pairs show up in any combination. Just for clarity's sake, even though 123 and 135 would be unique combinations, only one of these would be returned since the pair 13 occurs in both. The main criteria is that all numbers are eventually grouped with each other number, but only once.
In the final product, the number of arrays and number of values will be notably larger as in:
$array = array(
array('1', '2', '3', '4', '5', '6', '7', '8'),
array('9', '10', '11', '12', '13', '14', '15', '16'),
array('17', '18', '19', '20', '21', '22', '23', '24'),
array('25', '26', '27', '28', '29', '30', '31')
);
Any help/code to accomplish this would be most appreciated.
UPDATE:
I've taken the brute force approach. First off, I'm using the pear package Math_Combinatorics to create the combinations, starting with a specified maximum size grouping and working my way down to pairs. This way I can get all possible combinations when iterating through to strip out any duplicate clusters within the groups. This code works but is extremely memory intensive. Generating all combinations for an array of 32 values in groups of 6 uses an excess of 1.5G of memory. Is there a better algorithm or approach that will let me use bigger arrays without running out of memory? Here the current state of the code:
require_once 'Combinatorics.php';
$combinatorics = new Math_Combinatorics;
$array = range(1,20,1);
$maxgroup = (6);
$combinations = $combinatorics->combinations($array, $maxgroup);
for($c=$maxgroup-1;$c>1;$c--)
{
$comb = $combinatorics->combinations($array, $c);
$combinations = array_merge($combinations, $comb);
$comb = null;
}
for($j=0;$j<sizeof($combinations);$j++)
{
for($i=sizeof($combinations)-1;$i>=$j+1;$i--)
{
$diff = array_intersect($combinations[$j], $combinations[$i]);
if(count($diff)>1)
{
unset($combinations[$i]);
}
}
$combinations = array_values($combinations);
}
print_r($combinations);
Since the structure is just obscuring the numbers which are available, you should first unfold the nested arrays. I'll be kind and do that for you:
$numbers = []
foreach ($arrar as $subarr) {
foreach ($subarr as $num) {
$numbers[] = $num;
}
}
I'm assuming there aren't any duplicate numbers in the input.
Next, you want to perform your algorithm for finding the unique combinations. With array this small, even a recursive solution will work. You don't have to try all the combinatorially-many combinations.
i'm trying to generate the following diagram using Image_GraphViz for PEAR. However It only shows a top-level node (with the text "0") and childnodes "1", "2", "3" and "4" directly under the top-node. Am I missing something?
This is the code:
$gv = new Image_GraphViz(true);
$gv->addEdge(array('1', '2'));
$gv->addEdge(array('2', '3'));
$gv->addEdge(array('2', '4'));
$gv->addEdge(array('3', '5'));
$gv->addEdge(array('3', '6'));
$gv->addEdge(array('3', '7'));
$gv->addEdge(array('4', '5'));
echo $gv->fetch('svg');
I tried the following, but this is obviously wrong :).
$gv->addEdge(array('1', '2'));
$gv->addEdge(array('1', '2', '3'));
$gv->addEdge(array('1', '2', '4'));
$gv->addEdge(array('1', '2', '3', '5'));
$gv->addEdge(array('1', '2', '3', '6'));
$gv->addEdge(array('1', '2', '3', '7'));
$gv->addEdge(array('1', '2', '4', '5'));
I understand there are two ways to get to 5. Through (1, 2, 3, 5) and/or (1, 2, 4, 5).
The image should just show two lines going to 5. I'm very confused, any suggestions?
From the documentation it seems you need associative arrays. Can you try this instead?
$gv = new Image_GraphViz(true);
// Notice the use of =>
$gv->addEdge(array('1' => '2'));
$gv->addEdge(array('2' => '3'));
$gv->addEdge(array('2' => '4'));
$gv->addEdge(array('3' => '5'));
$gv->addEdge(array('3' => '6'));
$gv->addEdge(array('3' => '7'));
$gv->addEdge(array('4' => '5'));
echo $gv->fetch('svg');