For Loop print results line by line - php

Hi i have following php codes(part of my full code):
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['serisname1'] = $new_instance['serisname1'];
$instance['serisname2'] = $new_instance['serisname2'];
$instance['serisname3'] = $new_instance['serisname3'];
$instance['serisname4'] = $new_instance['serisname4'];
$instance['serisname5'] = $new_instance['serisname5'];
$instance['serisname6'] = $new_instance['serisname6'];
$instance['serisname7'] = $new_instance['serisname7'];
$instance['serisname8'] = $new_instance['serisname8'];
$instance['serisname9'] = $new_instance['serisname9'];
$instance['serisname10'] = $new_instance['serisname10'];
$instance['serisname11'] = $new_instance['serisname11'];
$instance['serisname12'] = $new_instance['serisname12'];
$instance['serisname13'] = $new_instance['serisname13'];
$instance['serisname14'] = $new_instance['serisname14'];
$instance['serisname15'] = $new_instance['serisname15'];
return $instance;
for($x = 1; $x <= 15; $x++)
$serisname = $instance[serisname.$x];
$items[] = $serisname;
print_r($items);
my export :
array ( [0] => The Flash )
i want its be like :
array ( [0] => The Flash [1] => Arrow [2] => Game Of Throne and etc...)
the problem is its only echo last result but i want its echo every 15 results line by line.

for($x = 1; $x <= 15; $x++){
$serisname = $instance['serisname'.$x];
$items[] = $serisname;
}
print_r($items);

If I understead your question correctly, you can try this:
$count = 0;
for($x = 0; $x <= count($instance); $x++) {
$items[$x][] = $instance[serisname.$x];
if($count == 15) {
$count = 0;
}
}
Maybe can include the instance before the loop in your question so we can replicate the array?

Related

Print multidimensional array index in php

hello can you help me to find the way to print the index of a multidimensional array
My script
<Php?
$a= array();
$a[0][0][0] = 0;
$a[0][0][1] = 1;
$a[0][1][0] = 2;
$a[0][1][1] = 3;
$a[1][0][0] = 4;
$a[1][0][1] = 5;
$a[1][1][0] = 6;
foreach( $a as $v){
foreach( $v as $v1 ){
foreach ($v1 as $v2=>$value){
echo"[][][$v2] = $value\n";
}
}
}
?>
//Result//
[][][0] = 0
[][][1] = 1
[][][0] = 2
[][][1] = 3
[][][0] = 4
[][][1] = 5
[][][0] = 6
I have a feeling you might want to print like this.
$a = [];
$a[0][0][0] = 0;
$a[0][0][1] = 1;
$a[0][1][0] = 2;
$a[0][1][1] = 3;
$a[1][0][0] = 4;
$a[1][0][1] = 5;
$a[1][1][0] = 6;
foreach( $a as $i => $v){
foreach( $v as $i1 => $v1 ){
foreach ($v1 as $i2=>$value){
echo"[$i][$i1][$i2] = $value\n";
}
}
}
RESULT
[0][0][0] = 0
[0][0][1] = 1
[0][1][0] = 2
[0][1][1] = 3
[1][0][0] = 4
[1][0][1] = 5
[1][1][0] = 6

Index in for loop not returning last index

I have an image gallery and each is saved by index.. gallery1: /image here.. gallery2: / image here.. etc..
I'm using an index with multiple for loops to return the images and by column because it's either Masonry or rectangular. I am getting the return fine except for the very last index.
private function rectangle($items, $columns, $contents = array()) {
$thumbs = array('talent_thumbnail','360x207');
$arr = array();
$col = round(count($items) / $columns);
$perCol = floor(count($items) / $columns);
$extra = count($items) % $columns;
$ind = 1;
$length = count($items);
$arr = array();
for($i = 0; $i < $columns && $ind < $length; $i++) {
$temp = array();
for($j = 0; $j < $perCol; $j++) {
$obj = new JObject();
$obj->image = $items['gallery' . $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
}
if ($extra > 0) {
$obj = new JObject();
$obj->image = $items['gallery'. $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
$extra--;
}
$arr[] = $temp;
}
}
I know it can't be that hard but I'm not that good at it right yet.
Any help is so much welcome.
Thank You.
You set the variable $ind to 1, count the length of the array, and then initialize the for loop with the condition $ind < $length.
When $ind reaches the index needed to access the last item, the loop is not run again, since $ind is now equal to $length, not smaller.
You can fix this by changing the condition in your for-loop to "less or equal":
$i < $columns && $ind <= $length
This runs the loop once more when $ind has reached the last index.
Probably the problem is at this line:
$ind = 1;
In PHP non-associative arrays have the indexes starting to 0 instead of 1:
$arr = array('a', 'b', 'c');
print_r($arr);
// output:
Array ( [0] => a [1] => b [2] => c )
So, try to change that line in your code to:
$ind = 0;
Complete code:
private function rectangle($items, $columns, $contents = array()) {
$thumbs = array('talent_thumbnail','360x207');
$arr = array();
$col = round(count($items) / $columns);
$perCol = floor(count($items) / $columns);
$extra = count($items) % $columns;
$ind = 0;
$length = count($items);
$arr = array();
for($i = 0; $i < $columns && $ind < $length; $i++) {
$temp = array();
for($j = 0; $j < $perCol; $j++) {
$obj = new JObject();
$obj->image = $items['gallery' . $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
}
if ($extra > 0) {
$obj = new JObject();
$obj->image = $items['gallery'. $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
$extra--;
}
$arr[] = $temp;
}
}

Get the last for loop variable value

I want to display the last modified variable outside the for loop
Code:
for( $i = 1; $i <= 100; $i++ )
{
$pm_discussion = $_POST['pm_discussion'.$i];
$pm_update = $_POST['pm_update'.$i];
$pm_reports = $_POST['pm_reports'.$i];
$pm_informed = $_POST['pm_informed'.$i];
$pm_complete = $_POST['pm_complete'.$i];
}
**echo $pm_discussion;**
For example:
$i inside the for loop having values from 1 to 6.
Then it should display $pm_discussion = $_POST['pm_discussion'.$i];
The above $i should be 6.
Try following code.
$temp='';
for( $i = 1; $i <= 100; $i++ )
{
$pm_discussion = $_POST['pm_discussion'.$i];
$pm_update = $_POST['pm_update'.$i];
$pm_reports = $_POST['pm_reports'.$i];
$pm_informed = $_POST['pm_informed'.$i];
$pm_complete = $_POST['pm_complete'.$i];
if($pm_discussion!='')
$temp = $pm_discussion;
}
echo $temp;
Try this, it will alsays store the last non empty value to the variable -
for( $i = 1; $i <= 100; $i++ )
{
if (!empty($_POST['pm_discussion'.$i])) {
$pm_discussion = $_POST['pm_discussion'.$i];
}
if (!empty($_POST['pm_update'.$i])) {
$pm_update = $_POST['pm_update'.$i];
}
if (!empty($_POST['pm_reports'.$i])) {
$pm_reports = $_POST['pm_reports'.$i];
}
if (!empty($_POST['pm_informed'.$i])) {
$pm_informed = $_POST['pm_informed'.$i];
}
if (!empty($_POST['pm_complete'.$i])) {
$pm_complete = $_POST['pm_complete'.$i];
}
}
echo $pm_discussion;

How to format an associative array with another structure?

I have a query like this:
select truck, oil_type, km_min, km_max from trucks;
I'm using codeigniter and with the $query->result_array() function so its loaded in an associative array with this structure:
/*
array(
truck
oil_type
km_min
km_max
)
*/
$arr[0]["truck"] = 2;
$arr[0]["oil_type"] = 2;
$arr[0]["km_min"] = 345;
$arr[0]["km_max"] = 567;
$arr[1]["truck"] = 2;
$arr[1]["oil_type"] = 4;
$arr[1]["km_min"] = 234;
$arr[1]["km_max"] = 867;
$arr[2]["truck"] = 1;
$arr[2]["oil_type"] = 2;
$arr[2]["km_min"] = 545;
$arr[2]["km_max"] = 867;
$arr[3]["truck"] = 4;
$arr[3]["oil_type"] = 3;
$arr[3]["km_min"] = 45;
$arr[3]["km_max"] = 567;
Then, I'm trying to restructure it grouping the trucks by the id, something like this:
/*
trucks - array(
truck
truck_data - array(
oil_type
km_min
km_max
)
)
*/
$arr["truck"][0]= 2;
$arr["truck"][0]["truck_data"][0]["oil_type"] = 2;
$arr["truck"][0]["truck_data"][0]["km_min"] = 345;
$arr["truck"][0]["truck_data"][0]["km_max"] = 567;
$arr["truck"][0]["truck_data"][1]["oil_type"] = 4;
$arr["truck"][0]["truck_data"][1]["km_min"] = 234;
$arr["truck"][0]["truck_data"][1]["km_max"] = 867;
$arr["truck"][1]= 1;
$arr["truck"][1]["truck_data"][0]["oil_type"] = 2;
$arr["truck"][1]["truck_data"][0]["km_min"] = 545;
$arr["truck"][1]["truck_data"][0]["km_max"] = 867;
$arr["truck"][2]= 4;
$arr["truck"][2]["truck_data"][0]["oil_type"] = 3;
$arr["truck"][2]["truck_data"][0]["km_min"] = 45;
$arr["truck"][2]["truck_data"][0]["km_max"] = 567;
I thought in something like the code below:
$res = $query->result_array();
$cnt_total = count($res);
$y = 0;
for ($x=0; $x < $cnt_total -1 ; $x++) {
$truck = $res[$x]["truck"];
$trucks["truck"][$y] = $truck;
$q = $x + 1;
$i = 0;
do{
$trucks[$y][$i]["oil_type"] = $res[$q]["oil_type"];
$trucks[$y][$i]["km_min"] = $res[$q]["km_max"];
$trucks[$y][$i]["km_max"] = $res[$q]["km_max"];
$q++;
$i++;
if ($q <= $cnt_total) break;
} while ( $truck === $res["truck"][$q] );
$y++;
}
But does not work properly... I'm too far of the solution? The performance It's important for me in this particular case.
There is a phpfiddle where you can try:
http://phpfiddle.org/main/code/67j-ui5
Any idea, tip, or advice will be appreciated, and if you need more info, let me know and I'll edit the post.
Try this:
$res = $query->result_array();
$trucks = array();
foreach ($res as $row) {
$truck["truck"] = $row["truck"];
$truck["truck_data"] = array(
'oil_type' => $row["oil_type"],
'km_min' => $row["km_min"],
'km_max' => $row["km_max"],
);
$trucks[] = $truck;
}
var_dump($trucks);
What you've provided as a desired result is a little ambiguous. This might give what you're asking for.
$trucks=array();
$res = $query->result_array(); // assuming this is actually several trucks
foreach ($res as $r){
// set up the array from the data without writing out every column manually
$truck=array(
'truck'=>$r['truck'],
'truck_data'=>$r,
);
// remove the bit you wanted separately as 'truck' from 'truck_data'
unset($truck['truck_data']['truck']);
// push into $trucks
$trucks[]=$truck;
}
Is this what you need? conversion result is stored in $result
$result = array();
foreach($query->result_array() as $truck)
{
$new_truck = array();
$new_truck['truck'] = $truck['truck'];
$new_truck['truck_data'] = array();
$new_truck['truck_data']['oil_type'] = $truck['oil_type'];
$new_truck['truck_data']['km_min'] = $truck['km_min'];
$new_truck['truck_data']['km_max'] = $truck['km_max'];
array_push($result, $new_truck);
}

Using key function without overwritting the same keys in PHP

I'd like to extract the data from an array within an array. But the problem is, when I use the key function it overwrites the first entry. I'm expecting to have a result of 135 rows, but it only gives me back 114.
Below is the code where I'm getting a problem..
while($row1 = mssql_fetch_array($result1)) {
$mycount = $mycount + 1;
$boskey = $row1["EAN11"]."^".$row1["AUART"];
$boskey1 = $row1["ORDNUM"]."^".$row1["ERDAT"]."^".$row1["KUNAG"]."^".$row1["NAME1"]."^".$row1["PERNR"]."^".$row1["FKIMG"]."^".$row1["BASEPRICE"]."^".$row1["NETPRICE"]."^".$row1["ZZMAXDISC"]."^".$row1["MVGR1"]."^".$row1["KDGRP"];
$bos[$boskey][$boskey1] = $bos[$boskey][$boskey1];
}
for ($i = 0; $i < count($bos); $i++) {
$abc = key($bos);
$arr = explode("^",$abc);
$ean11 = $arr[0];
$auart = $arr[1];
echo $ean11 . " - " . $auart;
for ($j = 0; $j < count($bos[$abc]); $j++) { //total count of $bos[$abc] is 114.
$xxx = key($bos[$abc]);
$arr1 = explode("^",$xxx);
$ordnum = $arr1[0];
$docdate = $arr1[1];
$customer = $arr1[2];
$name1 = $arr1[3];
$ae = $arr1[4];
$qty = $arr1[5];
$baseprice = $arr1[6];
$netprice = $arr1[7];
//$discount = $arr1[8];
$booktype = $arr1[9];
$kdgrp = $arr1[10];
$discount = 100 - (100 * ( $netprice / $baseprice));

Categories