Array ( [S10_2016] => Array ( [productName] => 1996 Moto Guzzi 1100i [productCode] => S10_2016 [MSRP] => 118.94 [quantity] => 1 ) )
print_r($_SESSION['shopping_cart']);
Above is my print r result. I have problem with displaying specific array value in this session. I want to get the value "productCode, MSRP and quantity" to store in a database after user click on checkout button.
Since you probably have no way of knowing that the keys are in $_SESSION['shopping_cart'] you need to iterate it using a foreach:
foreach ($_SESSION['shopping_cart'] as $product) {
$productCode = $product['productCode'];
$MSRP = $product['MSRP'];
$quantity = $product['quantity'];
// insert to database
}
You can try like below code. Here I placed key then placed productname, code etc...
## LIVE Example ##
$_SESSION['shopping_cart'] = array(
"S10_2016" =>array(
"productName"=> '1996 Moto Guzzi 1100i',
"productCode"=> 'S10_2016',
"MSRP"=> 118.94,
"quantity"=> 1
),
);
$body='';
foreach ($_SESSION['shopping_cart'] as $key => $val) {
$body .= $key . "--productName: " . $val['productName'] ."\r\n";
$body .= $key . "--productCode: " . $val['productCode'] ."\r\n";
$body .= $key . "--MSRP: " . $val['MSRP'] ."\r\n";
$body .= $key . "--quantity: " . $val['quantity'] ."\r\n";
}
echo $body;
?>
Related
I'm learning array and loop in php. But can't print the array with keys & values. How can I do this?
<?php
$marks = array (
"Alice" => array (
"physics" => "60",
"math" => "65"
),
"Bob" => array (
"physics" => "40",
"math" => "45"
)
);
foreach ( $marks as $key => $value) {
foreach ( $key as $key2 => $value2 ) {
echo $key . " : " . $key2 . " - " . $value2 . "<br>";
};
};
?>
In the nested foreach you have to iterate over the $value which holds the array.
foreach ( $marks as $key => $value) {
foreach ( $value as $key2 => $value2 ) {
// -------^^^^^^-------
echo $key . " : " . $key2 . " - " . $value2 . "<br>";
}
};
Use this
foreach ( $marks as $key => $value) {
foreach ( $value as $key2 => $value2 ) {
echo $key . " : " . $key2 . " - " . $value2 . "<br>";
}
}
This way it could be more readable to fix it and clear up confusion:
$marks = array(
'Alice' => array(
'physics' => 60,
'math' => 65,
),
'Bob' => array(
'physics' => 40,
'math' => 45,
),
);
// Loop students
foreach($marks as $name => $grades){
// Loop their grades
foreach ($grades as $subject => $score){
echo $name . ' : ' . $subject . ' - ' . $score . '<br>';
}
}
Please note that the numbers are without quotes. This will allow you to use them as numbers to do further calculations.
I have a multi-dimensional array as follows:
Array
(
[lists] => Array
(
[0] => Array
(
[id] => 23ybdwhdwbed
[name] => TEST
(
[1] => Array
(
[id] => e223edsewed
[name] => TEST 2
(
)
)
I want to access the ID & name variables using a foreach loop.
I'm using this:
$x = 0;
foreach($lists as $list){
$listId = $list[$x]['id'];
$listName = $list[$x]['name'];
echo"$x | $listId $listName <br />";
$x++;
}
For some strange reason, I can only get the value of the first $listId & $name, not the second $listId or $name.
What am I doing wrong here?
You're assuming that you still need to provide the key for each child element. This is not the case.
try
foreach($lists as $list){
$listId = $list['id'];
$listName = $list['name'];
$listId $listName <br />";
}
the foreach() will iterate over them in turn.
if you do need the index number, do this instead.
foreach($lists as $x => $list){
where $x is the index.
The array you posted is wrong because it's missing closing ), so correct that (I think that is TYPO mistake)
After that you need to do it like below:-
foreach($lists['lists'] as $key=> $list){
$listId = $list['id'];
$listName = $list['name'];
echo "$key | $listId $listName <br />";
}
Output:-https://eval.in/846464
Or an one-liner code:-
foreach($lists['lists'] as $key=> $list){
echo "$key | ".$list['id']." ".$list['name']." <br />";
}
Output:-https://eval.in/846465
Your foreach iterates the first, not the second level of your multi dimensional array.
Since the first level only holds the lists array as one and only element the loop only executes once.
Pass the lists key to the foreach instead like so:
$x = 0;
foreach($lists['lists'] as $list) {
echo "$x | " . $list['id'] . " " . $list['name'] . "<br />";
++$x;
}
Also note how in here I reference the list elements by name to make it easier to read.
I think those numerical indexed will just confuse you so try this instead:
$my_array = array(array("id" => "23ybdwhdwbed", "name" => "TEST"), array("id" => "e223edsewed", "name" => "TEST 2"));
To access the values: use:
foreach($my_array as $my_data){
echo "ID:" . $my_data["id"];
echo "<br>";
echo "NAME:" .$my_data["name"];
echo "<br><br>";
}
you just need to do:
foreach($lists['list'] as $listKey=>$listValue){
$listId = $listValue['id'];
$listName = $listValue['name'];
echo"$listKey | $listId : $listName <br />";
}
Try this, I fixed your array structure to work, this is also dynamic so it does not matter how many array you have 0 -> above
$array = array(
'lists' => array(
'0' => array(
'id' => '23ybdwhdwbed',
'name' => 'TEST 1'
),
'1' => array(
'id' => 'e223edsewed',
'name' => 'TEST 2'
)
)
);
foreach ($array as $key => $value) {
for($ctr = 0; $ctr < count($value); $ctr++){
echo 'ID: ' . $value[$ctr]['id'] . '<br>';
echo 'Name: : ' . $value[$ctr]['name'] . '<br><br>';
}
}
I would like the data to be echoed out in this format
[0] - [name][description]
[1] - [name][description]
[2] - [name][description]
$options = array('guide_info' => $guide_info);
$guide_info = array( 'guide_name' => $guide_name,
'guide_description' => $guide_description
);
I created two foreach loops to try and echo out the name and description of each, like this:
foreach ($options as $key => $value) {
foreach ($guide_info as $type => $info){
$html .= $type . " " . $info . "\n";
}
}
but I receive errors about invalid argument supplied for foreach() on the second loop.
Currently my print_r($options) shows
Array ( [guide_name] => f
[guide_description] => fff
[0] => Array (
[guide_name] => fsss
[guide_description] => sssss
)
)
and my echo prints
guide_name fsss
guide_description sssss
guide_name fsss
guide_description sssss
guide_name fsss
guide_description sssss
How would I be able to echo out the correct information that print_r is showing?
Use a recursive function to echo out the name and description values in the desired format.
function process_array($arr, $counter){
foreach($arr as $key => $value){
if(is_array($value)){
process_array($value, ++$counter);
}else{
if($key == "guide_name"){
echo "[" . $counter . "] - [" . $value . "][";
}else{
echo $value . "]<br />";
}
}
}
}
// Here $options is your original array
process_array($options, 0);
Output:
[0] - [f][fff]
[1] - [fsss][sssss]
$guide_info = array( 'guide_name' => 'guid name',
'guide_description' => 'guid description',
);
$options = array('guide_info' => $guide_info);
foreach ($options as $key => $value) {
foreach($value as $a => $b) {
echo $a," =",$b ;
}
}
or for print_r
foreach ($options as $key => $value) {
print_r($value) ;
}
Why are you putting the $guide_info in $options.If you want all the entries from $guide_info, you can do this-
for($i = 0; $i < count($guide_info); $i++){
$html .= $type . " " . $info . "\n";
}
-> considering $guide_info is a multidimensional array like = array( [0] => 'guide_name' => $guide_name,
'guide_description' => $guide_description)
If for some reason $guide_info IS important, I would suggest it to be an indexed array not assoc.. Hope you find the solution :)
After json_decode I have the following Array :::
$json = Array ( [name] => Array ( [0] => Peter [1] => David ) [dep] => Array ( [0] => accounts [1] => sales ) [date] => Array ( [0] => 10/27/2015 [1] => 09/25/2015 ) );
How can I group the values by key so I get the following in a PHP foreach loop ? :::
<ul>
<li>Peter, accounts, 10/27/2015</li>
<li>David, sales, 09/25/2015</li>
</ul>
I've tried a similar foreach loop ( see below ) without the desired results, I'm able to print all the key & value's but I'm not able to group values by key eg. [1] :::
foreach($json as $row){
foreach($row as $key=>$val){
echo $key . ': ' . $val . '<br>';
}
}
Any assistance would be appreciated :::
You could use the following code to sort the list:
<?php
$json = [
"name" => ["Peter","David"],
"dep" => ["accounts", "sales"],
"date" => ["10/27/2015","09/25/2015"]
];
$final = [];
foreach($json as $section)
{
foreach($section as $key=>$info)
{
if(!isset($final[$key])) $final[$key] = "";
$final[$key] .= $info . ", ";
}
}
echo "<ul>";
foreach($final as $row)
{
echo "<li>" . substr($row, 0, strlen($row) - 2) . "</li>"; // -2 to remove the extra ", "
}
echo "</ul>";
?>
Result:
<ul>
<li>Peter, accounts, 10/27/2015</li>
<li>David, sales, 09/25/2015</li>
</ul>
Perhaps try something like this:
$arrayCount = count($json['name']);
$output = '<ul>';
for($i = 0; $i < $arrayCount; $i++) {
$output .= '<li>';
$output .= $json['name'][$i] . ', ';
$output .= $json['dep'][$i] . ', ';
$output .= $json['date'][$i];
$output .= '</li>';
}
$output .= '</ul>';
echo $output;
You would also be better off using an array format like this:
$json =
Array(
Array(
'name' => 'Peter',
'dep' => 'accounts',
'date' => '10/27/2015'
),
Array(
'name' => 'David',
'dep' => 'accounts',
'date' => '09/25/2015'
)
);
This is because you can create a easier to understand foreach loop like this:
$output = '<ul>';
foreach($json as $row) {
$output .= '<li>';
$output .= $row['name'] . ', ';
$output .= $row['dep'] . ', ';
$output .= $row['date'];
$output .= '</li>';
}
$output .= '</ul>';
echo $output;
Looking for a little guidance as I don't know how to echo all my set options into an asana request. So I have an array from a user submitted form:
Array
(
[_wpcf7] => 5
[_wpcf7_version] => 4.1.2
[_wpcf7_locale] => en_US
[_wpcf7_unit_tag] => wpcf7-f5-o1
[_wpnonce] => 1531937d4e
[your-name] => TEST
[your-email] => test#test.com
[title] => test
[Description] => test
[venues] => Dragonfly
[duedate] => 2015-04-11
[options] => Array
(
[0] => Print Business Card (3.5x2)
[1] => Google Ads Package
[2] => Print Ticket (1.5x5.5)
[3] => Print Magazine Ad (8x10.75)
)
[_wpcf7_is_ajax_call] => 1
[upload] => ditch_fridays_logo.png
)
From there I parse the array to grab the variables:
$submittedBy = $posted_data['your-name'];
$eMail = $posted_data['your-email'];
$projectTitle = $posted_data['title'];
$projectDescription = $posted_data['Description'];
$venue = $posted_data['venues'];
$deadline = $posted_data['duedate'];
$options = $posted_data['options'];
$upload = $posted_data['upload'];
And then I create the asana task
// First we create the task
$result = $asana->createTask(array(
'workspace' => $workspaceId, // Workspace ID
'name' => $projectTitle, // Name of task
'assignee' => 'somewhere#somewhere.com', // Assign task to...
'due_on' => $deadline,
'notes' => 'Submitted By: ' . $submittedBy . "\n" . 'E-Mail: ' . $eMail . "\n\n" . '--------------------------------------------------' . "\n\n" . 'Task Name: ' . $projectTitle . "\n\n" . 'Description: ' . $projectDescription . "\n\n" . 'Venue: ' . $venue . "\n\n" . 'Deadline: ' . $deadline . "\n\n" . 'Attachments: http://inkentertainment.com/graphics/saved/' . $upload
));
The problem is, I cant include the selected options in the notes part of the task creation. How would I go about including all the options ($posted_data['options']) only if the user has selected them? (on the form there is a list of about 10, but the users will select 2 or 3)
I hope I have been clear enough for you to understand, if not let me know and I will try and clarify
$options = ( isset ( $posted_data[ 'options' ] ) &&
is_array( $posted_data[ 'options' ] ) ) ?
$posted_data[ 'options' ] : array();
here you'll have an array with zero or more elements.
If you need a string you may then use implode
http://php.net/manual/en/function.implode.php
ex: $options = implode( ', ', $options );