I fill a session like this:
$_SESSION[$id]=$value;
And I'm reading out the array with this:
foreach($_SESSION AS $value){...}
But how can I read out the $id of the session too? Array key?
Thanks!
foreach($_SESSION as $key => $value){
}
http://br2.php.net/manual/en/control-structures.foreach.php
You need something like the following:
foreach($_SESSION AS $key => $value) {
echo "$key -> $value";
}
foreach ($_SESSION as $key => $value)
foreach ($_SESSION as $key => $value) {
print $key . '<br>';
print $value;
}
foreach ($_SESSION as $key => $value) {
echo $key ;
echo $value;
}
session_start();
$dataArray = [];
//foreach(.......) can you passed your array value
foreach ($posts as $post) {
$myData = [];
//can you change ['name']
$myData["name"] = $post->name;
$myData["email"] = $post->email;
$dataArray[] = $myData;
}
$_SESSION["getAllarrydata"] = $dataArray;
//can you call other page
echo "<pre>";
print_r($_SESSION);
$array = array();
/*$array = array(0 => "Value");*/
$array[0] = "Value";
$_SESSION["array"] = $array;
/*$_SESSION["array"][0]= "Value 2";*/
foreach($_SESSION["array"] as $key => $value){
print $key;
print $value;
}
Related
I have the following function.
printArray($_POST);
function printArray($array){
foreach ($array as $key => $value){
echo "$key: $value <br>";
}
}
I modified it from here... Print $_POST variable name along with value
It gets all my html values along with there variable names and echos them. I need to save the output to variable. I've tried using ob..
printArray($_POST);
ob_start();
function printArray($array){
foreach ($array as $key => $value){
echo "$key: $value <br>";
}
}
$out = ob_get_contents();
ob_end_clean();
print $out;
and that just gives me nothing. Ive tried just saving my echo to a variable and that gets me closer...
&out = “”
function printArray($array){
foreach ($array as $key => $value){
$out = "$key: $value <br>";
}
}
but it only gives me the last value in my output. Help me out here what am i doing wrong.
NOTE: I screwed up and forgot to modify one of my functions to show what I was doing, whoops..
See screenshot:
<?php
printArray($_POST);
function printArray($array){
$result = [];
foreach ($array as $key => $value){
$result[] = (object)[
$key => $value
];
}
header('Content-Type: application/json; charset=utf-8');
echo json_encode($result);
}
its easy for sure..
i have code like this:
$indeks = 0;
foreach ($list as $k => $v)
{
$data['fname'] = $customer->firstname;
$data['lname'] = $customer->lastname;
$data['code'] = $code['code'];
$tablica[$indeks] = $data;
$indeks++;
and i want to read only 'code' value for each array.
i try:
foreach($tablica as $k => $v){
foreach ($v as $key => $value ) {
echo $value
}
}
but i get all arrays values.
when i try
foreach($tablica as $k => $v){
foreach ($v['code'] as $key => $value ) {
echo $value
}
}
i have nothing...
thx for help
You can use array_column function to get all values of column, for example:
foreach (array_column($tablica, 'code') as $value) {
echo $value;
}
I think a For loop should help
for($i=0;$i<count($tablica);$i++){
echo $tablica[$i]['code'];
}
or get all Codes into an Array
$code = array();
for($i=0;$i<count($tablica);$i++){
$code[$i] = $tablica[$i]['code'];
}
You don't need nested loops.
foreach ($tablica as $value) {
echo $value['code'];
}
DEMO
How can i know the variables ?
foreach($tree as $key => $value){
if (in_array($value['name'], $arr_folders)) {
${'id_'.$value['name']} = $value['id'];
}
//how to know variables?
$id_???
Currently I know the $value['name'] ie. it may be one,two, three, etc. but how to use them
echo $id_one;
I wanted to know here to split them in an array. So i can use
print_r($vars); which would result $id_one, $id_two, etc..
Something like this?
<?php
$array = [];
foreach($tree as $key => $value){
if (in_array($value['name'], $arr_folders)) {
$array[] = $value['id'];
}
}
print_r($array);
You can find variables by code:
foreach (get_defined_vars() as $var_name => $var_value) {
if(strpos($var_name, 'id_') ===0 ){
//it's your variable
}
}
But store variable in local scope look wrong.
May be better store to an other array:
$arIds = array();
foreach($tree as $key => $value){
if (in_array($value['name'], $arr_folders)) {
$arIds['id_'.$value['name']] = $value['id'];
}
}
Below is a simple array that I created:
$colors = array(
"parent1" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
),
"parent2" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
)
);
What I need to get is the key of my level 1 arrays which are string "parent1" and "parent2".
Currently I'm using foreach with while loop to get the key
foreach ($colors as $valuep) {
while (list($key, $value) = each($colors)) {
echo "$key<br />";
}
}
but I'm only able to get the "parent2" string from using the above method and not "parent1".
You're so close.
foreah($colors as $key => $val)
{
echo $key . "<br/>";
}
Use the key like so:
foreach ($colors as $key => $value) {
echo $key.'<br>';
}
To print out the keys:
foreach ($colors as $key => $value) {
echo $key . '<br />';
}
You can also get all of the keys from an array by using the array_keys() method, for example:
$keys = array_keys($colors);
foreach ($keys as $key) {
echo $key . '<br />';
}
I'm passing an array inside a GET(url) call like this:
&item[][element]=value
Then I do my stuff in PHP:
$item = $_GET['item'];
foreach ($item as $aValue) {
foreach ($aValue as $key => $value) {
echo '$key $value';
The problem I'm facing is that I need to have(echo) a third 'value':
echo '$key $value $thirdValue';
Do I have to change my the URL I'm passing or the foreach? And how do I do that? I Googled but I can't make heads nor tails out of it.
$item = $_GET['item'];
$item_temp=array_values($item);
foreach ($item as $aValue) {
foreach ($aValue as $key => $value) {
echo '$key $value'.$item_temp[2];
}
}
<?php
$item = $_GET['item'];
$r=array();
foreach($item as $rt){
array_push($r,array(key($rt)=> $rt));
}
foreach($r as $rt){
foreach($rt as $rt2){
$k = key($rt2);
echo $k.'__'.$rt2[$k] ;
echo "<br>";
}
}
?>
it's Work .