getting object properties and values recursivly - php

so i have an object:
$user->name->first = "Bob";
$user->name->last = "Smith";
$user->address->street = "1234 anywhere st.";
$user->address->city = "Chicago";
$user->address->state = "Texas";
how can i iterate through this object without knowing the "name,address" property?
I want to be able to do
foreach ($user as $key -> $value)
{}
now i can do this:
foreach ($user as $key -> $value)
{
foreach ($value as $k => $v)
{
echo $k . "," . $v . "\n";
}
}
and i get a nice little list of
first,Bob
last,Smith
street,1234 anywhere st.
city,Chicago
state,Texas
but how can i get the property name or address to print?
i.e.
name,first,Bob
name,last,Smith
address,street,1234 anywhere st.
address,city,Chicago
address,state,Texas

The value you want is in $key already from the first foreach loop.
foreach ($user as $key => $value)
{
foreach ($value as $k => $v)
{
echo "$key,$k,$v\n";
}
}
IDEOne.com demo

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

Add key and value to Zend MultiCheckbox dynamically

i have retrieved courseList and courseId from databse like this
foreach ($courses as $item) {
$checkBoxText = '';
$checkBoxText .= $item['courseRubric']. "-". $item['courseNumber']. " ". $item['courseTitle']. " [".$item['semester']. " " . $item['year']. "]";
$this->courseList[] = $checkBoxText;
$checkboxId = '';
$checkboxId .= $item['id'];
$this->courseId[] = $checkboxId;
}
Now, I want to add these array items to Zend_MultiCheckbox,
foreach ($this->courseId as $key => $value) {
$courseId[$value]= $value;
}
foreach ($this->courseList as $key => $value) {
$element->addMultiOptions(array(
$courseId[$key] => $value
));
}
This logic is not working.Can anyone suggest me how I can get
Course
Thanks
You have 2 solutions:
1 :
foreach ($this->courseList as $key => $value) {
$element->addMultiOption("$courseId[$key]", "$value");
}
2:
$opions = array();
foreach ($this->courseList as $key => $value) {
$options[$courseId[$key]] = $value;
}
$element->addMultiOptions($options);
I think, the 2nd is better.
Good luck.

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 .

Print contents of an array and their key in PHP

so currently I have a PHP function that prints out the applications configuration:
function cconfig() {
global $config;
// If the user wants debug information shown:
if (SHOWDEBUG == true) {
// If there is config information to show
if (isset($config)) {
cout("Variable $config is set.", "debug");
foreach ($config as $current) {
echo $current."<br/>";
}
} else {
cout("Variable $config isn't set.", "debug");
}
}
}
The output only shows the values of the array that it's at $current. For example:
Value1
Value2
Value3
How can I edit this function so instead of just showing the value at a given key, it also shows the key?
ConfigEntry1 = Value1
ConfigEntry2 = Value2
ConfigEntry3 = Value3
foreach ($config as $key => $current) {
echo "$key = $current<br />";
}
Expose the key like so:
foreach ($config as $k => $v) {
echo $k. ' ' . $v ."<br/>";
}
The two forms a foreach can take, from the manual:
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
The first form loops over the array
given by array_expression. On each
loop, the value of the current element
is assigned to $value and the internal
array pointer is advanced by one (so
on the next loop, you'll be looking at
the next element).
The second form does the same thing, except that the current element's key
will be assigned to the variable $key
on each loop.
Change:
foreach ($config as $current) {
echo $current."<br/>";
}
to
foreach ($config as $key => $val) {
echo $key." = " . $val . "<br/>";
}

How do I replace each item, without changing array's structure?

$array = array('lastname', 'email', 'phone');
How do I run foreach for this array and write updated value to its place?
Like:
foreach ($array as $item) {
// do something
// replace old value of this item with a new one
}
Example:
foreach ($array as $item) {
if (item == 'lastname')
$item = 'firstname';
// replace current $item's value with a 'firstname'
}
Array should become:
$array = array('firstname', 'email', 'phone');
An alternative is to loop by reference:
foreach ($array as &$value) {
$value = strtoupper($value);
}
unset($value);// <=== majorly important to NEVER forget this line after looping by reference...
In your example, a foreach loop would seem unnecessary if you know exactly what key maps to lastname, but if you need to you can still loop through the values. There are two very common ways: set the value according to the key:
foreach ($array as $key => $value) {
if ($value == 'lastname') {
$array[$key] = 'firstname';
}
}
Or (PHP 5 only) use references:
foreach ($array as &$item) {
if ($item == 'lastname') {
$item = 'firstname';
}
}
// Clean up the reference variable
unset($item);
You could do it like this:
foreach ($array as $key => $item) {
$array[$key] = "updated item";
}
foreach ($array as $i => $value) {
if ($value == 'lastname')
$array[$i] = 'firstname';
}

Categories