how to use session with different label in loop in php - php

i have query in php.
while($res=mysql_fetch_array($qu)) {
$s=$res['start'];
$d=$res['end'];
$wr=$res['waypoint'];
$_SESSION['start' ]=$s;
$_SESSION['end' ]=$d;
$_SESSION['waypoint' ]=$wr;
}
?>
In this session is overright and last value will store. i want each value from start to end of loop.

if you are looping and you want each value. Try the following:
while($res=mysql_fetch_array($qu)) {
$s=$res['start'];
$d=$res['end'];
$wr=$res['waypoint'];
$_SESSION['start'][]=$s;
$_SESSION['end'][]=$d;
$_SESSION['waypoint'][]=$wr;
}
This will create an array for each $_SESSION
You could simplify the loop to:
while($res=mysql_fetch_array($qu)) {
$_SESSION['start'][]=$res['start'];
$_SESSION['end'][]=$res['end'];
$_SESSION['waypoint'][]=$res['waypoint'];
}

what i do is set a numeric value and increment it by 1 each loop.
$a = 0;
while($res=mysql_fetch_array($qu)) {
$s=$res['start'];
$d=$res['end'];
$wr=$res['waypoint'];
$_SESSION[$a]['start' ]=$s;
$_SESSION[$a]['end' ]=$d;
$_SESSION[$a]['waypoint' ]=$wr;
$a++;
}
?>

Related

store multiple values in associative array and return that array in php

I have seen a lot of answers (so please don't mark it duplicate) to store values in associative array but I want to return that array in PHP. Here is my code. It prints all values but only return the first value. I want the whole array returned to use in another function.
Please help
function xml_parsing($response,$size,$array)
{
for($k=0;$k<$size;$k++)
{
$price=(string)$response->Items->Item[$k]->ItemAttributes->ListPrice->FormattedPrice;
$myarray[$k]=explode(',',$array["ItemId"]);
$update_fields=array('sku','price');
if($price=='')
{
$Col_array=array('sku'=>"".$myarray[$k][$k]."",'price'=>"-1");
}
else
{
$price_trimed=ltrim($price,'$');
$Col_array=array('sku'=>"".$myarray[$k][$k]."",'price'=>$price_trimed);
// I store the values here using a loop
}
}
print_r($Col_array);
return $col_array; //but here it return only first value
// But I want to return the whole array**
// I can't return it inside loop because it terminates
// the loop and the function
}
Some pseudocode:
//first, initialize empty array:
$Col_array = [];
//do the loop thing.
while(...){
//inside the loop:
//add to array:
$Col_array []= array('sku'=>"".$myarray[$k][$k]."",'price'=>$price_trimed);
}//loop ends here
print_r($Col_array); //<----- all values.
return $Col_array;
notice how one uses []= to append to an array.
Actually, the mistake here is you are not storing the data that is traversed in the loop. You need to push $Col_array to the main array to get the desired outcome. Here is your code
function xml_parsing($response,$size,$array)
{
//create an empty array before entering the for loop.
$main_array = array();
for($k=0;$k<$size;$k++)
{
$price=(string)$response->Items->Item[$k]->ItemAttributes->ListPrice->FormattedPrice;
$myarray[$k]=explode(',',$array["ItemId"]);
$update_fields=array('sku','price');
if($price=='')
{
$Col_array=array('sku'=>"".$myarray[$k][$k]."",'price'=>"-1");
}
else
{
$price_trimed=ltrim($price,'$');
$Col_array=array('sku'=>"".$myarray[$k][$k]."",'price'=>$price_trimed);
// I store the values here using a loop
}
//Here you push the $col_array to main_array
array_push($main_array,$Col_array);
//This will store whole bunch of data as multi dimensional array which you can use it anywhere.
}
print_r($main_array);
return $main_array;
}
I think you will get what you desired.
Here I want to answer my own question
function xml_parsing($response,$size,$array)
{
for($k=0;$k<$size;$k++)
{
$price=(string)$response->Items->Item[$k]->ItemAttributes->ListPrice-
>FormattedPrice;
//echo "PKkkkkkkkk".$array["ItemId"]."llllll";
$myarray[$k]=explode(',',$array["ItemId"]);
//print_r($myarray[$k]);
$update_fields=array('sku','price');
if($price=='')
{
// $Col_array=array('sku'=>"".$myarray[$k][$k]."",'price'=>"-1");
/*Here is the solution we have to index it inside the loop like a 2D
array now it contain an array inside which array of key value pair is
present (associative array) which i wanted */
//**********
$Col_array[$k]['sku']=$myarray[$k][$k];
$Col_array[$k]['price']="-1";
//*********
}
else
{
$price_trimed=ltrim($price,'$');
// final array to be stored in database
// $Col_array=array('sku'=>"".$myarray[$k]
[$k]."",'price'=>$price_trimed);
$Col_array[$k]['sku']=$myarray[$k][$k];
$Col_array[$k]['price']=$price_trimed;
}
}
return $Col_array;
}
//*******
//To Print the returned array use
foreach ($Col_array as $value) {
print_r($value);
}
//*********

Print an array variable with a loop

We have two array variables with names first_names and last_names that have an uncertain number of members. We intend to select an appropriate loop and only use the same information loop with the corresponding first and second variable information respectively.
You must use a loop.
<؟php
$first_names=array('jack','root','admin');
$last_names=array('jack111','root222','admin333'');
foreach (array_combine($first_names ,$last_names) as $fname => $lname)
{
print_r($fname.$lname."<br>");
};
?>
Try this. Simple n easy to understand
$first =count($first_names);
$last =count($last_names);
for($i=1;i<=$first;$i++)
{
echo $i;
}
for($i=1;i<=$last;$i++)
{
echo $i;
}

How to track if a variable value is used multiple times in same php while loop

I found this snippet in a different question and answer, and it detects if a variable value is the same as the variable value in the previous loop.
$prevValue = NULL;
while(condition) {
if ($curValue == $prevValue) {
//do stuff
}
$prevValue = $curValue;
}
However what I want to do is to check if a variable value has been used before in the loop, but anywhere in the loop, so if the value happened 1 or 2 or 10 loops ago I want it to tell me if the variable value has come through the loop before.
Make an array of previouse values and test by in_array function
$prevValue = [];
while(condition) {
if (in_array($curValue, $prevValue)) {
//do stuff
}
$prevValue[] = $curValue;
}
Use an array to store each value and then check if the current value is in the array:
$prevValues = array();
while(/*condition*/) {
if (in_array($curValue, $prevValues)) {
//do stuff
}
$prevValues[] = $curValue;
}
As Marc B points out in a comment, you can do it this way as well. It might be marginally faster but I haven't tested it:
while(/*condition*/) {
if (isset($prevValues[$curValue])) {
//do stuff
}
$prevValues[$curValue] = 1; //set to whatever
}
If you just want to print out unique values, use array_unique.
Here is an example:
//This should output 2 3 1 4
$arr = array(2, 3, 2, 1, 4, 4);
foreach(array_unique($arr) as $value){
echo $value;
}

Unset value (with unknown index) from array without iterating

I want to unset a known value from an array. I could iterate with a for loop to look for the coincident value and then unset it.
<?php
for($i=0, $length=count($array); $i<$length; $i++)
{
if( $array[$i] === $valueToUnset )
//unset the value from the array
}
Any idea? Any way to achieve it without looping?
I am presuming that your intention is to get the index back, as you already have the value. I am further presuming that there is also a possibility that said value will NOT be in the array, and we have to account for that. I am not sure what you are using array_slice for. So, if I properly understand your requirements, a simple solution would be as follows:
<?php
$foundIndex = false; //Initialize variable that will hold index value
$foundIndex = array_search($valueToExtract, $array);
if($foundIndex === null) {
//Value was not found in the array
} else {
unset($array[$foundIndex]; //Unset the target element
}
?>
array_diff is the solution:
<?php
array_diff($array, array($valueToUnset));
No iteration needed.

How do I delete array elements one by one every time I reload the page?

<?php
session_start();
$_SESSION['del']=array("a1","a2","a3","a4","a5");
unset($_SESSION['del'][0]);
echo implode(" ",$_SESSION['del'])
?>
How do I remove each array element one by one every time I refresh the page?
here is the code
<?php
session_start();
if(isset($_SESSION['del'])) { // to make sure array is not set again as in question
unset($_SESSION['del'][0]); // remove the first element
$_SESSION['del'] = array_values($_SESSION['del']); // to shift rest of the elements one location left for making indexes starts from 0
} else { // set session array only once
$_SESSION['del']=array("a1","a2","a3","a4","a5");
}
echo implode(" ",$_SESSION['del']); // print results
?>
if(isset($_SESSION['del']))
{
if(is_array($_SESSION['del']))
{
array_shift($_SESSION['del']);
}
}
Code Explanation
if the session del is currently set.
and that the session['del'] is an array
remove the first value of the array del.
I am not sure if it works with $_SESSION, but if it is an array then array_shift() would be what you are looking for:
array_shift($_SESSION['del']);

Categories