I need some help with this function to finish my project.
3 variables:
$cityName1 = "New York";
$cityName2 = "Madrid";
$cityName3 = "Paris";
The function:
function cityNameFunction($cityName) {
$city_name = $cityName;
return $city_name;
}
Calling the function:
$cityName = array();
for($x = 1; $x <= 3; $x++) {
$cityName[$x] = ${'cityName'.$x};
}
$cityName1 = cityNameFunction($cityName1);
$cityName2 = cityNameFunction($cityName2);
$cityName3 = cityNameFunction($cityName3);
What do I have to do if I have 2000 cities?
Thanks for any help
Strange example, but you may write something like this
$cityName = array();
for ($x = 1; $x <= 3; $x++) {
$cityName[$x] = ${'cityName'.$x};
${'cityName'.$x} = cityNameFunction(${'cityName'.$x});
}
An example of Itterating over a PHP Function at work:
$new = array(1,2,3,4);
for($i=0;$i<=3;$i++)
{
$val = $new[$i];
if(!function_exists('myfunction'))
{
function myfunction($value) {
//Do something
}
}
echo $val;
}
Related
I have these seven php variables:
$dataA = 1;
$targetA = 2;
$dataB = 3;
$targetB = 4;
$dataC = 5;
$targetC = 6;
results = array('A','B','C');
I would like to loop through the results array and pass the corresponding variables to a function.
foreach($results as $value) {
$data = '$data'.$value;
$target = '$target'.$value;
buildOutput('$data'.$value,'$target'.$value);
}
// example, first time thru, wish to pass $dataA variable and $targetA variable
function buildOutput($data,$target) {
echo "data=$data,target=$target<br>"; // echo's strings "$dataA" and "$targetA"
}
I cannot figure out how to declare the variables. Or if this is even possible.
I have more than just $data and $target variables, but I simplified down to two for the question.
I would not recommend using it, but try this:
$dataA = 1;
$targetA = 2;
$dataB = 3;
$targetB = 4;
$dataC = 5;
$targetC = 6;
$results = array('A','B','C');
foreach($results as $value) {
buildOutput(${'data'.$value}, ${'target'.$value});
}
function buildOutput($data, $target) {
echo "data=$data,target=$target<br>";
}
$client = Client::all();
$infos = array();
$counter = 0;
$a_name = "ayman";
foreach($client as $cl) {
if ($cl->name == $a_name)
//code
}
Can I use that ? but it does'nt work !!
$count_client = $Client::count();
for($i = 0; $i<$count_client; $i++) {
if ($cl[i]->name == $a_name) {
//code
}
}
It's a simple code but would know how to use "for loop" instead of "foreach loop" to set the data to an array.
Try This....
for ($x = 0; $x < count($client); $x++)
{
if($client[x]->name == $a_name)
//.........
}
You Can Do This
$client = Client::all();
$a_name = "ayman";
for($i=1; $i <= count($client) ; $i++)
{
if ($client[$i]->name == $a_name)
{
//your code
}
}
Converted your foreach to for loop
$client = Client::all();
$count = count($client);
$a_name = "ayman";
for($i=0; $i < $count ; $i++) {
if ($client[$i]->name == $a_name)
//your code
}
}
PHP NOOB, i have been working on this code all day, but cant find any good way to fix it.
here is my code:
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
$m.$i = 20;
}
else
{
$m.$i = 10;
}
}
Am trying to join $m.$i together and set it to 20 or 10, but i end up having me20 or me10 instead of me1 = 20 or me1 = 10 when i echo $m.$i which is legit, is there anyways to make this work?
$m.$i = 20;
This will assign $i = 20 and then concatenate it with $m and hence you will see me20.
What you need is $m . $i .= 20; instead. which will concatenate them altogether.
Fixed:
<?php
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
echo $m . $i .= 20;
}
else
{
echo $m.$i .= 10;
}
}
?>
EDIT:
The above answer was a total misunderstanding, I realised you intended to create the variables:
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
${$m.$i} = 20;
echo $me0;
}
else
{
${$m.$i} = 10;
}
}
Assign it like so.
${$m.$i} = 20;
You are trying to dynamically create variables, so you have to do something like this:
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
${$m.$i} = 20;
}
else
{
${$m.$i} = 10;
}
}
then try to priny $me0, $me1
already look around but cant find what i want for PHP.
just say i have a number : 1234 ( can be splitted first into array )
and i want to get how many number combination possible for 2 digits, 3 digits , and 4 digits
for example :
possible 4 digits will be :
1234,1243,1324,1342, and so on. ( i dont know how many more )
possible 2 digits will be :
12,13,14,21,23,24,31,32,34,41,42,43
the closest one i get is :
$p = permutate(array('1','2','3','4'));
$result = array();
foreach($p as $perm) {
$result[]=join("",$perm);
}
$result = array_unique($result);
print join("|", $result);
function permutate($elements, $perm = array(), &$permArray = array()){
if(empty($elements)){
array_push($permArray,$perm); return;
}
for($i=0;$i<=count($elements)-1;$i++){
array_push($perm,$elements[$i]);
$tmp = $elements; array_splice($tmp,$i,1);
permutate($tmp,$perm,$permArray);
array_pop($perm);
}
return $permArray;
}
but how can i edit this so i can display for 3 and 2 digits ?
Thanks
i got what i want
it's from #mudasobwa link. and i edit to what i want.
<?php
$in = array(1,2,3,4,5,6);
$te = power_perms($in);
// print_r($te);
$thou=0;
$hun =0;
$pu = 0;
for($i=0;$i<count($te);$i++)
{
$jm = count($te[$i]);
for($j=0;$j<$jm;$j++)
{
$hsl[$i] = $hsl[$i] . $te[$i][$j];
}
if($hsl[$i] >=100 && $hsl[$i] < 1000 )
{
$ratus[$hun] = intval($hsl[$i]);
$hun = $hun + 1;
}
if($hsl[$i] <100 && $hsl[$i] >=10)
{
$pul[$pu] = intval($hsl[$i]);
$pu = $pu + 1;
}
if($hsl[$i] >=1000 && $hsl[$i] < 10000)
{
$th[$thou] = intval($hsl[$i]);
$thou = $thou + 1;
}
}
$th=array_unique($th);
$pul = array_unique($pul);
$ratus = array_unique($ratus);
sort($ratus);
sort($pul);
sort($th);
print_r($th);
function power_perms($arr) {
$power_set = power_set($arr);
$result = array();
foreach($power_set as $set) {
$perms = perms($set);
$result = array_merge($result,$perms);
}
return $result;
}
function power_set($in,$minLength = 1) {
$count = count($in);
$members = pow(2,$count);
$return = array();
for ($i = 0; $i < $members; $i++) {
$b = sprintf("%0".$count."b",$i);
$out = array();
for ($j = 0; $j < $count; $j++) {
if ($b{$j} == '1') $out[] = $in[$j];
}
if (count($out) >= $minLength) {
$return[] = $out;
}
}
// usort($return,"cmp"); //can sort here by length
return $return;
}
function factorial($int){
if($int < 2) {
return 1;
}
for($f = 2; $int-1 > 1; $f *= $int--);
return $f;
}
function perm($arr, $nth = null) {
if ($nth === null) {
return perms($arr);
}
$result = array();
$length = count($arr);
while ($length--) {
$f = factorial($length);
$p = floor($nth / $f);
$result[] = $arr[$p];
array_delete_by_key($arr, $p);
$nth -= $p * $f;
}
$result = array_merge($result,$arr);
return $result;
}
function perms($arr) {
$p = array();
for ($i=0; $i < factorial(count($arr)); $i++) {
$p[] = perm($arr, $i);
}
return $p;
}
function array_delete_by_key(&$array, $delete_key, $use_old_keys = FALSE) {
unset($array[$delete_key]);
if(!$use_old_keys) {
$array = array_values($array);
}
return TRUE;
}
?>
I have problem with $res array.... i don't know with $res[0] is like a empty variable (array have a lot of dates but i don't know why and where is wrong... i tried with $res['ItemSlotX'] and now with $res[0]...
Edited: i have already changed my $res[0] and 1 but is same...
Thank you for help!
function smartsearch($whbin,$itemX,$itemY) {
if (substr($whbin,0,2)=='0x') $whbin=substr($whbin,2);
$items = str_repeat('0', 240);
$itemsm = str_repeat('1', 240);
$i = 0;
while ($i<240) {
$_item = substr($whbin,(64*$i), 64);
$type = (hexdec(substr($_item,18,2))/16);
$dbgetvalutslots = new DB_MSSQL;
$dbgetvalutslots->Database='DTRMUWAP';
$dbgetvalutslots->query("Select [ItemSlotX],[ItemSlotY] from ItemDetails where ItemIndex = '".hexdec(substr($_item,0,2))."' and ItemGroup = '".$type."'");
$ijj=0;
$res=array();
while ($dbgetvalutslots->next_record()) {
$temp = array(
'ItemSlotX' => $dbgetvalutslots->f('ItemSlotX'),
'ItemSlotY' => $dbgetvalutslots->f('ItemSlotY')
);
$res[$ijj]=$temp;
$ijj++;}
$y = 0;
while($y<$res[1]) {
$y++;
$x=0;
while($x<$res[0]) {
$items = substr_replace($items, '1', ($i+$x)+(($y-1)*8), 1);
$x++;
}
}
$i++;
}$y = 0;
while($y<$itemY) {
$y++;$x=0;
while($x<$itemX) {
$x++;
$spacerq[$x+(8*($y-1))] = true;
}
}
$walked = 0;
$i = 0;
while($i<120) {
if (isset($spacerq[$i])) {
$itemsm = substr_replace($itemsm, '0', $i-1, 1);
$last = $i;
$walked++;
}
if ($walked==count($spacerq)) $i=119;
$i++;}$useforlength = substr($itemsm,0,$last);
$findslotlikethis='^'.str_replace('++','+',str_replace('1','+[0-1]+', $useforlength));
$i=0;$nx=0;$ny=0;
while ($i<120) {
if ($nx==8) { $ny++; $nx=0; }
if ((eregi($findslotlikethis,substr($items, $i, strlen($useforlength)))) && ($itemX+$nx<9) && ($itemY+$ny<16))
return $i;
$i++;
$nx++;
}
return 1337;}
There seems to be a lack of context for the code in order to really understand it, but for what I can see here already, it should be:
Edit: some extra code to be more explicit
$y = 0;
$numberOfRecords = count($res);
while($recordN < $numberOfRecords)
while($y<$res[$recordN]["ItemSlotY"]) {
$y++;
$x=0;
while($x<$res[$recordN]["ItemSlotX"]) {
$items = substr_replace($items, '1', ($i+$x)+(($y-1)*8), 1);
$x++;
}
}
$i++;
$recordN++;
}
as $temp is an array and you're then doing $res[$ijj]=$temp;