I have two arrays:
$array_one = array(1=>6000,2=>500);
$array_two = array(1=>6500,2=>250);
I would like to compare the values with > or < like this:
if(6000 > 6500){
echo "ok";
}else{ echo "not allowed";}
if(500> 250){
echo "ok";
}else{ echo "not allowed";}
How can I perform this type of operation using a loop or something else?
You access array values using the square bracket notation [index], therefore you can simply reference the values using their index;
if($array_one[1] > $array_two[1]) {
echo "ok";
}
else {
echo "not allowed";
}
and then you can put that in a loop, like this;
for($i=1;$i<=count($array_one);$i++) {
if($array_one[$i] > $array_two[$i]) {
echo "ok";
}
else {
echo "not allowed";
}
}
Hope this helps.
Try the following:
<?php
foreach($array_one as $key => $value) {
if($value > $array_two[$key]) {
echo "OK";
} else {
echo "Not Allowed";
}
}
?>
Try this:
$array_one = array(1=>6000,2=>500);
$array_two = array(1=>6500,2=>250);
foreach($array_one as $k => $v)
{
if($v > $array_two[$k]){
echo "ok";
}else{ echo "not allowed";}
}
Related
I want to check if the data is match between array and the string.
Im trying to check if the string is equals to each other but the problem is the condition is returning false and both value of $subex2[1] and $subdat is IT100. I think the problem is you can't compare an array to a string. Can someone help me about this?
here's the code
$subex = $objPHPExcel->getActiveSheet()->getCell('D8')->getValue();
$subex2 = explode(":", $subex);
$q = "select * from class where id='$id'";
$r = mysql_query($q);
$data = mysql_fetch_array($r);
$subdat = $data['subject'];
if($subdat == $subex2[1]) {
echo "Data matched";
}else {
echo "Data doesn't matched";
}
As mentioned in the comments. One value has an space before it. You can solve this kind of problems like this:
if(trim($subdat) == trim($subex2[1])) {
echo "Data matched";
} else {
echo "Data doesn't matched";
}
for case sensitive issue, this trick should apply.
if(strtolower(trim($subdat)) == strtolower(trim($subex2[1]))) {
echo "Data matched";
} else {
echo "Data doesn't matched";
}
this test works fine
$subdat = "IT100";
$subex2[1] = "IT100";
if($subdat == $subex2[1]) {
echo "Data matched";
}
You are not comparing same string
You first use array_map and trim values, delete space, next use in_array, example
$subex2 = array_map('trim',$subex2);
if( is_array($subex2) ){
if(in_array($subdata, $subex2)){
echo "Data matched";
} else {
echo "Data doesn't matched";
}
}
It's always good to check if it's actually an array, with is_array
Reference in_array https://www.w3schools.com/php/func_array_in_array.asp
try this:
$isMatched = strval($subex2[1]) === strval($subdat) ?: false;
Try this code:
You use the URL with the parameter id e.g index.php?id=value
This value will be pushed into the data array that is maintained within a session.
I expect this test to always return true. But it always returns false, why?
if (in_array($id, $_SESSION['data'])) {
echo "$id in array";
} else {
echo "$id not in array";
}
The full code:
<?php
session_start();
//uncomment when need to clear the data array.
//if (isset($_SESSION['data'])) {
// unset($_SESSION['data']);
// die;
//}
if (!isset($_SESSION['data'])) {
$_SESSION['data'] = [];
}
if (isset($_GET['id'])) {
$id = strval($_GET['id']);
if (!in_array($id, $_SESSION['data'])) {
$_SESSION['data'][$id] = "data_$id";
}
echo '<pre>';
print_r($_SESSION['data']);
echo '</pre>';
// Why does this always return false?
if (in_array($id, $_SESSION['data'])) {
echo "$id in array";
} else {
echo "$id not in array";
}
}?>
Try isset() function.
Example:-
if (isset($_SESSION['data'][$id])) {
echo "$id in array";
} else {
echo "$id not in array";
}
Following is my code here actually o/p should be hi..but it is giving no
<?php
$arr=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$c='xyz,ccc';
if(in_array(isset($c) && $c,$arr))
{
echo 'hi';
}
else
{
echo 'no';
}
?>
output:hi
actual result should be 'no'.
Side note, this is bad code:
in_array(isset($weekendArr) && $weekendArr,$arr)
do it like
isset($weekendArr) && in_array($weekendArr,$arr)
and in_array is not strict so this
in_array(true,array('w','s'))
will be allways TRUE
do it with:
in_array(true,array('w','s'),true)
and you see.
And you can't check an array with an array the $needle be an STRING here.
The only solution is to do splitt your STRING into two values and then check two times for TRUE
$c='Sunday,Monday';
foreach(explode(',',$c) as $check){
if(in_array($c,$arr,true))
{
echo $check.' is in array';
}
else
{
echo $check.' is NOT in array';
}
}
Hope that helps a little.
<?php
$listDays=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$day='Sunday'; //You cant test both days ! Just one value at a time
if(true === in_array($day, $listDays))
{
echo 'hi';
}
else
{
echo 'no';
}
?>
Or option two if you want to test different days
<?php
$listDays=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$dayToTest='Sunday, Monday'; //Here we have multiple days
$tabTest = preg_split(',', $day); //split into an array
//Then test for each string in tabTest
foreach($tabTest as $string)
{
if(true === in_array($string, $listDays))
{
echo $string.' is OK';
}
else
{
echo 'no';
}
}
?>
Change your code to:
<?php
$arr=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$c='Sunday,Monday';
if(in_array(isset($c) && $c,$arr))
{
echo 'hi';
}
else
{
echo 'no';
}
my questions:
$state=array("你"=>1);
if(array_key_exists("你",$state))
{
$result = array_search("你",$state);echo $result;
}else
{
echo "No Exists";
}
i expect the result of "1", however the output is "No Exists", i don't know why the program can't get the value of the key "你".
array_search will search the given array by value. Try the following:
$state = array("你"=>1);
if(array_key_exists("你", $state)) {
echo $state["你"];
} else {
echo "No Exists";
}
// => 1
» demo
Hope below function will help.
<?php
$array = array('arr1'=>array('find_me'=>'yes you did.'));
function get_value_by_key($array,$key)
{
foreach($array as $k=>$each)
{
if($k==$key)
{
return $each;
}
if(is_array($each))
{
if($return = get_value_by_key($each,$key))
{
return $return;
}
}
}
}
echo get_value_by_key($array,'find_me');
?>
the encoding type of the show paper and the store paper is GB2312.
$state=array("你"=>1);
if(array_key_exists("你",$state))
{
$result1 = $state["你"];
echo $result1; // can get the value 111
}else
{
echo "No Exists";
}
the code above can be executed rightly. i can't show my problems accurately. Now i paste out my code , there is some questions.
<?php
$file = file("GB2312-HanZiBianMa.txt"); // file encoding type ANSI
foreach ($file as $line_num => $line)
{
list($no,$hex,$dec) = preg_split('[\t]',htmlspecialchars($line));;
$result[$hex] = $dec;
}
$result_2 = array_flip($result);
if(array_key_exists("你",$result_2)) // **can't find the value** 222
{
$state= $result_2["你"];
echo $state."<br/>";
}else
{
echo "No Exists<br/>";
}
foreach($result_2 as $k=>$each) //can get the value using the preg_match
{
if(preg_match('/你/', $k))
echo $k."\t".$each."<br/>";
}
?>
the format of GB2312-HanZiBianMa.txt is as follows:
1947 c4e3 你
1948 c4e4 匿
1949 c4e5 腻
1950 c4e6 逆
if your want to test the code , you can save the php code and save the GB2312.. file.
the question is:
why can't the following function get the right value ? the data comes from file and one stores together.
if(array_key_exists("你",$result_2)) // **can't find the value** 222
{
$state= $result_2["你"];
echo $state."<br/>";
}else
{
echo "No Exists<br/>";
}
i wanted to arrange this arrays element in assending order and wrote the below code :
<?php
$a=array("z","s","a","j","t","b");
for($i=0;$i<=6;$i++)
{
if ($i[0]<$i[1]) { echo $i[1]; }
else if ($i[1]<$i[2]) { echo $i[2]; }
else if ($i[2]<$i[3]) { echo $i[3]; }
else if ($i[3]<$i[4]) { echo $i[4]; }
else if ($i[4]<$i[5]) { echo $i[5]; }
else if ($i[5]<$i[6]) { echo $i[6]; }
else if ($i[6]<$i[7]) { echo $i[7]; }
else if ($i[7]<$i[8]) { echo $i[8]; }
else if ($i[8]<$i[9]) { echo $i[9]; }
else if ($i[9]<$i[10]) { echo $i[10]; }
else if ($i[10]<$i[11]) { echo $i[11]; }
else ($i[11]<$i[12]) { echo $i[12]; }
}
?>
but i an getting the following error :
Parse error: syntax error, unexpected '{' in C:\wamp\www\arange.php on line 16
how can i rectify it
This snippet is the problem:
else ($i[11]<$i[12]) { echo $i[12]; }
Either edit it into elseif or remove the ($i[11]<$i[12]).
I'd do this differently. Consider using PHP's built in sort() function.
$a = array("z","s","a","j","t","b");
sort($a);
foreach ($a as $element) {
echo "$element\n";
}
Also read about the foreach statement.
$b = '';
$a=array("z","s","a","j","t","b");
foreach($a as $i) if($i > $b) $b = $i;
echo $b;
Check out the manual for a clear example of the elseif/else if syntax. The else part in your code is the problem.
if ($i[5]<$i[6]) { echo $i[6]; }
actually will output something like this;
if ( b < ) { echo ; }
That why you see kinda error...