PHP read value form multidimensional array - php

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

Related

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

Adding a value to a PHP associative array

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 .

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

PHP Merge Similar Objects In A Multidimensional Array

I have a multidimensional array in PHP, something that looks like:
array(array(Category => Video,
Value => 10.99),
array(Category => Video,
Value => 12.99),
array(Category => Music,
Value => 9.99)
)
and what I would like to do is combine similar categories and output everything into a table, so the output would end up being:
<tr><td>Video</td><td>23.98</td></tr>
<tr><td>Music</td><td>9.99</td></tr>
Any suggestions on how to do this?
EDIT:
I can have these in two different arrays if that would be easier.
A simple loop will do:
$array = [your array];
$result = array();
foreach ($array as $a) {
if (!isset($result[$a['Category']])) {
$result[$a['Category']] = $a['Value'];
} else {
$result[$a['Category']] += $a['Value'];
}
}
foreach ($result as $k => $v) {
echo '<tr><td>' . htmlspecialchars($k) . '</td><td>' . $v . '</td></tr>';
}
$result = array();
foreach ($array as $value) {
if (isset($result[$value['Category']])) {
$result[$value['Category']] += $value['Value'];
} else {
$result[$value['Category']] = $value['Value'];
}
}
foreach ($result as $category => $value) {
print "<tr><td>$category</td><td>$value</td></tr>";
}

Categories