<?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/>";
?>
Related
How do i correct my code ..i would like to check a value if is in array
$years[] = ''.$myyear.'';
$years_array = "array('" . implode( "','", $years) . "');";
if (in_array("2017", $years_array))
{
//do this
}
else
{
//do this
}
Your in_array with if clause looks fine, but year_array is wrong (which is string not array)
You can define year_array simply like below
$years_array = array(2015,2016,2017,2018);
OR
// Define array
$years_array = array();
// Add elements to array
$years_array[] = 2015;
$years_array[] = 2016;
$years_array[] = 2017;
In case if you have list of years as string separated by comma, then you can create array using explode() function like below
// this is string
$year_string = '2015,2016,2017,2018';
// this is array
$year_array = explode(',', $year_string);
// print string
print $year_string.PHP_EOL;
// print the contents of array
print_r($year_array);
meanwhile you can read more about arrays from here
How can I store dates on a multi-dimensional associative array?
I can't explain further my problem, but what if I have an array that contains different dates. For example:
<?php
$dates = array("2015-06-01","2015-06-07","2015-07-08","2015-08-01", "2015-08-21","2015-08-26");
?>
I want to store the array above into multi dimensional associative array, according to their month, so therefore it would be like this..
<?php
array = array(
"June" => array("2015-06-01","2015-06-07"),
"July"=> array("2015-07-08"),
"August" => array("2015-08-01","2015-08-21","2015-08-26")
);
?>
But in my case, the dates are from the database compare to the example above that are defined dates, how can I group that following dates according to their month and store in an associative array named according to their month and the content is the second dimensional array that contains the date grouped?
Thanks!
My code above are just example: This is my solution but it's not working good! -_-
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/empc/library/functions.php");
$previousmonth = "";
$a = getDistinctDates(); // Function that get dates, nevermind of this.
$data = array();
foreach ($a as $b){
echo $b["date_modified"] . "<br>";
$datemonth = date("F",strtotime($b["date_modified"]));
echo $datemonth . "<br>";
if ($datemonth != $previousmonth){
array_push($data,
array(
$datemonth => $b["date_modified"]
)
);
} else {
array_push($data[$datemonth][],$b["date_modified"]);
}
echo $b["balance_after_approval"] . "<br>";
echo "<br>";
$previousmonth = $datemonth;
}
?>
your over thinking this, just use the month as the array key:
<?php
$dates = array("2015-06-01","2015-06-07","2015-07-08","2015-08-01", "2015-08-21","2015-08-26");
$out=array();
foreach ($dates as $b){
$datemonth = date("F",strtotime($b));
$out[$datemonth][]=$b;
}
print_r($out);
?>
demo:http://codepad.viper-7.com/3Gh9s7
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";
?>
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;
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".