how to call this variable in my array editcase - php

I have a session called $_SESSION['data']
And I have a text input called 'lengtezijde'
I already used foreach on the session:
foreach ($_SESSION['data'] as $key => $data);
And if I want to use my input from lengtezijde, I tried it like this:
echo $_SESSION['data'][$_GET['key'];
but then it is an array and I want the input value.
How do I go a layer deeper in the array to use the value?

Please try this inside the foreach loop:
echo $data['lengtezijde'];
or
echo $_SESSION['data'][$key]['lengtezijde'];
We have seen $key will have indexing value 0.
Note: as looping on session data you will get $data value as when print $data:
Array ( [hoogte] => 1 [kleur] => 1 [lengtezijde] => 800 [toevoegen] => toevoegen )
So you can get directly the value of lengtezijde by using as :
$data['lengtezijde']; inside the foreach loop.

Try this ....
echo $_SESSION['data'][$key]['lengtezijde'];

It is not clear how you set the value of the array. If you set the array value in the for-loop like this:
foreach ($_SESSION['data'] as $key => $data);
$data['lengtezijde'] = "some value";
Then to get the value you must do something like this:
$key = $_GET['key']; //or the key to the index you want
echo $_SESSION['data'][$key]['lengtezijde'];

Related

Extract each value from an associative array with unknown length and value names

I have an array like:
Array
(
[item1] => value1
[item2] => value
[item3] => value3
)
And I want to extract all the names and values to variables.
But say, I don't know the names of items that the array contains.
I wanna generate variables for each array item with the name of this item name in array to make possible of using this variables later.
The result should look like this:
item_name1 = item_value1
item_name2 = item_value2
item_name3 = item_value3
Seems the foreach loop should be usefull here.
I'm not sure if I understood this.
If you want the key from the array to become a variable with the same name you can use the extract function: http://php.net/manual/en/function.extract.php
Using built-in functions is always faster, but if you need the foreach approach with $$:
foreach ($array as $key=>$val)
{
$$key = $val;
}

WordPress: get data from array in database

I have custom fields that are created dynamicaly. I get the data from those fields and store it into a database as an array with update_post_meta. It's stored as a serialised array in the database:
a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}
Now I need to get this array and echo it out on the website, so it looks something like: 4 children (1993,1994,1995,1996).
Here's the code I use now, but it doesn't work.
<?php
$children = get_post_custom_values('rbchildyear');
foreach ($children as $key => $value){
echo "$key => $value('rbchildyear')<br>";
}
?>
And thats what I get in the front office:
0 => a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}('rbchildyear')
So how can I do that?
Thank you!
use unserialize().
$children = unserialize('a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}');
print_r($children);
This will return array
If you use get_post_meta it will return an array of values (numerically indexed). Then you can loop through the array with a foreach.
$childYears = get_post_meta($post_id, "rbchildyear", true);
foreach($childYears AS $theYear)
{
$printThis .= $theYear.",";
}
print count($childYears)." children ( ".$printThis." )";

Why are variables renamed within a for each statements?

I have wondered for a while now, what the reason is behind the renaming of a variable for a foreach statement. So for example, why is this:
foreach ($Foobar as $Foo) {};
Not used like this:
foreach ($Foobar) {};
I understand that this would just check to see if the value was true and is therefore a bad example, yet that doesn't explain why the whole variable needs to be renamed?
because you don't rename the variable, you declare another variable where the foreach result on every loop is stored in.
as a example you have this array
array('key' => 'value', 'foo' => 'bar');
and use this foreach syntax
foreach ($Foobar as $value)
the variable $value hold the value from the current loop of the array.
output each loop
first loop => $value holds value
second loop => $value holds bar
or your can write it like this
foreach ($Foobar as $key => $value)
with this on each loop the array key and value are written in $key and $value
output each loop
first loop => $value holds value / $key hold key
second loop => $value holds bar / $key hold foo
$Foo is one item of the $Foobar array, not the same variable.
$Foo is a variable that contains one different element of the Array $Foobar in each iteration

Can I use array_push on a SESSION array in php?

I have an array that I want on multiple pages, so I made it a SESSION array. I want to add a series of names and then on another page, I want to be able to use a foreach loop to echo out all the names in that array.
This is the session:
$_SESSION['names']
I want to add a series of names to that array using array_push like this:
array_push($_SESSION['names'],$name);
I am getting this error:
array_push() [function.array-push]:
First argument should be an array
Can I use array_push to put multiple values into that array? Or perhaps there is a better, more efficient way of doing what I am trying to achieve?
Yes, you can. But First argument should be an array.
So, you must do it this way
$_SESSION['names'] = array();
array_push($_SESSION['names'],$name);
Personally I never use array_push as I see no sense in this function. And I just use
$_SESSION['names'][] = $name;
Try with
if (!isset($_SESSION['names'])) {
$_SESSION['names'] = array();
}
array_push($_SESSION['names'],$name);
$_SESSION['total_elements']=array();
array_push($_SESSION['total_elements'], $_POST["username"]);
Yes! you can use array_push push to a session array and there are ways you can access them as per your requirement.
Basics:
array_push takes first two parameters array_push($your_array, 'VALUE_TO_INSERT');.
See: php manual for reference.
Example:
So first of all your session variable should be an array like:
$arr = array(
's_var1' => 'var1_value',
's_var2' => 'var2_value'); // dummy array
$_SESSION['step1'] = $arr; // session var "step1" now stores array value
Now you can use a foreach loop on $_SESSION['step1']
foreach($_SESSION['step1'] as $key=>$value) {
// code here
}
The benefit of this code is you can access any array value using the key name for eg:
echo $_SESSION[step1]['s_var1'] // output: var1_value
NOTE: You can also use indexed array for looping like
$arr = array('var1_value', 'var1_value', ....);
BONUS:
Suppose you are redirected to a different page
You can also insert a session variable in the same array you created. See;
// dummy variables names and values
$_SESSION['step2'] = array(
's_var3' => 'page2_var1_value',
's_var4' => 'page2_var2_value');
$_SESSION['step1step2'] = array_merge($_SESSION['step1'], $_SESSION['step2']);
// print the newly created array
echo "<pre>"; // for formatting printed array
var_dump($_SESSION['step1step2']);
echo "<pre>";
OUTPUT:
// values are as per my inputs [use for reference only]
array(4) {
["s_var1"]=>
string(7) "Testing"
["s_var2"]=>
int(4) "2124"
["s_var3"]=>
int(4) "2421"
["s_var4"]=>
string(4) "test"
}
*you can use foreach loop here as above OR get a single session var from the array of session variables.
eg:
echo $_SESSION[step1step2]['s_var1'];
OUTPUT:
Testing
Hope this helps!
Try this, it's going to work :
session_start();
if(!isset($_POST["submit"]))
{
$_SESSION["abc"] = array("C", "C++", "JAVA", "C#", "PHP");
}
if(isset($_POST["submit"]))
{
$aa = $_POST['text1'];
array_push($_SESSION["abc"], $aa);
foreach($_SESSION["abc"] as $key => $val)
{
echo $val;
}
}
<?php
session_start();
$_SESSION['data']= array();
$details1=array('pappu','10');
$details2=array('tippu','12');
array_push($_SESSION['data'],$details1);
array_push($_SESSION['data'],$details2);
foreach ($_SESSION['data'] as $eacharray)
{
while (list(, $value) = each ($eacharray))
{
echo "Value: $value<br>\n";
}
}
?>
output
Value: pappu Value: 10 Value: tippu Value: 12

How do I output these PHP array values?

Using PHP, how do I output/display the values from this array within my web page:
http://api.getclicky.com/api/stats/4?site_id=83367&sitekey=e09c6cb0d51d298c&type=clicks&output=php&unserialize
Looks like a print_r() output to me.
Use a foreach statement to iterate over the array echoing each as you go
foreach($data as $k => $v) {
echo "{$k} => {$v}";
}
$data is the input data
$k is the key $v is the $value
The above is only useful for a single depth array for an array of arrays like the example data provided you need to use a recursive function then check to see if the value is an array if so call the function recursivally.
If the data structure never changes then some nested loops will do the job. (some people think recursion is evil, I pity them)
DC
Start with our Array
$myarray = array(
"Key1" => "This is the value for Key1",
"Key2" => "And this is the value for Key2"
);
Show All Output (Helpful for Debugging)
print_r($myarray);
/* or */
var_dump($myarray);
Show only a Single Value
print $myarray["Key1"]; // Prints 'This is the value for Key1'
print $myarray[0]; // Prints 'This is the value for Key1'

Categories