I have a string as shown bellow:
SIM types:Azadi|Validity:2 Nights|Expirable:yes
I have the following code to seperate them by | and then show them line by line
$other = "SIM types:Azadi|Validity:2 Nights|Expirable:yes";
$others['items'][] = explode("|",$other);
for($i = 0; $i < count($others['items']); $i++){
echo $others['items'][$i];
}
but the for loop is iterated only once and prints only the first value. This is what i am getting now:
SIM types:Azadi
Try like this
$others['items'] = explode("|",$other);
$my_count = count($others['items']);
for($i = 0; $i < $my_count; $i++){
echo $others['items'][$i];
}
Change
$others['items'][] = explode("|",$other);
to
$others['items'] = explode("|",$other);
remove []
Explode will return a array. ref: http://php.net/manual/en/function.explode.php
$other = "SIM types:Azadi|Validity:2 Nights|Expirable:yes";
$others['items'] = explode("|",$other);
for($i = 0; $i < count($others['items']); $i++){
echo $others['items'][$i];
}
Try this:
<?php
$other = "SIM types:Azadi|Validity:2 Nights|Expirable:yes";
$others = explode("|",$other);
$total = count($others);
for($i = 0; $i < $total; $i++){
echo $others[$i];
}
?>
Related
I have the following code:
$extraPhoto_1 = get_field('extra_photo_1');
$extraPhoto_2 = get_field('extra_photo_2');
$extraPhoto_3 = get_field('extra_photo_3');
$extraPhoto_4 = get_field('extra_photo_4');
But I would like to rewrite it with a for loop, but I can't figure out how to put a variable within the value field. What I have so far is:
for($i = 1; $i < 5; $i++) {
${'extraPhoto_' . $i} = get_field('extra_photo_ . $i');
}
I've tried with an array like this:
$myfiles = array();
for ($i = 1; $i < 5; $i++) {
$myfiles["$extraPhoto_$i"] = get_field('extra_photo_ . $i');
}
Nothing seems to fix my problem. I'v searched on the PHP website (variable variable).
There is some bug in your code which not allowing. Use 'extra_photo_'. $i instead of 'extra_photo_. $i'
for($i = 1; $i < 5; $i++) {
$extraPhoto_.$i = get_field('extra_photo_'. $i);
}
You can build an array as defined below and than just call extract($myfiles) to access them as variables.
Again your syntax for the get field is incorrect you should append $i after the quotes.
$myfiles = array();
for ($i = 1; $i < 5; $i++) {
$myfiles["extraPhoto_".$i] = get_field('extra_photo_'.$i);
}
extract($myfiles);
If you want to create dynamic variable, use below code
for ($i = 1; $i < 5; $i++) {
${'extraPhoto_'.$i} = $_POST['extra_photo_'.$i];
}
if you want to assign variables to array use below code.
for ($i = 1; $i < 5; $i++) {
$myfiles["extraPhoto_$i"] = $_POST['extra_photo_'.$i];
/// $myfiles["extraPhoto_$i"] = get_field('extra_photo_'.$i);
}
and than to see if values are assign to new array or not, you can use below code.
echo "<pre>";print_r($myfiles);echo "</pre>";
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;
I am having trouble dynamically naming my PHP variables when doing a foreach loop. Here is my code:
$count = count($_POST['member']);
for ($i = 0; $i < $count; $i++) {
$fname = $_POST['member'][$i]['fname'];
$lname = $_POST['member'][$i]['lname'];
}
How would I append the number contained in $i to $fname & $lname so that $fname & $lname become $fname1 & $lname_2 and so on? Or is there a better way of doing this? Nothing I have tried works.
$i = 0;
$output = array();
foreach ($_POST['member'] as $member)
{
$output['fname' . $i] = $member['fname'];
$output['lname' . $i] = $member['lname'];
$i++;
}
extract($output);
Though..why?
EDIT: Answer to your new question - how to store in the session:
session_start();
$count = count($_POST['member']);
for ($i = 0; $i < $count; $i++) {
$_SESSION["fname_{$i}"] = $_POST['member'][$i]['fname'];
$_SESSION["lname_{$i}"] = $_POST['member'][$i]['lname'];
}
Try this
$count = count($_POST['member']);
for ($i = 0; $i < $count; $i++) {
$fname.'_'.$i = $_POST['member'][$i]['fname'];
$lname.'_'.$i = $_POST['member'][$i]['lname'];
}
So you can access $fname_1,$fname_2 etc...
Hi try the simple concatination(".") operator of php.. So your code will be
$count = count($_POST['member']);
for ($i = 0; $i < $count; $i++) {
$fname = $_POST['member']['fname'.$i];
$lname = $_POST['member']['lname_'.$i];
}
Please check my code below,it returns 0 while I am expecting a result 14.But when I add A+D manually it returns 5.Am i doing something wrong inside the loop ?
<?php
define('A',1);
define('B',2);
define('C',3);
define('D',4);
define('E',5);
//echo A+D; returns 5
$name = 'EACE';
$len = strlen($name);
for($i = 0; $i<=$len; $i++)
{
$val += $name[$i];
}
echo $val; //returns 0
?>
You need to use constant(..) to get the value of a constant by name. Try this:
for ($i = 0; $i < strlen($name); $i++) {
$val += constant($name[$i]);
}
define('A',1);
define('B',2);
define('C',3);
define('D',4);
define('E',5);
//echo A+D; returns 5
$name = 'EACE';
$len = strlen($name);
$val = null;
for($i = 0; $i<=$len-1; $i++)
{
$val += constant($name[$i]);
}
echo $val;
I want to create somthing like 100 var's which their names will be:
$numbr_1 = 1;
$numbe_2 = 2;
$number_3 = 3;
...
I won't write 100 vars of course, but there is a way to do it with foor loop or somthing? I thought about this:
for ($i = 1; $i <= 100; $i++)
$number_{$i} = $i;
You're talking about variable variables, and they are incredibly stupid to use. For one, they make debugging next to impossible.
What you want is an array:
for ($i = 1; $i <= 100; $i++) {
$numbers[$i] = $i;
}
Something like this should work:
for($i = 1 ; $i <= 100 ; $i++){
$var_name = "number_$i";
$$var_name = $i;
}
for($i=1;$i<=100;$i++) {
$j="number$i";
$$j = $i;
}
Why don't you use an array?
$number = array();
for ($i = 0; $i < 100; $i++)
{
$number[] = $i;
}
for($i = 1 ; $i <= 100 ; $i++){
${'number_'.$i} = $i;
}
Possible solution is usage of array.
$number = array();
for ($i = 1; $i <= 100; $i++) {
$number[$i] = $i;
}