This is my php array $data.
Array
(
[upcoming] => Array
(
[webinars] => Array
(
[0] => Array
(
[webinarKey] => 123456
[subject] => webinar title ...
[times] => Array
(
[0] => Array
(
[startTime] => 2014-04-03T00:00:00Z
Please check my code below. I want to get values of "webinarkey" and "subject" and "start date - with date formatted" and put them into my select box. I can now populate "webinarkey and "subject". Thank you all to help to to populate "webinarkey and subject" but now i want to show "start date with date format as well.
echo '<form method="POST"><select>';
foreach ($data["upcoming"]["webinars"] as $webinar) {
echo '<option value="' . htmlspecialchars($webinar["webinarKey"]) . '">' . htmlspecialchars($webinar["subject"]) . htmlspecialchars($webinar["times"]["startTime"]) . '</option>';
}
echo '</select></form>';
Please help me to show start date as well.
$data = array(/**/);
foreach ($data["upcoming"]["webinars"] as $webinar) {
echo '<option value="' . htmlspecialchars($webinar["webinarKey"]) . '">' . htmlspecialchars($webinar["subject"]) . '</option>';
}
From what I gather the only array you need to loop over is $data["upcoming"]["webinars"].
Ensure your output HTML is valid and your data is escaped properly.
Since you are trying to access a value in a multidimensional array, you cannot use -> as that is for properties of objects. Instead you need to access it like a normal array: $array[key]
Updated Code:
echo '<select>';
foreach ($webinars as $obj)
{
foreach ($obj['webinars'] as $ob)
{
echo '<option value='.$ob['webinarKey'].'>'.$ob['subject'].'</option>';
}
}
echo '</select>';
You're trying to use properties, it's an array not an object.
You need to use $ob['----'] instead of $ob-> notation
echo '<select>';
foreach ($webinars as $obj)
{
foreach ($obj['webinars'] as $ob)
{
echo '<option value='.$ob['webinarKey'].'>'.$ob['subject'].'</option>';
}
}
echo '</select>';
Related
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
I have a data set being returned from a database and the keys are the names of the fields I need to print on a table.
For example, if I have a array of 5 keys (fields), I need to create an HTML table that had a heading with each of the keys on it. I would then need to print the results to the table.
Esentially, I need to create an HTML table from an array and the keys in the first result can act as the headers.
Here is my data:
SimpleXMLElement Object
(
[data] => SimpleXMLElement Object
(
[primaryFields] => SimpleXMLElement Object
(
[primary] => Array
(
[0] => SimpleXMLElement Object
(
[Project_Title] => Test
[Project_Description_Preview] => Test Desc
)
[1] => SimpleXMLElement Object
(
[Project_Title] => Blueprint Development Project
[Project_Description_Preview] => We are continuing to improve Blueprint as a tool by collecting feedback from our internal users and developing new features.
)
What would be the best way to go about getting the keys from the first result in the array? Should this be two separate loops; first one loops to create the headers from the keys and then the second one prints the data?
Project_Title & Project_Description_Preview would be the table headers in this case.
I will leave it to you to parse the data you get back. From the look of the object get to primary.
<?php
//You will need to parse the object to get this.
$primary = array(
array(
"Project_Title" => "Test",
"Project_Description_Preview" => "Test Desc"
),
array(
"Project_Title" => "Blueprint Development Project",
"Project_Description_Preview" => "We are continuing to improve Blueprint as a tool by collecting feedback from our internal users and developing new features."
)
);
echo "<table>";
for($i = 0; $i < count($primary); $i++){
//Only create the head on your first object
if($i==0){
echo "<thead>";
//build header here
echo "<tr>";
foreach($primary[$i] as $key => $value){
echo "<th>" . $key . "</th>";
}
echo "</tr>";
echo "</thead><tbody>";
}
//Then create the rows and data like all php...
echo "<tr>";
foreach($primary[$i] as $value){
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</tbody></table>";
?>
you have to browse your array from a certain place, say
$arr->data->primaryFields->primary as $primary.
considering the array from primary.
foreach($primary as $values){
foreach($values as $key => $ value){
echo $key." & ".$value;
}
}
My typical approach would be just to grab the keys when you first descend into the data on your first iteration, assuming you're looping over it.
Looping like so (as a bonus lets extract the keys just in case):
$primary = $obj->data->primaryFields->primary; //an array
$first = true;
$keys = array();
foreach ($primary as $obj) {
if ($first) {
$first = false;
$keys = get_array_keys((array)$obj));
make_headers($obj,$keys); //do header things here
}
make_table_stuff($obj,$keys); //do table stuff
}
But, you could always do:
$keys = get_object_vars($obj->data->primaryFields->primary[0]);
Note: this will work for any number of database columns, whether they are known or not.
You should be able to build the whole table like this:
//where $obj = your output...
//get the column names, convert to an object and store inside an array of one element
$keys = array(json_decode(json_encode(array_keys(get_object_vars($obj[0])))));
//create the output array by merging the keys as the first element with the existing dataset into a new array
$output = array_merge($keys, $obj);
//here is the output step
echo "<table><thead>";
foreach($output as $record_num => $record) {
echo '<tr>';
$cells = get_object_vars($record);
foreach ($cells as $column => $cell) {
echo '<td class="' . $column . '">' . $cell . '</td>';
}
echo '</tr>';
//if this is the first element of the array, it is the column names. After these are output, the rest should be in the tbody tag
if ($record_num == 0) { echo '</thead><tbody>';}
}
echo "</tbody></table>";
This also adds a class name to each cell that corresponds to its database column, which can be used for vertical css styling or javascript selectors after the fact for UI considerations. This can be removed if not needed though.
I have an HTML form that's submitting to a PHP script and send it as an email.
The problem I'm having is iterating the array and getting the output formatted into a table correctly.
My input fields look like this:
<input class="form-control" type="text" name="alternate[][qty]" />
<input class="form-control" type="text" name="alternate[][height]" />
I am iterating the array like this:
if ( isset( $_POST['alternate']))
{
foreach($_POST['alternate'] as $alt)
{
$message .= "<tr><td>" . $alt['qty'] . "</td><td>" . $alt['height'] . "</td></tr>";
}
}
I'm getting the correct values from the array but they are not formatted correctly. I am looking for an output something like this:
123 45
but instead it breaks across two rows like this:
How can I get both values on the same line?
EDIT:
Using
echo '<pre>';
print_r($_POST['alternate']);
echo '</pre>';
I get
Array
(
[0] => Array
(
[qty] => 54
)
[1] => Array
(
[height] => 5
)
[2] => Array
(
[qty] => 34
)
[3] => Array
(
[height] => 5
)
[4] => Array
(
[qty] => 36
)
[5] => Array
(
[height] => 45
)
...
)
which makes it look like I actually have 6 arrays...? That would explain why I'm getting each cell on a separate row, but I still don't understand how to fix it...
You're iterating through every element in $_POST['alternate'] and creating a row for each iteration. There are two elements, thus two rows.
There's no need to iterate since you already know which elements you'll get:
if ( isset( $_POST['alternate']))
{
$message = "<tr><td>{$_POST['alternate']['qty']}</td><td>{$_POST['alternate']['height']}</td></tr>";
}
Give this a shot, I hope that's what you're opting for.
if(isset($_POST['alternate']))
{
$message = "<tr>";
foreach($_POST['alternate'] as $alt)
{
if(isset($alt['qty']))
$message .= "<td>" . $alt['qty'] . "</td>";
elseif(isset($alt['height']))
$message .= "<td>" . $alt['height'] . "</td>";
}
$message .= "</tr>";
}
This gave me <tr><td>123</td><td>45</td></tr>.
EDITED SOLUTION
This script actually takes care of <tr> tags inside of the loop.
if(isset($_POST['alternate']))
{
foreach($_POST['alternate'] as $alt)
{
if(isset($alt['qty']))
$message .= "<tr><td>" . $alt['qty'] . "</td>";
elseif(isset($alt['height']))
$message .= "<td>" . $alt['height'] . "</td></tr>";
}
}
Try it out and let me know.
Your HTML actually declares separate array entries. You need to group them by defining keys for the array. Something like:
<input class="form-control" type="text" name="alternate[0][qty]" />
<input class="form-control" type="text" name="alternate[0][height]" />
Then the next group of fields uses "1" and so on.
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;
}
}
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;