So i have a very simple snippet. I studied the in_array command and found it would be what i need.
However its not working?
I have tried several scenarios:
$this->item->tour_days is an array containing 1,2,3
test 1
$days = $this->item->tour_days;
$array = array($days);
if (in_array(2,$array,TRUE)) {
echo 'Tuesday';
}
test 2
$days = $this->item->tour_days;
$array = array($days);
if (in_array(2,$array)) {
echo 'Tuesday';
}
test 3
$days = $this->item->tour_days;
$array = array($days);
if (in_array('2',$array)) {
echo 'Tuesday';
}
I have tried to echo Tuesday where Tuesday = 2 from my csv but no luck.
Thanks in advance for nay help here
jonny
To convert a string containing a comma-delimited list into an array, use explode:
$array = explode(',', $days);
$this->item->tour_days is an array containing 1,2,3
Then why do you want to make another array out of it?
remove the $array = array($days); converstion.
Instead say $array = $days;
Try this:
$array = explode(",",$days);
This should work for you:
If $days is a string then use this:
$days = "1,2,3"; //$this->item->tour_days;
$array = explode(',', $days);
if(in_array(2, $array))
echo "Tuesday";
?>
If $days is already an array use this:
<?php
$days = array(1,2,3); //$this->item->tour_days;
if(in_array(2, $days))
echo "Tuesday";
?>
Related
I have values in my database that has values with commas. Eg:
Monday,Tuesday,Wednesday,Thursday,Friday. When I call the row from the database where certain id, I want to write the the result to a text file after every comma into a new line as follows:
Monday
Tuesday
Wednesday
Thursday
Friday
One below the other.
Hope this makes sense.
You can use explode function.
explode — Split a string by string
$days = "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday";
$weekdays = explode(",", $days);
foreach( $weekdays as $value ){
echo $value."\n";
}
Output:
Monday
Tuesday
Wednesday
Thursday
Friday
I think it will work for you!
Something like that:
$result = "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday";
$array = explode(",", $result);
foreach( $array as $key => $value ){
echo $value."\n";
}
explode function will help you
$str = "Monday,Tuesday,Wednesday,Thursday,Friday";
$val = explode(",",$str);
foreach ($val as $key=>$value) {
echo $value.'</br>';
}
Use something like this
$colVal = $row['col-name'];
echo str_replace(",", "\r\n", $colVal);
this is my problem 1 here: Handle number in string PHP . I solved it.
Now, i see new problem, you can see the picture:
I want to get only number, not date (500000 and 200000) and sum it.
This is my code without date:
$total= 0;
$ex = explode(' ',$_POST['txtSalary']);
function total($ex) {
global $total;
return $total+=$ex;
}
array_map('total',$ex);
echo $total."<br/>";
I try so much but no result, hope you can help me. Thank you!
I assume that your $_POST['txtSalary'] look like below:-
$_POST['txtSalary'] = '-27/07/2016: 5000000
-01/08/2016: 2000000';
So do like below:-
<?php
$_POST['txtSalary'] = '-27/07/2016: 5000000
-01/08/2016: 2000000';
$array = explode(PHP_EOL, $_POST['txtSalary']);
print_r($array);
$sum = 0;
foreach($array as $arr){
$sum += explode(': ',$arr)[1];
}
echo $sum;
Output:- https://eval.in/612692
My array contain date $a1[0] day $a1[1] month $a1[2] year I want the result as year/day/month give me one solution.
Below is my code
<?php
$a1 = array("01","10","2012");
$result = implode("/",$a1);
print $result;
?>
This will print 01/10/2012 but I want this result 2012/01/10. How can I take the array manualy using implode function?
How can I take year first day second and month third?
You can't use implode alone for that matter, if you are having exactly this pattern (elements order are inverted) the use this code (YYYY/MM/DD):
$a1 = array("01", "10", "2012");
$result = implode("/", array_reverse($a1));
print $result;
Else use a regex (YYYY/MM/DD):
$a1 = array("01", "10", "2012");
$result = preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/', '$3/$2/$1', implode("/", $a1));
print $result;
If you need the format of (YYYY/DD/MM) then use this one:
$a1 = array("01", "10", "2012");
$result = preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/', '$3/$1/$2', implode("/", $a1));
print $result;
You can use array_reverse like this:
$result = implode("/", array_reverse($a1));
This will create a new array in the reverse order.
Use the mktime function. If you have a $timestamp, you can format is as you like with the date function:
$timestamp = mktime(0, 0, 0, $a1[1], $a1[0], $a1[2]);
echo date('y/m/d', $timestamp);
You can also use a loop (manual implode):
$a1 = array("01","10","2012");
$a1_length = sizeof($a1);
$date = '';
for ($i = $a1_length; $i > 0; $i--) {
$date .= $a1[$i] . '/';
}
$date = rtrim($date, '/');
echo $date;
<?php
$d1 = '1';
$d2 = '1';
$d3 = '1';
$date2 = ''.$d1.','.$d2.','.$d3.'';
I want to to put these values in an array, but when I assign a variable as array($date2) it just gets printed as 'Array'.
What is the problem?
*Updated, this is the code that is printing 'Array':
$date2 = array($date);
echo $date2;
Firstly, you cannot just echo array('data');.
To put data into an array such as that, you need to specify it in the following format:
$array = array($d1,$d2,$d3); //build array
print_r($array); //show array contents
echo $array[0] //echo first array element
You can also add data to an array as follows:
// add to array
$array[] = 'item 1';
$array[] = 'item 2';
// add to array
array_push($array,'item 3');
Additional reading: foreach & arrays
Try this:
$date2 = array($d1, $d2, $d3);
That will create an array with those values.
If you want to view the contents of the array, do this:
var_dump($date2);
If you want to show what's in an array you can use var_dump or print_r. You can't use echo to view an array (unless you do it like Briedis describes).
Putting in the array:
$array['key'] = "Value";
Printing out:
echo "Array: " . $array['key'];
$date2[] = array();
$date2[0] = 10;
$date2[1] = 20;
$date2[2] = 30;
$date2 = array();
array_push($date2,$d1);
array_push($date2,$d2);
array_push($date2,$d3);// or one in all $date2 = array($var1,$var,$var3);
print_r($date2);
for printing array use print_r
you should define $date2 like this:
$date2 = ($d1,$d2,$d3);
the way you have it now, $date2 is just a string of concatenated values
As you wrote using
print_r(array($date2));
will output Array ( [0] => 1,1,1 )
If you want add the three values separatly you should write this:
$date2 = array($d1, $d2, $d3);
Try the following code it will give you the expected output. There are three way print_r() , var_dump() and the ever green foreach(). you can use any of them.
<?php
$d1 = '1';
$d2 = '1';
$d3 = '1';
$date = ''.$d1.','.$d2.','.$d3.'';
$date2 = array($date);
echo "with print_r:";
print_r($date2);
echo "<hr/>";
echo "with var_dump:";
var_dump($date2);
echo "<hr/>";
echo "with foreach:<br/>";
foreach($date2 as $v)
echo "$v<br/>";
?>
Is there any ways to use DATE in PHP as array. I need to achieve something as below:
$date_array = (1-Jan => 'A', 2-Jan => 'B', 3-Jan => 'C', .... so on)
Is it possible?
Manually:
$array['1-Jan'] = 'A';
$array['2-Jan'] = 'B';
...
Or with a loop:
$array = array();
$currentDate = strtotime('2010-01-01');
$totalDays = 365;
for ($i=0; $i<$totalDays; $i++) {
$array[date('j-M', $currentDate)] = $i;
$currentDate = strtotime("+1 day", $currentDate);
}
The PHP manual specifies that only string and integer can be used as keys for assoc. arrays, but date returns a string, so they should be fine, but to retrive the values back would be a bit messy.
What about something like:
$arr = Array(
'A'=>date('Y-m-d', time()),
'B'=>date('Y-m-d', time()-(7 * 24 * 60 * 60))
);
echo in_array(date('Y-m-d'), $arr);
That echos "1".
My mistake, you would need to use array_search() instead of in_array() so you get the array key:
echo array_search(date('Y-m-d'), $arr);
Ouputs "A".