Adding a value to a PHP associative array - php

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 .

Related

PHP read value form multidimensional array

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 to sort array into specific format using PHP

I need one help. I need to sort an array into a specific format using PHP. I am explaining my code below.
$firstArr=array("K"=>"location","L"=>"nearaddrss","M"=>"dsdsfll");
$secondArr=array(array("K"=>"loc","L"=>"Aggggkk","M"=>"dsdsfuu","A"=>"jhkhjg","B"=>"nnnn","C"=>"dsmmmmdsf"),array("K"=>"lo","L"=>"Aggggpp","M"=>"dsdsfjj","A"=>"jhkhjg","B"=>"nnnn","C"=>"dsmmmmdsf"));
$firstResultArr=array();
$secondResultArr=array();
foreach ($firstArr as $key => $value) {
foreach ($secondArr as $key1 => $value1) {
foreach ($value1 as $key2 => $value2) {
if($key==$key2){
$firstResultArr[]=$value;
$secondResultArr[]=array($value=>$value2);
}
}
}
}
echo json_encode($secondResultArr);exit;
Here I am getting the output in below format.
[{"location":"loc"},{"location":"lo"},{"nearaddrss":"Aggggkk"},{"nearaddrss":"Aggggpp"},{"dsdsfll":"dsdsfuu"},{"dsdsfll":"dsdsfjj"}]
But here I need my output like below.
[{"location":"loc","nearaddrss":"Aggggkk","dsdsfll":"dsdsfuu"},{"location":"lo","nearaddrss":"Aggggpp","dsdsfll":"dsdsfjj"}]
Please help me.
Have a look.
$firstArr=array("K"=>"location","L"=>"nearaddrss","M"=>"dsdsfll");
$secondArr=array(array("K"=>"loc","L"=>"Aggggkk","M"=>"dsdsfuu","A"=>"jhkhjg","B"=>"nnnn","C"=>"dsmmmmdsf"),array("K"=>"lo","L"=>"Aggggpp","M"=>"dsdsfjj","A"=>"jhkhjg","B"=>"nnnn","C"=>"dsmmmmdsf"));
$firstResultArr=array();
$secondResultArr=array();
$firstResultArr=array();
$secondResultArr=array();
foreach($secondArr as $key2 => $val2) {
foreach($val2 as $key3 => $val3) {
foreach ($firstArr as $key => $value) {
$firstResultArr[$value] = $val2[$key];
}
}
$secondResultArr[] = $firstResultArr;
}
echo json_encode($secondResultArr);exit;
Iterate over the second array and within that, iterate over the first array like this.
<?php
$firstArr=array(
"K"=>"location",
"L"=>"nearaddrss",
"M"=>"dsdsfll"
);
$secondArr=array(
array(
"K"=>"loc",
"L"=>"Aggggkk",
"M"=>"dsdsfuu",
"A"=>"jhkhjg",
"B"=>"nnnn",
"C"=>"dsmmmmdsf"
)
,array(
"K"=>"lo",
"L"=>"Aggggpp",
"M"=>"dsdsfjj",
"A"=>"jhkhjg",
"B"=>"nnnn",
"C"=>"dsmmmmdsf")
);
$result = array();
foreach ($secondArr as $key2 => $value2) {
$item = array();
foreach ($firstArr as $key => $value) {
$item[$value] = $value2[$key];
}
$result[] = $item;
}
echo json_encode($result);
die();

how to split stored variables name inside foreach

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'];
}
}

merge array with itself from foreach loop

I have foreach loop like:
foreach($attributes as $key => $value)
{
$option[] =["$value->name"=>"$value->value"]; //it is like ["color"=>"red"]
}
I want to merge $option[0], $option[1] and so on.... How to merge that ?
I tried:
for($i=1;$i<$count;$i++)
{
$option = array_merge($option[0],$option[$i]);
}
If you want a merged version, try this (you need only 1 loop):
$merged_options = array();
foreach($attributes as $key => $value)
{
$option[] =["$value->name" => "$value->value"];
$merged_options[$value->name] = $value->value;
}
This code should hopefully loop through each of your current arrays and reconstruct it to a multi-dimensional array.
foreach($attr as $k=>$v):
$temp = array();
$i = 0;
while(count($k) != $i):
array_push($temp, $k[$i] => $v[$i]);
$i++;
endwhile;
array_push($attr, $temp);
endforeach;
Hope it helped.
Why not you use something like this:
foreach($attributes as $key => $value)
{
$option[$value->name] =$value->value;
}

foreach($_SESSION AS $value){...} -> get array key of session

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;
}

Categories