View Full Version : Making an array with a php variable
letom
06-16-2013, 01:08 PM
Hi ,
I have a php variable $ year which contains values as follows
$year = 1922192319241925
please note these are not a single value, it is year 1922, 1923, 1924, 1925
My issue is i need to check each of this value of year with another variable $yearcheck as follows.
If ($yearcheck = $year) //$yearcheck == "1922" or "1923" or "1924" or "1925"
{
echo "Message";
}
can anybody find a solution for this problem
Beverleyh
06-16-2013, 03:08 PM
Maybe something like this - for php5, although I've included a php4 str_split() function for anyone who might need it;
<?php
// php4 version of php5 native str_split() function
// uncomment if required
/*
function str_split($string, $length=1) {
$string_array = explode("\r\n", chunk_split($string, $length));
return $string_array;
}
*/
$year = "1922192319241925";
$yearcheck = "1923";
$array = str_split($year, 4); // split $year at every 4th character
$yr = array($array[0], $array[1], $array[2], $array[3]);
if (in_array($yearcheck, $yr)) {
echo "Found $yearcheck";
}
?>
This works on the assumption that $year is always formatted as 4 concatenated years, with each year consisting of 4 numbers (although you could extend the $yr array() with more values as required).
jscheuer1
06-16-2013, 04:04 PM
You don't have to copy the array. This will do just fine:
<?php
// php4 version of php5 native str_split() function
// uncomment if required
/*
function str_split($string, $length=1) {
$string_array = explode("\r\n", chunk_split($string, $length));
return $string_array;
}
*/
$year = "1922192319241925";
$yearcheck = "1923";
$yeararray = str_split($year, 4); // split $year at every 4th character
if (in_array($yearcheck, $yeararray)) {
echo "Found $yearcheck";
}
?>
Beverleyh
06-16-2013, 04:26 PM
Yes I just realised that, except I did it with a few variable name differences;
<?php
// php4 version of php5 native str_split() function
// uncomment if required
/*
function str_split($string, $length=1) {
$string_array = explode("\r\n", chunk_split($string, $length));
return $string_array;
}
*/
$year = "1922192319241925";
$yearcheck = "1923";
$yr = str_split($year, 4); // split $year at every 4th character
if (in_array($yearcheck, $yr)) {
echo "Found $yearcheck";
}
?>
Its a Sunday - my brain is still in weekend mode ;)
Still, not bad for a somebody on iPhone :D
jscheuer1
06-16-2013, 09:12 PM
Right, that works. and if you really needed to copy the array or, more to the point perhaps make another version of it slightly changed, a foreach or other loop might be a better choice because you wouldn't have to know ahead of time how many elements were in the array.
letom
06-17-2013, 06:48 AM
Right, that works. and if you really needed to copy the array or, more to the point perhaps make another version of it slightly changed, a foreach or other loop might be a better choice because you wouldn't have to know ahead of time how many elements were in the array.
Thanks for your advice and the coding seems to be fine.
But to solve my issue i need to give some more information as follows.
<?php
// php4 version of php5 native str_split() function
// uncomment if required
/*
function str_split($string, $length=1) {
$string_array = explode("\r\n", chunk_split($string, $length));
return $string_array;
}
*/
$year = "1922192319241925";
$yearcheck = "1923";
$yr = str_split($year, 4); // split $year at every 4th character
if (in_array($yearcheck, $yr)) {
echo "Found $yearcheck";
}
?>
the values of $year is displaying from a loop increment $year++ not in such a way i asked in my first question, so i think it not to be need of splitting ?
May be foreach is solution for that ?
Beverleyh
06-17-2013, 02:18 PM
I'm not sure it matters how $year is being generated, or how long it is - whether
$year = "1922192319241925"; or
$year = "19221923192419251926192719281929193019311932193319341935";
the provided script will just take that string, split at everything 4th characters and add it to the array.
The suggested code might need to be reevaluated if the string is very, very long but we'll need details. I guess we need to see more of your script to make and solid suggestions.
Also - don't forget your formatting.
jscheuer1
06-17-2013, 02:46 PM
I should add that foreach requires an array to work with. So, if you want to use foreach, you need an array, not a string, which is what split does for you.
But you say all you need is to see if a given year ($yearcheck) is in the string $year. If that's all you need you have your answer. If you need something else, what else do you need?
letom
06-17-2013, 07:10 PM
Thanks.. I Solved the issue
Also - don't forget your formatting.
Noted, But I am not getting WYSWING at that time,
But you say all you need is to see if a given year ($yearcheck) is in the string $year. If that's all you need you have your answer. If you need something else, what else do you need?
Yes the solutions provided here are correct, but need to be an expert thinking only in this issue, because the result of $year is from a for loop check the following it will give some what help to other readers
$dec = array();
for ($a=$tyear; $a<=$fyear; $a++)
array_push($dec,$a);
Then make it in
if (in_array($yearcheck, $dec)) {
echo "Found $yearcheck";
}
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.