$cInc = json_decode($inc);
$c = count((array)$cInc);
for ($x = 0; $x < $c; $x++)
{
$section->addListItem($cInc[$x]);
}
So I want to loop the array $cInc to a List Item and somehow the loop corrupts the document.
i think you are wrong using count
The count() function returns the number of elements in an array
so you dont need array inside count
just
$cInc = json_decode($inc);
$c = count($cInc);
for ($x = 0; $x < $c; $x++)
{
$section->addListItem($cInc[$x]);
}
or u can use foreach if you dont know how many array that you have
$cInc = json_decode($inc);
foreach ($cInc as $val)
{
$section->addListItem($val);
}
Related
Hello I would like to know if there is away and if this is valid programming to increment the value of array inside echo. Example code is:
for ($x = 1; $x <= $number; $x++) {
echo"<td>".(round(($row['day_1'])/3600))."</td>";
}
Where I would like to display $row['day_1'], $row['day_2'], $row['day_3'] etc. if there is no way this to be achieved, is there a way to increment the same predefined array with the results of $row like
$time_01[0] = $row['day_1'];
$time_01[1] = $row['day_2'];
So after that to loop through the time_01[] array ?
Least amount of changes: Use the $x you already have:
for ($x = 1; $x <= $number; $x++) {
echo"<td>".(round(($row["day_$x"])/3600))."</td>";
}
or
for ($x = 1; $x <= $number; $x++) {
echo"<td>".(round(($row['day_' . $x])/3600))."</td>";
}
or if you want to put it in another array first, for whatever reason:
for ($x = 1; $x <= $number; $x++) {
$time_01[] = $row['day_' . $x];
}
This really is basic stuff by the way. I do recommend that you follow some basic tutorials about variables, expressions, string concatenation and more.
I am trying to make card game.
I have 6 variable that are stored in array. Than I use fisherYates method to randomize array, and display four of them.
Problem is, when I randomize it this way only, it will give only random output of those six, with all different types.
So I want that some repeats like, if you draw four cards, you get output of
ex: club, club, diamond,heart, or heart, star,star,heart.. if you get a point..
I thought to do it like this way: put the array in loop of 4 times, and every time it loops, it stores first, or last value in new array, so that way, I can have greater chances of combination of same cards in output array.
But I'm stuck, and I don't know how to do it :/
this is what I've tried so far
$diamond = 'cube.jpg';
$heart = 'heart.jpg';
$spade = 'spade.jpg';
$club = 'tref.jpg';
$star='star.jpg';
$qmark='qmark.jpg';
$time=microtime(35);
$arr=[$diamond,$heart,$spade,$club,$star,$qmark];
function fisherYatesShuffle(&$items, $time)
{
for ($i = count($items) - 1; $i > 0; $i--)
{
$j = #mt_rand(0, $i);
$tmp = $items[$i];
$items[$i] = $items[$j];
$items[$j] = $tmp;
}
return $items;
}
$i=0;
do {
$niz[$i]=fisherYatesShuffle($arr,$time);
reset($niz);
$i++;
} while ($i <= 3);
Got a solution. Was just to simply do foreach of first element of multidimensional array :)
Code goes like this:
$diamond = 'cube.jpg';
$heart = 'heart.jpg';
$spade = 'spade.jpg';
$club = 'tref.jpg';
$star='star.jpg';
$qmark='qmark.jpg';
$time=microtime(35);
$arr=[$diamond,$heart,$spade,$club,$star,$qmark];
$niz=array();
$i=0;
do {
$niz[$i]=fisherYatesShuffle($arr,$time);
//reset($niz);
$i++;
} while ($i <= 3);
foreach ($niz as $key ) {
$randomArr[]=$key[0]; ;
}
function fisherYatesShuffle(&$items, $time)
{
for ($i = count($items) - 1; $i > 0; $i--)
{
$j = #mt_rand(0, $i);
$tmp = $items[$i];
$items[$i] = $items[$j];
$items[$j] = $tmp;
}
return $items;
}
print_r($randomArr);
I've got this code
$vote = array(
$arrayName[0][firstsector],
$termin[1][firstsector],
$termin[2][firstsector],
$termin[3][firstsector]
);
Now I want to create a loop for it. I tried this:
$howMuchIneed = 5;
for ($x = 0; $x <= $howMuchIneed; $x++) {
$vote = array(
$arrayName[$x][firstsector]
);
}
But the result doesn't look the same as the first code.
Have you tried this ?
for ($x = 0; $x <= $howMuchIneed; $x++) {
array_push($vote, $arrayName[$x][firstsector]);
}
initializes Array
$vote = array();
If you want to learn more about array_push http://php.net/manual/en/function.array-push.php
Try this (in your for loop):
$vote[] = $arrayName[$x]['firstsector'];
... and don't forget to declare your array before your loop!
$vote = array();
And in your first example you have 4 elements, condition in your for should be $x < 4 - arrays in PHP are zero-based.
I am reading a bunch of excel files and inserting their data inside database. The queries are all working correctly. Only problem is if an author is present in first excel file its entry is also showing in other excel file also. This is my code.
$array = array(
1=>"abc",
2=>"def",
3=>"age");
foreach ($array as $key=>$val) {
$file = $array[$key].'.xls';
$data->read($file);
$ID = $key;
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
$a = addslashes($data->sheets[0]['cells'][$i][1]);
if($a == "Ali")
{
echo $a."=>".$ID." ".$i."<br>";
}
}
}
The desired output is
abc.xls
Ali=>1 282
def.xls
age.xls
and the output coming is
abc.xls
Ali=>1 282
def.xls
Ali=>2 282
age.xls
Ali=>3 282
Can anybody tell me where is mistake in this code. Any help will be appreciate..
NOTE The number of rows present in excel sheet are 100.
It seems to me like the issue is whatever is column 1 of that specific row is always "Ali". Thats a guess though because your output is as expected with the exception of the two extra Ali=>1 282. It would help if you could give us the first few rows just to understand. Otherwise this really seems to be the issue.
I haven't included this lines inside foreach loop , because of that $data is always taking 1st excel file. Not taking further files. This lines are included before foreach loop.
The lines are
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('utf-8');
Now code becomes
foreach ($array as $key=>$val) {
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('utf-8');
$file = $array[$key].'.xls';
$data->read($file);
$ID = $key;
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
$a = addslashes($data->sheets[0]['cells'][$i][1]);
if($a == "Ali")
{
echo $a."=>".$ID." ".$i."<br>";
}
}
}
Use unset() to unset(destroy) the current value of $a at end of each loop iteration.
The required change is as follows:
Change the following:
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
$a = addslashes($data->sheets[0]['cells'][$i][1]);
if($a == "Ali")
{
echo $a."=>".$ID." ".$i."<br>";
}
}
to this:
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
$a = addslashes($data->sheets[0]['cells'][$i][1]);
if($a == "Ali")
{
echo $a."=>".$ID." ".$i."<br>";
}
unset($a);
}
How would I alter certain elements in the inner for loop if I am using an array with numbers?
e.g
$decryptFields[0] = '1';
$decryptFields[1] = '3';
if($z) == ANY OF THOSE NUMBERS IN THE ARRAY DO SOMETHING.
$x[$i][$z]
so if the inner for loop contains any of those numerals then something will happen e.g maybe I'll make the text bold.
foreach($decryptFields as $dfield) {
echo $dfield;
}
for($i = 0; $i< 10; $i++) {
for($z = 0; $z < $columnLength; $z++) {
echo $x[$i][$z];
}
}
}
Your question is not very clear, but I will do my best to answer it.
If you want to 'do something' if the value $z equals any of the values in the array $decryptFields, you can simply use:
if(in_array($z, $decryptFields)){ /*do something*/}
EDIT: It seems $z is also an array of values.
In that case, use :
$intersection = array_intersect($z, $decryptFields);
foreach($intersection as $key=>$value){
echo "<b>$value</b>";
}
Maybe something like...
for ($i = 0; $i < 10; $i++) {
// Build a string
$str = '';
for ($z = 0; $z < $columnLength; $z++) {
$str .= $x[$i][$z];
}
// If any of the elements of $decryptFields are present in the string, wrap
// it in <span class='bold'></span>
foreach ($decryptFields as $dfield) {
if (strpos($str, $dfield) !== FALSE) {
$str = "<span class='bold'>$str</span>";
break;
}
}
// Echo the result
echo $str;
}
If you need to check multiple items for match to any item of some array, best solution is to build index for the array:
foreach ($decryptFields as $key => $value) $decryptIndex[$value] = $key;
And use that index later:
if (isset($decryptIndex[$x[$i][$z]])) {
// Do something
}
And if you need to get index of matching element in $decryptFields array, use $decryptIndex[$x[$i][$z]]
This is the fastest method, since associative array implementation in PHP is very fast.