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;
Related
I implemented the functionality get the alphanumberic value and plus one value.
$string = "VDLE009567";
$person = $numbers = array();
if(preg_match_all('/([a-z ]+[0-9]+)/i', $string, $mt)) {
$nrmt = count($mt[0]);
for($i=0; $i<$nrmt; $i++) {
if(preg_match('/([a-z ]+)([0-9]+)/i', $mt[0][$i], $mt2)) {
echo $person[$i] = trim($mt2[1]);
echo $numbers[$i] = $mt2[2];
}
}
}
echo $numbers=$numbers[0]+1;
i need to get the values as "VDLE009568" but i got the values as "VDLE9568". Is there is any possible way to get this "VDLE009568"
for your required condition i made some changes in the code
<?php
$string = "VDLE999999";
$person = $numbers = array();
if(preg_match_all('/([a-z ]+[0-9]+)/i', $string, $mt)) {
$nrmt = count($mt[0]);
for($i=0; $i<$nrmt; $i++) {
if(preg_match('/([a-z ]+)([0-9]+)/i', $mt[0][$i], $mt2)) {
$person[$i] = trim($mt2[1]);
$numbers[$i] = $mt2[2];
}
}
}
$num = (string)$numbers[0];
$word_length = strlen($num);
$max = 9;
$max_str = '';
for($i=0;$i<$word_length;$i++){
$max_str .=$max;
}
$max_str = (int)$max_str;
if($num < $max_str){
echo $numbers=str_pad($num+1, $word_length, '0', STR_PAD_LEFT);
}else{
$numbers=str_pad($num+1,$word_length+1, '0', STR_PAD_LEFT);
$new_str = strrev((string)$numbers);
$new_num = (int)$new_str;
echo $numbers = str_pad($new_num,$word_length+1, '0', STR_PAD_LEFT);
}
Use this code for result
<?php
$string = "VDLE999999";
$person = $numbers = array();
if (preg_match_all('/([a-z ]+[0-9]+)/i', $string, $mt)) {
$nrmt = count($mt[0]);
for ($i = 0; $i < $nrmt; $i++) {
if (preg_match('/([a-z ]+)([0-9]+)/i', $mt[0][$i], $mt2)) {
$personout = $person[$i] = trim($mt2[1]);
$numbers[$i] = $mt2[2];
if ($numbers[0] == 999999) {
$last = substr($personout, -1, 1);
$personout = substr($personout, 0, -1) . (++$last);
$numbers[0] = 0;
}
echo $personout;
}
}
}
echo $numbers = str_pad($numbers[0] + 1, 6, '0', STR_PAD_LEFT);
For result : VDLF000001 Online Test Click Here
I just tried out this code below and it doesn't seem to work. I corrected the bugs and it doesn't show nothing. It should show the standings array which I can then make it into a table. It's for Joomla MVC component that I'm working on. Can maybe somebody help me?
Thanks!
The code: EDIT:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('home' , 'scoreHome' , 'away' , 'scoreAway')));
$query->from($db->quoteName('futliga_pc_liga1'));
$db->setQuery($query);
$rows = $db->loadAssocList();
$standings = array ();
$standingTemplate = array ('matches' => 0, 'wins' => 0, 'draws' => 0, 'losses' => 0, 'scoreHome' => 0, 'scoreAway' => 0, 'goalsdiff' => 0, 'points' => 0);
foreach ($rows as $row) {
handleMatch($row['home'], $row['scoreHome'], $row['scoreAway']);
handleMatch($row['away'], $row['scoreAway'], $row['scoreHome']);
echo '<pre>';
print_r( usort($standings, 'comparePoints' ) );
}
function handleMatch($team, $scoreHome, $scoreAway){
global $standings, $standingTemplate;
if ($scoreHome > $scoreAway)
{
$points = 3;
$win = 1;
$draw = 0;
$loss = 0;
}
elseif ($scoreHome == $scoreAway)
{
$points = 1;
$win = 0;
$draw = 1;
$loss = 0;
}
else
{
$points = 0;
$win = 0;
$draw = 0;
$loss = 1;
}
if ( empty($standings[$team])){
$standing = $standingTemplate;
} else {
$standing = $standings[$team];
$standingTemplate['matches']++;
$standingTemplate['wins'] += $win;
$standingTemplate['draws'] += $draw;
$standingTemplate['losses'] += $loss;
$standingTemplate['scoreHome'] += $scoreHome;
$standingTemplate['scoreAway'] += $scoreAway;
$standingTemplate['goalsdiff'] += $scoreHome - $scoreAway;
$standingTemplate['points'] += $points;
}
$standings[$team] = $standing;
}
function comparePoints($a, $b){
if ($a['points'] == $b['points']) {
if ($a['goalsdiff'] == $b['goalsdiff']) return 0;
return ($a['goalsdiff'] < $b['goalsdiff']) ? 1 : -1 ;
}
return ($a['points'] < $b['points']) ? 1 : -1 ;
}
You are printing result of usort which is a boolean value. If you expect an array representation, then print the array itself:
usort($standings, 'comparePoints');
print_r($standings);
I tried this code to localhost and it worked fine but when I upload it in the server it doesn't work and it has the problem with this line:if($key[$j]== $this->test);. Can you help ? :)
<?php
class key {
public $test;
public $result;
public $imp;
public $sentc;
function __construct() {
$this->test = $_GET['c'];
$arr = explode("-", $this->test);
$p = PRODUCTS::getallteedrow();
$tproduct = PRODUCTS::getAlltaeed();
$ii = 0;
for ($i = 0; $i < $p; $i++) {
$pp = $tproduct[$i];
$key = $pp['keyword'];
$key = explode(',', $key);
$count = count($key);
for ($j = 0; $j < $count; $j++) {
if ($key[$j] == $this->test) {
$this->result[$ii] = PRODUCTS::products_SelectRow($pp['id']);
$this->sentc = $this->result[$ii]['description'];
$this->sentc = explode(" ", $this->sentc);
if (count($this->sentc) >= 30) {
$this->sentc = array_slice($this->sentc, 0, 30);
$this->sentc = implode(" ", $this->sentc);
$this->result[$ii]['description'] = $this->sentc . ".....";
}
echo $this->result[$ii]['description'];
$ii++;
}
}
}
}
}
?>
It is PHP code and I work in PHP MVC.
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 a problem with an advanced loop, this are my arrays
$array1 = array(1,2,3);
$array2 = array(4,5);
$array3 = array(6,7,8,9);
$array4 = array(10,11);
I want to loop with the following result:
1,4,6,10
1,4,6,11
1,4,7,10
1,5,7,11
With the last: 3,5,8,11
How can I do that?
Solution for a variable number of arrays. Can probably be written a bit shorter, but I leave that to you. ;)
<?php
$array1 = array(1,2,3);
$array2 = array(4,5);
$array3 = array(6,7,8,9);
$array4 = array(10,11);
function turboArray()
{
$arrays = func_get_args();
$indexes = array_fill(0, count($arrays), 0);
function turboSubArray(&$arrays, &$indexes, $l){
for ($a = 0; $a < count($arrays[$l]); $a++){
$indexes[$l] = $a;
if ($l == count($arrays) - 1) {
for ($i = 0; $i < count($indexes); $i++)
{
echo $arrays[$i][$indexes[$i]];
if ($i == count($indexes) - 1){
echo "\n<br/>";
$indexes[$i] = 0;
if ($i == 0) return;
}
else {
echo ', ';
}
}
} else if ($l < count($indexes)) {
turboSubArray($arrays, $indexes, $l + 1);
}
}
}
$l = 0;
turboSubArray($arrays, $indexes, $l);
}
turboArray($array1, $array2, $array3, $array4);
This should do it... ?
foreach ($array1 as $first) {
foreach ($array2 as $second) {
foreach ($array3 as $third) {
foreach ($array4 as $fourth) {
echo "$first $second $third $fourth";
}
}
}
}
I got bored and decided to overengineer a bit. Enjoy.
class CrossJoin implements Iterator {
private $arrays = array();
private $index = 0;
public function __construct(/* $array1, ... */) {
foreach (func_get_args() as $arr) {
// ArrayIterator's iteration stuff is cleaner than arrays',
// and less prone to breakage when objects are passed around
if (count($arr)) $this->arrays[] = new ArrayIterator($arr);
}
// if any of the arrays were empty, a cross join should fail.
// fudge a single empty array to prevent special-casing later
if (count($this->arrays) !== func_num_args()) {
$this->arrays = array(new ArrayIterator(array()));
}
}
public function next() {
for ($i = count($this->arrays) - 1; $i >= 0; --$i) {
// carry til we don't have to anymore
$place = $this->arrays[$i];
$place->next();
if ($place->valid()) break;
}
// if $i<0, then we carried right off the edge of the list.
// don't let $arrays[0] be reset, cause that's our indicator
// that we're done.
if ($i<0) return;
++$this->index;
for (++$i; $i < count($this->arrays); ++$i) {
$this->arrays[$i]->rewind();
}
}
public function current() {
$result = array();
foreach ($this->arrays as $arr) { $result[] = $arr->current(); }
return $result;
}
public function key() { return $this->index; }
public function valid() {
return !empty($this->arrays) && $this->arrays[0]->valid();
}
public function rewind() {
foreach ($this->arrays as $arr) $arr->rewind();
$this->index = 0;
}
}
$combos = new CrossJoin(
array(1, 2, 3),
array(4, 5),
array(6, 7, 8, 9),
array(10, 11)
);
foreach ($combos as $combo) {
echo implode(', ', $combo), "\n";
}
Hope that helps:
for ($i = 0; $i < count($a1); ++i) {
for ($j = 0; $j < count($a2); ++j) {
for ($k = 0; $k < count($a3); ++k) {
for ($l = 0; $l < count($a4); ++l) {
echo $a1[$i] . ' ' . $a2[$j] . ' ' . $a3[$k] . ' ' . $a4[$l] . '\n';
}
}
}
}
Cheers!
You are looking for the PHP array_merge function.