i have a problem like the following sample code :
$code='100'; //$maybe $code='0' or $code='1' ... i just set a number as an sample
$xx=array(
'0'=>array(a,b,c),
'1'=>array(d,e,f),
........
'100'=>array(aa,bb,cc)
);
I want find $code in array :
if($code==$xx['$code']){
echo $xx['code'][0]; //if i want get the value 'aa'
}
But it seems like $xx['$code'] doesn't work.
Do anyone know the right way to solve it?
Firstly you need to use array_key_exists for getting within if condition and then after you can use it simply like as
if(array_key_exists($code,$xx)){
echo $xx[$code][0];
}
or can simply use isset instead like as
if(isset($xx[$code])){
echo $xx[$code][0];
}
you shouldn't use single quotations here,
when filling displaying a variable either use without quotations or put it between double "" try $xx[$code] or $xx["$code"]
Use isset OR !empty to check key exist in array or not. It will also check that key has valid value.
if(isset($xx[$code])){
echo $xx[$code][0];
}
OR
if(!empty($xx[$code])){
echo $xx[$code][0];
}
if($code==$xx['$code']){
echo $xx[$code][0]; //if i want get the value 'aa'
}
If you use '$code' the content of $code will not be checked as '' interpret everything as a string and won't look a variables in it.
You can't sace 'code' either, as code is only the name of the variable you use.
Related
This might seem weird to ask such a stupid question.
However, I am not able to figure out why I am getting wrong value with such a small piece of code.
Code is:
<?php
error_reporting(0);
$result_all = 'User not found';
if($result_all['BookingDetail']['BookingReference']){
echo "<br>Output in if :- ".$result_all['BookingDetail']['BookingReference'];
}else{
echo "<br>Output in else:- ".$result_all['BookingDetail']['BookingReference'];
}
I expected output to be blank however it returns first character of the string. I know checking if its is_array and isset will resolve my problem.
But I want to understand why its happening?
Problem:-
You are assigning 'User not found' to $result_all variable in your script, which makes it a string variable.
Since $result_all is a string, any index like BookingDetail is considered as 0 for the string and that's why it will give the first character of string
Solution:-
You need to use isset() to check index exist or not?
<?php
//comment these two lines when code started working fine
error_reporting(E_ALL);
ini_set('display_errors',1);
if( isset( $result_all['BookingDetail']['BookingReference'] ) ){
echo "<br>Output in if :- ".$result_all['BookingDetail']['BookingReference'];
}else{
echo "<br>Output in else:- User not found";
}
I ran this on PHP 7.3
This is the output:
Output in if :- UIllegal string offset 'BookingReference' on line 5
Please consider upgrading your PHP version
Because you convert it in array because of the same name of the variable.
$result_all in ARRAY by doing this $result_all['BookingDetail']['BookingReference']
and not define which value of array you want to show so by default it showing the first character of string.
$result_all = 'User not found';
Output:- U
Let's say my echo sometimes:
1234
And sometimes Empty.
Is there a possibility to check if its a number?
Or to check if the result is empty?
echo $check;
if(strpos($check, ' check-number/empty ') !== false){
...to do...
Check the value using an if-else statement to check if current value is null. Use is_numeric to check a value is a number.
Use this:
if(is_numeric($check)){
echo "I am a number";
}
As is_numeric account if a value is null as well. All the best.
I don't fully understand what are you trying to do but to check if a variable is a number you can use the PHP built-in function is_numeric()
I have an array like these
$data=array(
'a'=>'value1',
'b'=>'value2',
'c'=>'value3',
'd'=>array('e'=>'value4','f'=>'value5' ),
);
By using CI how to get the value of 'e' and how to check 'e' is equal to any value or not.
This isn't related to CodeIgniter.
You can just do this: $data['d']['e']
And then to check if it's equal to any value do this:
if ($data['d']['e'] == $anyValue) {
// do something
}
You can get the value as in the case of a two dimensional array..$data['d'] will select the array inside.Then get the value of 'e' or 'f' as $data['d']['e'] or $data['d']['f']. If you want to compare try:
if ($data['d']['e'] == $Value) {
//put your code here.....
}
You can use
echo "<pre>";
print_r($data['d']['e']);
die();
inside your code to check what value you have inside the index 'e'. Always use this technique. Very handy.
By the way, this is standard/raw PHP technique, not CI. You can use raw PHP in CI, there's nothing wrong with that.
Checking whether the value you have inside index 'e' is equal to a specific value, is a very basic thing you probably might've learned back in high school or grad school. It's by using an if() statement that you can compare your 'e' value with the specific value you have.
if($data['d']['e'] == 'somevalue')
{
//do your work here
}
I am reading file. I read all data in $part. it is working fine but issue arise when i use empty function. It should display NULL but it is not showing NULL.
My code is as follows:
echo "\nParts------------".$parts[$r]."\n";
echo "\nParts---Size---------".strlen($parts[$r])."\n";
var_dump($parts[$r]);
// $parts[$r]=trim($parts[$r],' ');
//$parts[$r]=str_replace('""','',($parts[$r]));
if(empty($parts[$r]))
{
$entryarray[$c][$c2]='NULL';
}
else if(strlen($parts[$r])<1) //removing special characters
{
$entryarray[$c][$c2]='NULL';
// array_push($entryarray[$c]);
$valueArray=$valueArray.",".'NULL';
}
when i vardump($part) then it is showing its length is 2 instead of 0.
How to display it NULL. i cant write check as if(strlen($parts<2)) cause there is data in file which has lenght less then 2.
I think you meant to use isset instead.
$foo = array("a", "b");
isset($foo[2]); // false
The empty function check if is null or empty string or 0 or something like that.
If you want to chack if is null, i recommend u to use "is_null()"
To check if a key is on array use
array_key_exists($key,$array);
I have one variable that comes from a database.
I then want to check whether that value is the same of one of the values in an array.
If the variable matches one of the array values, then I want to print nothing, and if the variable does not match one of the array values, then I want to print something.
This is the code I have been trying without luck, I know that contains is not valid code, but that is the bit I cannot find any info for:
<?php
$site = getStuff();
$codes = array('value2', 'value4');
if ($codes contains $site)
{
echo "";
}
else
{
echo "something";
?>
So if the database would return value1 for $site, then the code should print "something" because value1 is not in the array.
The function you are looking for is in_array.
if(in_array($site, array('value2', 'value4')))
if(!in_array($site,$codes)) {
echo "something";
}
To provide another use way to do what the other answers suggest you can use a ternary if
echo in_array($site, $codes)?"":"something";