while($row = $update_post->fetch_array()){
//Explodes checkbox values
$res = $row['column_name'];
$field = explode(",", $res);
$arr = array('r1','r2,'r3','r4','r5','r6');
if (in_array($arr, $field)) {
echo "<script>alert('something to do')</script>";
}else{
echo "<script>alert('something to do')</script>";
}
}
How to check if value of $arr is equal to the value of $field.
Thank you in Advance.
If you want to match two array than you need to use array_intersect() here.
If you want to check specific value with in_array() than you need to use loop here as:
<?php
$res = $row['column_name'];
$field = explode(",", $res);
$arr = array('r1','r2','r3','r4','r5','r6');
foreach ($arr as $value) {
if(in_array($value, $field)) {
echo "success";
}
else{
echo "failed";
}
}
?>
According to manual: in_array — Checks if a value exists in an array
Also note that, you have a syntax error in your array:
$arr = array('r1','r2,'r3','r4','r5','r6'); // missing quote here for r2
Update:
If you want to use array_intersect() than you can check like that:
<?php
$arr1 = array('r1','r2');
$arr2 = array('r1','r2','r3','r4','r5','r6');
$result = !empty(array_intersect($arr1, $arr2));
if($result){
echo "true";
}
else{
echo "false";
}
?>
DEMO
Update 2:
If you want to check which value are you getting by using array_intersect() than you can use like:
<?php
$arr1 = array('r2');
$arr2 = array('r1','r2','r3','r4','r5','r6');
$result = array_intersect($arr1, $arr2);
if(count($result)){
echo "Following ID(s) found: ".implode(",",$result);
}
?>
DEMO
compare two array by array_intersect then check with count to know matches array value has...
array_intersect
Compare the values of two arrays, and return the matches:
while($row = $update_post->fetch_array()){
//Explodes checkbox values
$res = $row['column_name'];
$field = explode(",", $res);
$arr = array('r1','r2','r3','r4','r5','r6');
if (count(array_intersect($arr, $field)) > 0) {
echo "<script>alert('duplicate array')</script>";
}else{
echo "<script>alert('something to do')</script>";
}
}
Related
I have already try like this
<?php
echo '<pre>';
$arrayName = array(1,2,3,4,5,6,2,3,1 );
$arr= array_count_values(array_column($arrayName,'5'));
print_r($arr);
?>
I just want to counting of repeated value
This script counts the amount of each value.
<?php
echo '<pre>';
$arrayName = [1,2,3,4,5,6,2,3,1];
$arr = [];
foreach ($arrayName as $item) {
if (empty($arr[$item]))
$arr[$item] = 0;
$arr[$item] += 1;
}
print_r($arr);
if you want count the values
$arrayName = array(1,2,3,4,5,6,2,3,1 );
$arr =array_count_values($arrayName);
print_r($arr);
Why are you using array_column if you only want to count repeated values
you can do this
<?php
echo '<pre>';
$arrayName = array(1,2,3,4,5,6,2,3,1 );
$arr= array_count_values($arrayName);
print_r($arr);
?>
I want the loop just to output once. Instead it outputs twice. Here is the code:
$results = mysql_query($query);
while ($c = mysql_fetch_array($results)){
$individualPostcode = explode(",", $c['postcode']);
foreach($individualPostcode as $val){
$val = trim($val); //Get rid of spaces
if($val === $postcode){
echo $c['url']."<br>";
}
}
}
}
Here is the output:
http://www.dyno.com/home-security/local-experts/greater-london/dyno-locks-and-alarms-enfield
http://www.dyno.com/home-security/local-experts/greater-london/dyno-locks--alarms-enfield
http://www.dyno.com/home-security/local-experts/greater-london/dyno-locks-and-alarms-enfield
http://www.dyno.com/home-security/local-experts/greater-london/dyno-locks--alarms-enfield
I've tried taken out the foreach loop but I need to go through that array checking against a user input.
Here is the initialisation of $postcode:
$userInput = $_POST["input"];
if(strlen($userInput) < 4)
echo "User Input : ".$userInput."<br>";
else //Below gets the first three chars of the users string
echo "User Input : $userInput<br>What is being used : ".mb_substr($userInput, 0, 3)."<br>";
$postcode = mb_substr($userInput, 0, 3);
You can always create an array of the URL's to stop them from duplicating by checking if the url has been put into the array:
$results = mysql_query($query);
$urlsArr = array();
while ($c = mysql_fetch_array($results)){
$individualPostcode = explode(",", $c['postcode']);
foreach($individualPostcode as $val){
$val = trim($val); //Get rid of spaces
if($val === $postcode){
if (!in_array($c['url'], $urlsArr)) echo $c['url']."<br>";
$urlsArr[] = $c['url'];
}
}
}
mysql_fetch_array returns both an associative and index array for each of your returned results. The foreach loop is going to loop over both and output twice. Try using mysql_fetch_assoc()
http://php.net/manual/en/function.mysql-fetch-array.php
Better still, try moving to the mysqli class. It's faster and mysql is depricated.
http://php.net/manual/en/intro.mysqli.php
I am trying to compare the array and values
I have something like
$values = array(1,5,9);
$array = array(1,3,5,7,9);
I want to get if 1,5,9 is within $array
so I did
foreach($values as $value) {
if(in_array($value, $array)){
echo "found";
}else{
//i also need get 3 and 7 from `$array` because they are not in values array
//How do I do this?
}
}
However, I also need get 3 and 7 from $array because they are not in values array. Is there anyways to do this? Thanks.
You cant do it in that foreach, however, have you considered something like this:
$values = array(1,5,9);
$array = array(1,3,5,7,9);
foreach($values as $value) {
if(in_array($value, $array)){
echo "found";
}
}
//check numbers that are not in array
$diff = array_diff($array, $values);
print_r($diff);
Maybe searching in the target array
foreach($array as $value) {
if(in_array($value, $values)){
echo "found: ".$value;
}else{
echo "not found: ".$value;
}
}
Just store them in a new array:
$valuesNotInArray = array();
foreach($values as $value) {
if(in_array($value, $array)){
echo "found";
}else{
//i also need get 3 and 7 from `$array` because they are not in values array
//How do I do this?
array_push($valuesNotInArray, $value);
}
And then do whatever you want with the elements in $valuesNotInArray.
Or echo them out:
foreach($values as $value) {
if(in_array($value, $array)){
echo "found";
}else{
//i also need get 3 and 7 from `$array` because they are not in values array
//How do I do this?
echo $value;
}
For example:
$arr = array(3,5,2,5,3,9);
I want to show only common elements i.e 3,5 as output.
Here's my attempt:
<?php
$arr = array(3,5,2,5,3,9);
$temp_array = array();
foreach($arr as $val)
{
if(isset($temp_array[$val]))
{
$temp_array[$val] = $val;
}else{
$temp_array[$val] = 0;
}
}
foreach($temp_array as $val2)
{
if($val2 > 0)
{
echo $val2 . ', ';
}
}
?>
--
Output --
3, 5,
Try the following:
$arr = array(3,5,2,5,3,9);
foreach($arr as $key => $val){
//remove the item from the array in order
//to prevent printing duplicates twice
unset($arr[$key]);
//now if another copy of this key still exists in the array
//print it since it's a dup
if (in_array($val,$arr)){
echo $val . " ";
}
}
Output:
3 5
Addition:
I guess that the reason you were asked to implement it yourself (without using built-in functions) was to avoid answers like:
$unique = array_unique($arr);
$dupes = array_diff_key( $arr, $unique );
$arrnew = array();
for($i=0;$i<count($arr);$i++)
{
for($j=$i+1;$j<count($arr);$j++)
{
if($arr[$i]==$arr[$j])
{
$arrnew[]=$arr[$j];
}
}
}
I have a function to use but I don't know the content of this function. The only thing I know is that the function returns an array of associative arrays and the keys for the arrays. The data that the function returns come from a database. Can you help in how to read the data from this array? I am confused with the arrays. For now I am doing this:
$array = myfunction($var);
if(!empty($array))
{
while($row = mysql_fetch_array($array))
{
print"$row[elem1]
$row[elem2]";
}
}
I take the error: Warning: mysql_fetch_array() expects parameter 1 to be resource, array given in... I know that something is missing, but I till now I can't fix it.
If the function is returning an array then why are you using it in mysql_fetch_array. It is useless. Instead use this
foreach($array as $key => $value){
echo $key;
echo '<br>';
echo $value;
}
This will print the whole array.
Or a short method is
echo '<pre>';
print_r($array);
echo '</pre>';
Something like -
$array = myfunction($var);
foreach($array as $key => $row) {
print"{$row['elem1']} {$row['elem2']}";
}
You have to pass Resource Identifier to mysql_fetch_array() function.
Something like:
$sql = mysql_query('SELECT * FROM `table`');
if(!empty($array)) {
while($row = mysql_fetch_array($array)) {
print"$row[elem1]
$row[elem2]";
}
}
The error is correct .. the issue is not associative arrays in php but you are not using a valid mysql resource
Please see http://php.net/manual/en/function.mysql-fetch-array.php for documentation
Examples
$mysqli = new mysqli("localhost","root","","test");
$result = $mysqli->query("SELECT * FROM test");
$row = array() ;
echo "<pre>" ;
if($result->num_rows > 0)
{
while($row = $result->fetch_array(MYSQLI_NUM))
{
print implode (",", $row) . PHP_EOL;
}
}
Or
$array = myfunction($var);
if(!empty($array))
{
foreach($array as $key => $row)
{
if(is_array($row))
{
print implode (",", $row) . PHP_EOL;
}
else
{
print $row . PHP_EOL ;
}
}
}