Issue splitting an array for use in another array - 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

Related

get a set of values from an array

i have a set of arrays:
$nums = array(2,3,1);
$data = array(11,22,33,44,55,66);
what i want to do is to get a set of $data array from each number of $nums array,
the output must be:
output:
2=11,22
3=33,44,55
1=66
what i tried so far is to slice the array and remove the sliced values from an array but i didn't get the correct output.
for ($i=0; $i < count($nums); $i++) {
$a = array_slice($data,0,$nums[$i]);
for ($x=0; $x < $nums[$i]; $x++) {
unset($data[0]);
}
}
Another alternative is to use another flavor array_splice, it basically takes the array based on the offset that you inputted. It already takes care of the unsetting part since it already removes the portion that you selected.
$out = array();
foreach ($nums as $n) {
$remove = array_splice($data, 0, $n);
$out[] = $remove;
echo $n . '=' . implode(',', $remove), "\n";
}
// since nums has 2, 3, 1, what it does is, each iteration, take 2, take 3, take 1
Sample Output
Also you could do an alternative and have no function usage at all. You'd need another loop though, just save / record the last index so that you know where to start the next num extraction:
$last = 0; // recorder
$cnt = count($data);
for ($i = 0; $i < count($nums); $i++) {
$n = $nums[$i];
echo $n . '=';
for ($h = 0; $h < $n; $h++) {
echo $data[$last] . ', ';
$last++;
}
echo "\n";
}
You can array_shift to remove the first element.
$nums = array(2,3,1);
$data = array(11,22,33,44,55,66);
foreach( $nums as $num ){
$t = array();
for ( $x = $num; $x>0; $x-- ) $t[] = array_shift($data);
echo $num . " = " . implode(",",$t) . "<br />";
}
This will result to:
2 = 11,22
3 = 33,44,55
1 = 66
This is the easiest and the simplest way,
<?php
$nums = array(2,3,1);
$data = array(11,22,33,44,55,66);
$startingPoint = 0;
echo "output:"."\n";
foreach($nums as $num){
$sliced_array = array_slice($data, $startingPoint, $num);
$startingPoint = $num;
echo $num."=".implode(",", $sliced_array)."\n";
}
?>

$_GET form variables in a loop

I've got a form with input names like price1, price2, price3 and I'm trying to get these values in another page. I'm sending it using GET.
for ($i = 1; $i < $qtd_itens; $i++) {
$price = $_GET['price" + $i + "'];
echo $price;
}
How should I declare the $price variable?
Need to put . in place of + sign...
for ($i = 1; $i < $qtd_itens; $i++) {
$price = $_GET['price'. $i. ''];
echo $price;
}
If you just want to collect all the prices you can do:
$prices = array();
for ($i = 1; $i < $qtd_itens; $i++) {
$prices[] = $_GET["price" . $i];
}
var_dump($prices);
To clarify:
$arr[] = "a";
is shorthand for:
array_push($arr, "a");
or
$arr[$i] = "a";
If you form input having price1, price2, price3... etc, you simply make this for view content of data send:
print_r($_REQUEST)
This show you data in array send, and collect with foreach with this:
$prices = array();
foreach ($_REQUEST as $key=>$val) {
$prices[$key] = $val;
}
print_r($prices);

How to output arrays in a certain way using PHP?

I'm try to print an array where every other value is reassigned, as examples (from this):
17.34502870451717,62.46137370987033
To this:
62.46137370987033,17.34502870451717
That part have I succeeded with, but now I have this structure:
[62.46137370987033,[17.34501402936927,]
[62.46123453616544,[17.34525377433593,]
[62.4610178881864,[17.34546663705899,]
This is where I get stuck and do not know how to write.
The structure I want looks like this:
[62.392628, 17.309413],
[62.393162, 17.309193],
[62.393403, 17.30922]
Here is my explode.php (GIST)
<?php
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$wing = "[";
for ($i = 0;$i < count($minion) -1; $i++) {
echo $wing . $minion[$i+1].",";
if($i%2==1) { echo "]<br />"; }
} echo $minion[0] . $wing;
?>
As I understand it, as long as there's always even pairs it should be as easy as;
<?php
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$eol = '';
for ($i = 0;$i < count($minion) -1; $i+=2) {
echo $eol.'['.$minion[$i+1].','.$minion[$i]."]";
$eol=',<br/>';
}
echo '<br/>';
>>> [62.46137370987033,17.34502870451717],
>>> [62.46123453616544,17.34501402936927]
Try This
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$wing = "[";
for ($i = 0;$i < count($minion) -1; $i+=2)
{
echo $kk = $wing . $minion[$i+1].",".$minion[$i]."],<br>";
}
Just a small modification to the given answers
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$str = '';
for ($i = 0;$i < count($minion) -1; $i+=2) {
$str.='['.$minion[$i+1].','.$minion[$i].'],<br/>';
}
echo rtrim($str,','); // to trim ',' at the end

How do I simplify this basic loop?

I have code which pretty much does this.....
//get the row info
$Row1 = $FullTable->find('div[class=ismPitchRow1]',0);
$Row2 = $FullTable->find('div[class=ismPitchRow2]',0);
$Row3 = $FullTable->find('div[class=ismPitchRow3]',0);
$Row4 = $FullTable->find('div[class=ismPitchRow4]',0);
$Row5 = $FullTable->find('div[class=ismPitchRow5]',0);
//Loop 5 times. One for each row on the pitch.
for ($i=1; $i<=5; $i++)
{
if ($i = 1) { echo $Row1; }
if ($i = 2) { echo $Row2; }
if ($i = 3) { echo $Row3; }
if ($i = 4) { echo $Row4; }
if ($i = 5) { echo $Row5; }
}
It works, but as you can see it's not very efficient and badly designed. How would I simplify this? I know there are much smaller ways that these kind of loops can be done.
Thanks.
use the great invention of arrays:
//get the row info
$Row[1] = $FullTable->find('div[class=ismPitchRow1]',0);
$Row[2] = $FullTable->find('div[class=ismPitchRow2]',0);
$Row[3] = $FullTable->find('div[class=ismPitchRow3]',0);
or, even more clever...
for ($i = 1; $i <= 5; $i++) {
$find = "div[class=ismPitchRow$i]";
$Row[$i] = $FullTable->find($find,0);
}
do the same for echoing:
for ($i = 1; $i <= 5; $i++) {
echo $Row[$i];
}
but why not do everything in 1 loop?
for ($i = 1; $i <= 5; $i++) {
$find = "div[class=ismPitchRow$i]";
echo $FullTable->find($find,0);
}
I would store $Row1 through $Row5 in an array and iterate through it with a foreach loop.
<?php
$YourArray = array();
array_push($YourArray,$FullTable->find('div[class=ismPitchRow1]',0),$FullTable->find('div[class=ismPitchRow2]',0),$FullTable->find('div[class=ismPitchRow3]',0),$FullTable->find('div[class=ismPitchRow4]',0),$FullTable->find('div[class=ismPitchRow5]',0));
foreach($YourArray as $row){
echo $row;
}
?>

How can I store for loop in a variable?

How can I store this in a $_var variable ?
$s_number = 5;
$spn = '6';
echo "'Landscapes':[";
for ($i = 1; $i <= $s_number; $i++)
{
echo "'".$spn."/"."content"."/".$i.".png"."'".", ";
}
echo "]";
Question is bit vague though it seems probably you are looking for this,
$string = "'Landscapes':[";
for ($i = 1; $i <= $s_number; $i++) {
$string .= "'".$spn."/"."content"."/".$i.".png"."'".", ";
}
$string .= "]";
echo $string;
Other suggestions that use strings are fine, but I prefer to create arrays for tasks like this:
$s_number = 5;
$spn = '6';
$landscapes_array = array();
for ($i = 1; $i <= $s_number; $i++) {
$landscapes_array[] = "'".$spn."/"."content"."/".$i.".png"."'".", ";
}
$landscapes = "'Landscapes':[" . implode('', $landscapes_array) . "]";
You could also try putting the for loop in a function passing your variables and then return the values to a new variable. That's like saving a loop in a variable.

Categories