how to echo multidimension array using php - 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;
}
}

Related

How do I echo Nested Array Element?

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

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

foreach and multidimensional array

I have a multidimensional array and I want to create new variables for each array after apllying a function. I dont really know how to use the foreach with this kind of array. Here's my code so far:
$main_array = array
(
[first_array] => array
(
['first_array1'] => product1
['first_arrayN'] => productN
)
[nth_array] => Array
(
[nth_array1] => date1
[nth_arrayN] => dateN
)
)
function getresult($something){
## some code
};
foreach ($main_array as ["{$X_array}"]["{$key}"] => $value) {
$result["{$X_array}"]["{$key}"] = getresult($value);
echo $result["{$X_array}"]["{$key}"];
};
Any help would be appreciated!
foreach ($main_array as &$inner_array) {
foreach ($inner_array as &$value) {
$value = getresult($value);
echo $value;
}
}
unset($inner_array, $value);
Note the &, which makes the variable a reference and makes modifications reflect in the original array.
Note: The unset is recommended, since the references to the last values will stay around after the loops and may cause unexpected behavior if you're reusing the variables.
foreach($main_array AS $key=>$array){
foreach($array AS $newKey=>$val){
$array[$newKey] = getResult($val);
}
$main_array[$key] = $array;
}

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