How can I access individual values in an associative array in PHP? - php

I am pulling elements from a standard class object into an assoc array like so:
$array = $subjects;
foreach ( $array as $subject ) {
foreach ( $subject as $prop => $val ) {
if ( $val !== '' ) {
echo $prop . ' = ' . $val;
echo "<br>";
}
}
}
I get the result I expect from above, except what I'd like to do is echo out individual values into a table.
When I do this:
echo $subject['day1'];
I get this: "Cannot use object of type stdClass as array."
Where am I going wrong? Thanks in advance.

If it's using StdClass you'll need to do this:
$subject->day1
If you want to convert it to an array, have a look at this question: php stdClass to array

You are trying to iterate over the $array and $array is still an object. Use this:
$vars = get_object_vars($subjects);
to get assoc. array from the object $subjects. Then go:
foreach ($vars as $name => $value) {
echo $name . "=" . $value;
echo "<br>";
}

Related

How to get the value of specific object in PHP

I have a result array which looks like this:
Array
(
[formdata] => [{"id":"1"},{"name":"name"}]
)
Now I want to get the value of id but I can't get it right.
The code I've done so far is:
foreach ($text as $key => $file) {
print_r($file['id']); //<--- it shoud print 1
}
$var2 = json_decode($var['formdata'], true);
echo $var2[0]['id'];
Simple Example to what Babak commented:
$var['formdata'] = '[{"id":"1"},{"name":"name"}]';
$var2 = json_decode($var['formdata']);
foreach($var2 as $item) {
echo $item->id;
}

JSON and Arrays mixed up (in PHP)

This is what I get from my SQL selection. The data is correct, now I'd like to echo it by foreach.
Array ( [0] => stdClass Object ( [sql_column] => [{"1":"value1", "2":"value2", "3":"value3"}] ) )
What I've tried (and which didn't work) was:
$obj = json_decode($arr);
foreach($obj as $data){
echo $data->sql_column->1; //this should echo "value1", but it doesn't
}
Does anyone see my mistake? Thanks in advance!
If $arr is the array you posted then you can't json_decode it since it's an array and not a JSON string.
//First you need to get the string
$json = $arr[0]->sql_column;
//Then decode
$data = json_decode($json);
//Then loop
foreach($data as $key => $value){
echo "$key = $value \n";
}
Your json seems to be complicated (double dimensional array), but you can fetch values from it in this manner:
foreach($arr as $data){
$json = json_decode($data->sql_column);
$temp = (array)$json[0];
foreach($temp as $k=>$v){
print($v."<br/>");
}
}
I hope you get an idea...
I used foreach for $arr to cover multi values, you can omit that if you want:
$data=$arr[0];
$json = json_decode($data->sql_column);
$temp = (array)$json[0];
foreach($temp as $k=>$v){
print($v."<br/>");
}

Arrays and nested foreach

I have two array output (using preg_match_all), for example: $name[1] and $family[1].
i need to put these arrays together, and i use foreach as this:
foreach( $name[1] as $name) {
foreach( $family[1] as $family) {
echo $name.$family.'<br />';
}
}
but it don't work.
(each foreach loop works separately)
Assuming they have matching keys for the loop:
foreach( $name as $key => $value) {
echo $value[$key] . $family[$key] . '<br />';
}
This will go through each match for $name and print it out, and then print out the corresponding $family with it. I don't think you want to hardcode [1].
If you do, I'm a little confused and would like to see a var_dump of both $name and $family for clarification.
$together= array();
foreach( $name as $key => $n) {
$tuple= array('name'=>$name[$key],'family'=>$family[$key]);
$together[$key]= $tuple;
}
foreach( $together as $key => $tuple) {
echo "{$tuple['name']} {$tuple['family']}<br />";
}
Use array_combine():
Creates an array by using the values from the keys array as keys and
the values from the values array as the corresponding values.
PHP CODE:
$nameFamilly=array_combine($name[1] , $family[1]);
foreach( $nameFamilly as $name=>$familly) {
echo $name.$family.'<br />';
}

Can I display the array key and value without using foreach?

I don't like to use the foreach for every little array, I hear'd it is also possible to replace it with some array_ function, but I forget the function name.
This is what I wan't to reach:
$down = array (
"file.rar" => $l->get()->fileDesc(),
"file1.rar" => $l->get()->clientDesc()
);
normally, I'm using this to display the data:
foreach ( $down as $key => $value )
$data .= $key . ' = ' . $value . '<br/>';
So it'll return:
//echo $data;
file.rar = File Description One
file1.rar = Client Description
It there a way to prevent from using the foreach and display the same $data anyway?
I'm just curious, so please be nice.
Why don't you just write your own function?
function print_array($down) {
$data = '';
foreach ( $down as $key => $value )
$data .= $key . ' = ' . $value . '<br/>';
return $data;
}
You can use print_r with true on the second parameter, but I would suggest to use a foreach (as I say in my comment)
<?php
$data = print_r($down, true);
?>
well the only time you would need to use a foreach is for a dynamic array. if thats not the case, just use isset to find out what is set. you can also use count if your just trying to figure out how big an array is.

get all values from php associative array

I have an array $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);. How can i get (echo) all names and corresponding ages in a single instance (like foreach $value as $value)?? The array may have more data than shown here.
foreach ($ages as $name => $age) {
echo "$name = $age\n";
}
u have a lot options
like
print_r
print_r($array)
var_dump , foreach
print_r($array);
or
var_dump($array)
foreach ($ages as $key => $value) {
echo $key . "'s age is " . $value . "<br />";
}

Categories