I have this name="opt['.$id.']" value="'.$points.'" inside a checkbox input.Does anybody knows how I can get the $id?
UPDATED:
foreach($_POST['opt'] as $id => $value) {
$gift_ids = $value;
$gift_ids2 = implode(", ", $gift_ids);
}
echo $gift_ids2;
}
But I don't get any value on echo..
You need to iterate over the HTML array. Something like this should do it for you:
foreach($_POST['opt'] as $id => $value) {
Demo: https://eval.in/585379
your used array so you need to Iterate the $_POST['opt'] value
$_POST = array('opt' => array('1'=>100 ), 'eksasrgirwsh' => 'other');
foreach($_POST['opt'] as $id => $value)
{
echo $id //key example 1
echo $value //value example 100
}
If your $_POST is like this
$_POST = array('opt' => array('1'=>100 ), 'eksasrgirwsh' => 'other');
and you are giving $_POST['opt'] to foreach then your array for foreach is
$_POST['opt'] = array('1'=>100);
then you can't use implode in foreach because it give you an error. Do it without implode.
foreach($_POST['opt'] as $id => $value) {
$gift_ids = $value;
echo $gift_ids;
}
Related
I have an array that looks something like this:
Array
(
[2] => http://www.marleenvanlook.be/admin.php
[4] => http://www.marleenvanlook.be/checklogin.php
[5] => http://www.marleenvanlook.be/checkupload.php
[6] => http://www.marleenvanlook.be/contact.php
)
What I want to do is store each value from this array to a variable (using PHP). So for example:
$something1 = "http://www.marleenvanlook.be/admin.php";
$something2 = "http://www.marleenvanlook.be/checklogin.php";
...
You can use extract():
$data = array(
'something1',
'something2',
'something3',
);
extract($data, EXTR_PREFIX_ALL, 'var');
echo $var0; //Output something1
More info on http://br2.php.net/manual/en/function.extract.php
Well.. you could do something like this?
$myArray = array("http://www.marleenvanlook.be/admin.php","http://www.marleenvanlook.be/checklogin.php","etc");
$i = 0;
foreach($myArray as $value){
${'something'.$i} = $value;
$i++;
}
echo $something0; //http://www.marleenvanlook.be/admin.php
This would dynamically create variables with names like $something0, $something1, etc holding a value of the array assigned in the foreach.
If you want the keys to be involved you can also do this:
$myArray = array(1 => "http://www.marleenvanlook.be/admin.php","http://www.marleenvanlook.be/checklogin.php","etc");
foreach($myArray as $key => $value){
${'something'.$key} = $value;
}
echo $something1; //http://www.marleenvanlook.be/admin.php
PHP has something called variable variables which lets you name a variable with the value of another variable.
$something = array(
'http://www.marleenvanlook.be/admin.php',
'http://www.marleenvanlook.be/checklogin.php',
'http://www.marleenvanlook.be/checkupload.php',
'http://www.marleenvanlook.be/contact.php',
);
foreach($something as $key => $value) {
$key = 'something' . $key;
$$key = $value;
// OR (condensed version)
// ${"something{$key}"} = $value;
}
echo $something2;
// http://www.marleenvanlook.be/checkupload.php
But the question is why would you want to do this? Arrays are meant to be accessed by keys, so you can just do:
echo $something[2];
// http://www.marleenvanlook.be/checkupload.php
What I would do is:
$something1 = $the_array[2];
$something2 = $the_array[4];
I am having a array like so and I am looping trough it like this:
$options = array();
$options[0] = 'test1';
$options[1] = 'test2';
$options[2] = 'test3';
foreach($options as $x)
{
echo "Value=" . $x ;
echo "<br>";
}
It outputs as expected:
Value=test
Value=test2
Value=test3
Now I want to add some options to my array and loop trough them:
$options = array();
$options['first_option'] = 'test';
$options['second_option'] = get_option('second_option');
$options['third_option'] = get_option('third_option');
foreach($options as $x)
{
echo "Value=" . $x ;
echo "<br>";
}
But it does not work as I want. Because it outputs:
Value=first_option
Value=second_option
Value=third_option
So now I do not know how to access stored values using foreach from these guys?
Something like:
Value=first_option='test'
So when I use print_r($options)
Output is:
Array
(
[first_options] => test
[second_option] =>
[third_option] =>
)
1
your loop should look like this:
foreach($options as $key => $val){
echo "Val: ".$val;
echo "<br/>";
}
Your code is working just as expected and producing the desired result. You must have something else changing the values in $options. Correction: now that I see your edit, your functions are not returning any values, so options 1 and 2 are blank. Make sure that function returns something. Other than that, all of this code is good.
By the way, I recommend this:
$options = [
'first_option' => 'test',
'second_option' => get_option('second_option'),
'third_option' => get_option('third_option')
];
foreach($options as $key) {
echo "Value = {$key}<br>";
}
you can also use:
foreach($options as $key => $value) {
echo "Value - {$value} = {$key}<br>";
}
or you could at least replace array() with just []. Those are just some suggestions for neatness.
$arr = array ('name'=>'bunt','game'=>'battlefield','fame'=>'hero');
foreach ($arr as $key=>$val){
$val = ucfirst($val);
}
var_dump($arr);
// result would be
// 'name' => 'Bunt', 'game' => 'Battlefield', 'fame' => 'Hero'
I am missing something here.... How to accomplish this ?
Use array_map()
$new_array = array_map('ucfirst', $arr);
See it in action
$val is just a temporary variable in each iteration. To update the value of each key you need to pass it as a reference. Do this.
foreach ($arr as $key => &$val) {
$val = ucfirst($val);
}
Notice the & following $val.
Here's some documentation on references in PHP.
foreach ($arr as $key=>&$val){
$val = ucfirst($val);
}
Put an & sign before $val . that will make it reference the variable instead of assigning the value.
Why not just use the key to access the array?
<?php
$arr = array('name' => 'bunt', 'game' => 'battlefield');
foreach ($arr as $key => $val) {
$arr[$key] = ucfirst($val);
}
var_dump($arr);
How to echo out the values individually of this array?
Array ( [0] => 20120514 [1] => My Event 3 )
so
echo $value[0]; etc
I have this so far:
foreach (json_decode($json_data_string, true) as $item) {
$eventDate = trim($item['date']);
// positive limit
$myarray = (explode(',', $eventDate, 2));
foreach ($myarray as $value) {
echo $value;
}
This echo's out the whole string no as an array. and if i do this?
echo $value[0};
Then I only get 2 characters of it??
The print_r :
Array ( [0] => 20120430 [1] => My Event 1 )
foreach ($array as $key => $val) {
echo $val;
}
Here is a simple routine for an array of primitive elements:
for ($i = 0; $i < count($mySimpleArray); $i++)
{
echo $mySimpleArray[$i] . "\n";
}
you need the set key and value in foreach loop for that:
foreach($item AS $key -> $value) {
echo $value;
}
this should do the trick :)
The problem here is in your explode statement
//$item['date'] presumably = 20120514. Do a print of this
$eventDate = trim($item['date']);
//This explodes on , but there is no , in $eventDate
//You also have a limit of 2 set in the below explode statement
$myarray = (explode(',', $eventDate, 2));
//$myarray is currently = to '20'
foreach ($myarray as $value) {
//Now you are iterating through a string
echo $value;
}
Try changing your initial $item['date'] to be 2012,04,30 if that's what you're trying to do. Otherwise I'm not entirely sure what you're trying to print.
var_dump($value)
it solved my problem, hope yours too.
Why does this yield this:
foreach( $store as $key => $value){
$value = $value.".txt.gz";
}
unset($value);
print_r ($store);
Array
(
[1] => 101Phones - Product Catalog TXT
[2] => 1-800-FLORALS - Product Catalog 1
)
I am trying to get 101Phones - Product Catalog TXT.txt.gz
Thoughts on whats going on?
EDIT: Alright I found the solution...my variables in my array had values I couldn't see...doing
$output = preg_replace('/[^(\x20-\x7F)]*/','', $output);
echo($output);
Cleaned it up and made it work properly
The doc http://php.net/manual/en/control-structures.foreach.php clearly states why you have a problem:
"In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference."
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
Referencing $value is only possible if the iterated array can be referenced (i.e. if it is a variable). The following code won't work:
<?php
/** this won't work **/
foreach (array(1, 2, 3, 4) as &$value) {
$value = $value * 2;
}
?>
Try
foreach( $store as $key => $value){
$store[$key] = $value.".txt.gz";
}
The $value variable in the array is temporary, it does not refer to the entry in the array.
If you want to change the original array entry, use a reference:
foreach ($store as $key => &$value) {
// ^ reference
$value .= '.txt.gz';
}
You are rewriting the value within the loop, and not the key reference in your array.
Try
$store[$key] = $value.".txt.gz";
pass $value as a reference:
foreach( $store as $key => &$value){
$value = $value.".txt.gz";
}
Try
$catalog = array();
foreach( $store as $key => $value){
$catalog[] = $value.".txt.gz";
}
print_r ($catalog);
OR
foreach( $store as $key => $value){
$store[$key] = $value.".txt.gz";
}
print_r ($store);
Depends on what you want to achieve
Thanks
:)
I believe this is what you want to do:
foreach( $store as $key => $value){
$store[$key] = $value.".txt.gz";
}
unset($value);
print_r ($store);
How about array map:
$func = function($value) { return $value . ".txt.gz"; };
print_r(array_map($func, $store));
foreach(array_container as & array_value)
Is the way to modify array element value inside foreach loop.