How do I echo Nested Array Element? - php

I don't know what I'm doing wrong, I can't get my array item to echo out. I keep getting this error:
Illegal string offset 'CreatedDatetime'.
My XML looks like this via JSON print_r:
Array
(
[ABOUT_VERSIONS] => Array
(
[ABOUT_VERSION] => Array
(
[CreatedDatetime] => 2019-10-22T22:29:47.7617229Z
[DataVersionIdentifier] => 201703
)
)
)
My code is:
foreach ($newArr["ABOUT_VERSIONS"]["ABOUT_VERSION"] as $item){
echo $item["CreatedDatetime"]."<br>";
}
This seems so simple but I'm having a block. I can't echo out the CreatedDatetime key.

$item is not an array, it iterates over the two values in $newArr['ABOUT_VERSION'] i.e. 2019-10-22T22:29:47.7617229Z and 201703. To display the CreatedDatetime, either access it directly:
echo $newArr["ABOUT_VERSIONS"]["ABOUT_VERSION"]["CreatedDatetime"] . "<br>";
or do a key comparison in the loop:
foreach ($newArr["ABOUT_VERSIONS"]["ABOUT_VERSION"] as $key => $value){
if ($key == "CreatedDatetime") echo $value . "<br>";
}
In both cases the output is 2019-10-22T22:29:47.7617229Z.
Demo on 3v4l.org

Related

php get the value of attribute name from array

I have an array stored in my database. So when I try : print_r($arrayname),
It showed the result like this:
Array
(
[0] => Color,Processor
[attribute_name] => Color,Processor
)
I want to get the values of [attribute_name] => Color,Processor .
So far I made the foreach loop like this :
foreach ($arraylist as $name) {
echo $name['attribute_name];
}
But it showing the result like cc. So can someone kindly tell me how to get the values from database?
Actually you don't need to use foreach
You can access it like this.
echo $arraylist["attribute_name"];
Please try this
foreach ($arraylist as $key => $name) {
if($key == 'attribute_name')
echo $name;
}

php get the text into an array list

I have an array in the database. When I used print_r($variable_name), I got the array like this
Array
(
[0] => Array
(
[attribute_name] => Disk space,Color,Processor
)
)
So to get the value of attribute_name I tried this
foreach($attributes_name as $key=>$val) {
echo $val['attribute_name'];
}
Here I got the result like Disk space,Color,Processor. But I want the result should come like a list
<li>Disk Space</li>
<li>Color</li>
<li>Processor</li>
So can someone tell me how to do this?
Try this :
<?php
$arr = array(array("attribute_name" => "Disk space,Color,Processor"));
foreach($arr as $val) {
$resultArr = explode(",",$val['attribute_name']);
foreach($resultArr as $value){
echo "<li>".$value."</li>";
// Add id in li
echo "<li id='".str_replace(" ","_", strtolower($value))."'>".$value."</li>";
}
}
?>

how to echo multidimension array using php

I have the following array, I need to display the names on a form.
How can I do this via foreach() loop?
What I am trying is:
Array
(
[0] => Array
(
[to_user_name] => John
)
[1] => Array
(
[to_user_name] => Mike
)
)
foreach( $myArray as $subArray ) {
echo $subArray["to_user_name"];
}
It's not clear how you want to use those values in your form, but just echo the values wherever you'd need them, e.g.,
foreach( $myArray as $subArray ) {
echo "<input type=\"text\" name=\"user_name\" value=\"" . $subArray["to_user_name"] . "\">";
}
I was treating it like a single array and then i realized it is a multidimensino array and the following worked, i hope this helps someone else too
foreach ($messages->getMessage('recipient_names') as $section => $items ){
foreach ($items as $key => $value){
echo "$value, ";
}
}
to see the content you can use
print_r($your_array);
For developing purpose you need to use for/foreach loop
foreach($your_array as $array_temp)
{
foreach($array_temp as $item)
{
echo $item;
}
}

getting information out of an array with key/value pairs

I have the following snippet of code that is creating the following array...
while ($stmt->fetch()) {
foreach($row as $key => $val) {
$x[$key] = $val;
}
$results[] = $x;
}
Results in the follow array:
Array ( [0] => Array ( [cap_login] => master [cap_pword] => B-a411dc195b1f04e638565e5479b1880956011badb73361ca ) )
Basically I want to extract the cap_login and cap_pword values for testing. For some reason I can't get it!
I've tried this kind of thing:
echo $results[$cap_login];
but I get the error
Undefined variable: cap_login
Can someone put me right here?
Thanks.
cap_login is in an array within $results so you would have to do $results[0]['cap_login']
You would have to do the following:
echo $x[0]['cap_login'] . '<br />';
echo $x[0]['cap_pword'];
The reson $results[$cap_login] wont work is because there isn't a variable called $cap_login, there is a string called cap login. In addition, there isn't a key in $results called $cap_login. There is a value in $results called 'cap_login'

Printing an stdClass object

I have:
$value = $wpdb->get_row("SELECT custom_message FROM `wp_wpsc_cart_contents` WHERE purchaseid='" . $purchase_log['id'] . "'");
if I do:
print_r($value);
I get:
stdClass Object
(
[custom_message] => |Castor Seed Oil $4.45|
)
So I tried to get that value doing:
foreach($value as $index => $result) {
echo $result["custom_message"];
}
I also tried:
foreach($value as $index => $result) {
echo $result->custom_message;
}
but that prints nothing, any idea what I'm doing wrong here?
The loop does nothing, you are iterating an object with a single property that you already know the name of. Just do this:
echo $value->custom_message;
No need for the for loop. Just do
echo $value->custom_message;

Categories