Implement two-dimensional array [closed] - php

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I need to implement two-dimensional array in PHP. Is it a correct way to do this?
$constr = array();
for ($i = 0; $i < $size; $i++)
{
for ($j=0; $j < $ncons; $j++) {
$constr[$i][$j] = $set->getInd($i)->getConstr($j);
}
}

The code as you have it is fine, but since you're using objects, it's best to cache them inside the outer loop:
$constr = array();
for ($i = 0; $i < $size; $i++) {
$ind = $set->getInd($i);
for ($j=0; $j < $ncons; $j++) {
$constr[$i][$j] = $ind->getConstr($j);
}
}
In this way, you're not repeating $set->getInd($i) for the inner loop.

Related

Having Issue On PHP Increment [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Can you please take a look at this demo and let me know why I am not able to increment the $start as the output is looks like 88888 only
<?php
for($i = 1; $i<= 5; $i++){
$start = 8;
echo $start;
$start++;
}
?>
That is because you always assign your value inside loop , take it out
<?php
$start = 8;
for($i = 1; $i<= 5; $i++){
echo $start;
$start++;
}
?>
If you want to increment the value of start, you must declare it outside of the for loop.
<?php
$start = 8;
for($i = 1; $i<= 5; $i++){
echo $start;
$start++;
}
?>

Nested for loop exercise [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can I solve this using php?
Write nested for loops to produce the following output:
Enter a number:5
----1
---22
--333
-4444
55555
Here you go:
for ($i = 1; $i <= 1; $i++) {
for ($j = 1; $j <= 1; $j++) {
echo 'Enter a number:5 ----1 ---22 --333 -4444 55555';
}
}
Edit: Here is the real answer:
$number = 5;
for ($i = 1; $i <= $number; $i++) {
for ($j = $number; $j >= 1; $j--) {
if ($i < $j) {
echo '-';
} else {
echo $i;
}
}
echo PHP_EOL;
}
This code uses two for loops. The outer loop creates the rows (iterates from 1 to $number), the inner loop creates the characters (iterates from $number to 1). There is a comparison inside the inner loop. If $i is less than $j, then it prints -, otherwise it prints $i.

For loop inside of an Array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The code below works, but I need to run a loop for the results. For example where it says $results0 I need it to say $results[$var].
for($i = 0; $i < (count($results0)); $i++) {
$teams[$i] = array(
$results0[$i],
$results1[$i],
$results2[$i],
$results3[$i],
$results4[$i],
);
}
$teams1 = array_sort($teams, $sortVar, SORT_ASC);
I realize you cannot do this but I need something that looks like this but actually works:
for($i = 0; $i < (count($results0)); $i++) {
$teams[$i] = array(
for($j = 0; $j < (count($teams)); $j++) {
${'results'.$j}[$i],
);
}
}
$teams1 = array_sort($teams, $sortVar, SORT_ASC);
Also now I need to be able to sort by category. Thanks in advance, I'm aware my current code may not be secure, I'm doing that after I'm finished.
You might simply use array_map():
$results1 = array(1, 2, 3);
$results2 = array('a', 'b', 'c');
$results3 = array('I', 'II', 'III');
$zipped = array_map(function($r1, $r2, $r3) {
return array($r1, $r2, $r3);
}, $results1, $results2, $results3);
var_dump($zipped);
We unlikely are able to provide an answer to the question about sorting since we have no idea what is "category" in your code. It doesn't appear anywhere. But give usort() a try.

PHP: for VS. foreach? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Kindly someone explain to me about this?
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a";
is faster than
foreach($aHash as $key=>$val) $aHash[$key] .= "a";
According to The PHP Benchmark. However I have a a code in my script:
My CODE:
foreach($_SESSION['undo'] as $key2=>$value2)
{
if{
}
else
{
}
.
.
.
.
}
How can I convert a code like that as shown above to my code?
Kindly explain why? Thank you.
do not count in for condition
you can try this
$size = count($_SESSION['undo']);
for($i = 0; $i< $size; $i++){
$value = $_SESSION['undo'][$i];
}
In a foreach loop the first part is your array and the second part after the as is the current value when iterating.
When using a for loop you are working with indexes and have to manually access them. Just do the same as in your example. I'm assuming you are working with an associative array as you are using array keys.
$myArray = $_SESSION['undo'];
$keys = array_keys($myArray);
$size = sizeOf($keys);
for ($i = 0; $i < $size; $i ++) {
/* do something with $myArray[$keys[$i]] */
echo $myArray[$keys[$i]];
}
try this:
for($i = 0; $i< count($_SESSION['undo']); $i++){
$value = $_SESSION['undo'][$i];
}
Seems you made a mistake, from the http://www.phpbench.com/,
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a";
This way cost 92 us, just foreach cost 16 us, with 'as' cost 21 us.
Guys, wake up....

How to increment a for loop by more than 1? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do I print a series like this?
6,15,24,33,42.....1000
I have tried using a for loop like this, but can't work out how to get it to increment by 9 on each iteration.
for($i = 6; $i <= 1000; $i = 9)
{
echo $i . ', ';
}
Quite simple really:-
for($i = 6; $i <= 1000; $i += 9){
echo $i . ', ';
}
//If you have to finish the series with 1000, although it is not a part of the series:-
echo '1000';
See it working
just for fun:
echo implode(', ', range(6, 1000, 9));
echo ", 1000";
<?php
for($i=6; $i<1000; $i+=9)
echo $i."<br>";
?>
Seeing the question this can be an answer.

Categories