I have the following code:
$page=3;
$i=1;
while($i<=$pages) {
$urls .= "'"."http://twitter.com/favorites.xml?page=" . $i ."',";
$i++;
}
What I need to create is this array:
$data = array('http://twitter.com/favorites.xml?page=1','http://twitter.com/favorites.xml?page=2','http://twitter.com/favorites.xml?page=3');
How can I produce an array from the while loop?
$urls = array();
for ($x = 1; $x <= 3; $x++) {
$urls[] = "http://twitter.com/favorites.xml?page=$x";
}
. is for concatenating strings.
[] is for accessing arrays.
[] = pushes a value onto the end of an array (automatically creates a new element in the array and assigns to it).
You can do:
$page=3;
$i=1;
$data = array();
while($i <= $page) {
$data[] = "http://twitter.com/favorites.xml?page=" . $i++;
}
Try this instead:
$page=3;
$i=1;
$url=array();
while($i<=$pages) {
$urls[]="http://twitter.com/favorites.xml?page=".$i ;
$i++;
}
echo("<pre>".print_r($url,true)."</pre>");
Related
I have these for loop to determine consecutive number. What I achieve so far is to print the output in string.
$arr = [1,2,3,6,11,5,4,8,9,3];
for($start=0; $start<=count($arr); $start++){
for($end=$start+1; $end<=count($arr); $end++){
$total = $arr[$end] - $arr[$start];
if($total == 1){
echo 'Number is '.$arr[$start].','.$arr[$end].'<br/>';
} else {
echo '';
}
$arr[$start++];
}
}
My goal is to add the output into array.
I tried to use multidimensional array but no output display.
$arr = [1,2,3,6,11,5,4,8,9,3];
$arr3 = [];
for($start=0; $start<=count($arr); $start++){
for($end=$start+1; $end<=count($arr); $end++){
$total = $arr[$end] - $arr[$start];
if($total == 1){
$arr2 = array();
$arr2[] = $arr[$start].','.$arr[$end].'';
$arr3[] = $arr2;
} else {
}
$arr[$start++];
}
}
echo '<pre>';
print_r($arr3);
echo '</pre>';
exit;
Appreciate if someone can help me. Thanks.
you can simply use array functions, if sorting is important to you as #nice_dev said, you must sort your array before.
$arr = [1,2,3,6,11,5,4,8,9,3];
$cons = [] ;
while (array_key_last($arr) != key($arr)) {
if ((current($arr)+1) == next($arr)) {
prev($arr);
$cons[] = current($arr) . ',' . next($arr);
}
}
print_r($cons);
the output will be :
Array
(
[0] => 1,2
[1] => 2,3
[2] => 8,9
)
You can better sort() the input array first. This way, collecting all consecutive elements would get much simpler. If value at any index isn't +1 of the previous one, we add the $temp in our $results array and start a new $temp from this index.
Snippet:
<?php
$arr = [1,2,3,6,11,5,4,8,9,3];
$result = [];
sort($arr);
$temp = [];
for($i = 0; $i < count($arr); ++$i){
if($i > 0 && $arr[ $i ] !== $arr[$i - 1] + 1){
$result[] = implode(",", $temp);
$temp = [];
}
$temp[] = $arr[$i];
if($i === count($arr) - 1) $result[] = implode(",", $temp);
}
print_r($result);
Online Demo
How can I implement the code:
$numberList3 = array();
for($i = 0; $i < 10; $i++)
{
$numberList3[$i] = $i;
}
print_r($numberList3);
Using a foreach loop as the no. of times the loop is going to execute is decided by the user at run time.
Any suggestion.?
Use array_fill maybe?
<?php
$n = 10;
$arr = array_fill(0,$n,0);
foreach($arr as $k => $v) {
$arr[$k] = $k;
}
print_r($arr);
Or, as suggested by #deceze, use range
<?php
$n = 10;
$arr = array();
foreach(range(0,$n-1) as $v) {
$arr[$v] = $v;
}
print_r($arr);
Or when the value is the same as the key, you can use just this:
<?php
$n = 10;
$arr = range(0,$n-1);
// no foreach needed
print_r($arr);
foreach() works for object and array not for a single value.
What you can do create an array or object from users input.
like:
$userInput = 10;
$forEachArray = array_fill(0, $userInput, 0);
$arrayToDisplay = array();
foreach($forEachArray as $key){
$arrayToDisplay[$key] = $key;
}
print_r($arrayToDisplay);
I have an associative array in PHP like this:
$weight["a"]=1;
$weight["b"]=4;
$weight["c"]=5;
$weight["d"]=9;
Here I want to calculate pair-wise difference between consecutive array elements, e.g.,
"b-a" = 3
"c-b" = 1
"d-c" = 4
How should this be computed?
Try this:
$i = 0;
foreach ($weight AS $curr) {
if ($i > 0) {
echo '"'.array_keys($weight)[$i].'-'.array_keys($weight)[$i-1].'" = '.($curr-$prev)."<br />";
}
$i++;
$prev = $curr;
}
Store keys on a temporary array where keys are integers on which you can easily get the next key, and use it to parse your main array.
$tmp_array = array();
foreach ($weight as $key => $val) {
$tmp_array[] = $key;
}
$array_length = count($tmp_array);
for ($i = 0; i < array_length - 2; ++$i) {
echo $weight[$tmp_array[$i+1]], '-', $weight[$tmp_array[$i]], ' = ', ($weight[$tmp_array[$i+1]] - $weight[$tmp_array[$i]], PHP_EOL;
}
For given foreach loop output is display using echo
$output = array();
$counts = array('45','55','75','95');
foreach ($counts as $count) {
$output= $count + 10 ;
echo $output
}
it gives
output : 556585105
But instead of printing this I want to store $output as an array variable.
where I want to get $output = array('55','65', '85', '105'); So later I can grab any value from $output using key value.
You can modify original array like:
$counts = array('45','55','75','95');
foreach ($counts as &$count) {
$count += 10;
}
print_r($counts);
demo
You need to change the assignment. What you are doing is overwriting the $output. So $output contains the last assignment.
$output = array();
$counts = array('45','55','75','95');
foreach ($counts as $count) {
$output[] = $count + 10 ; // change this
}
You can do something like this :
$array = array();
$counts = array('45','55','75','95');
foreach ($counts as $count) {
$array[] = $count + 10 ;
}
print_r($array); // here is your array
Try this,
<?php
$output = array();
$counts = array('45','55','75','95');
foreach ($counts as $count) {
$output[] = $count + 10 ;
print_r($output);
}
?>
Your error was that you did $output = when you had to do $output[] = . Also remember you can't echo variables like that, but you can use print_r or var_dump.
You may use this array_push() function
<?php
$output = array();
$counts = array('45','55','75','95');
foreach ($counts as $count) {
array_push($output,$count + 10 );
}
print_r($output);
?>
array_push() function is a built-in function of php
I have some data, which i currently have hard coded, basically i am trying to split the numbers, 1-port and recreate it as
'INSERT INTO '.$tbl_name.'(PortNumber) VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(145),(146),(147),(148),(149),(150),(151),(152),(153),(154),(155),(156)';
and etc.. i have a pretty good start i think..
PHP
//post vars
$port = 24; //(int)$_POST['ports'];
$super = 2; //(int)$_POST['super'];
$col = 12; //(int)$_POST['columns'];
$row = 2; //(int)$_POST['rows'];
//rest of vars
$halfPort=$port/2;//12
$colHalf = $col / $super;//6
$half=$colHalf-1;//5
$count=1;
$and=1;
$one=1;
echo "<h1>here goes nothen</h1><br><br>";
//sperate port
$finalArray = array();
for ($i = $port; $i >= 1; $i -= $colHalf) {
$tempArray = array();
for ($j = 0; $j < $colHalf; $j++) {
$tempArray[] = $i - $j;
}
$tempArray[]= sort($tempArray);
$finalArray[] = implode(",", $tempArray);
}
$finalArray = array_reverse($finalArray);
echo "<pre>" . print_r($finalArray, true) . "</pre>";
echo "<br><br>";
//sql for insert
$sqlinsert='
INSERT INTO '.$tbl_name2.'
(PortNumber) VALUES ';
$start='(';
$end=')';
//preset b
$b=0;
for($c = $port; $c >= 1; $c -= $colHalf) {
$queryStart = array();
$queryStart[]=explode(',',$finalArray[$b]);
echo "<pre>" ."start". print_r($queryStart, true) . "</pre>";
for($s=0; $s<6; $s+=$and) {
$queryEnd = array();
$queryEnd[] = $start.$queryStart[$s].$end;
echo "<pre>" ."end". print_r($queryEnd, true) . "</pre>";
}
$b+=1;
}
to view it live insert it here: http://phptester.net/index.php?lang=en
baisaclly it gets to $queryEnd, everything goes down hill, any ideas?
I'm not quite sure what you want to do, but if you just want to build something like the INSERT query you can do something like:
$tbl_name = '?';
echo 'INSERT INTO '.$tbl_name.'(PortNumber) VALUES ('.implode('),(', array_merge(range(1,12),range(145,156))).')';
I think I see it. You are reinitializing your $queryEnd array every time the For loop runs. Move that out of the For loop.
Mike