PHP :
<?php
print_r($details);
?>
Hi friends i'm printing the value of $details in php code and the out put i got in browser is as follows.
Array (
[0] =>
Array ( [item_id] => 8 [row] => [merchant_id] => 1 [discount] => [currentController] => store [price] => 60|Large [qty] => 1 [notes] => [cooking_ref] => Cooking reference 1
[ingredients] => Array ( [0] => Ingredients 1 ) [require_addon_5] => 2
[sub_item] => Array (
[5] => Array ( [0] => 2|10|Addon Item 1|right [1] => 3|20|Addon Item 2|right )
[6] => Array ( [0] => 2|10|Addon Item 1|right [1] => 3|20|Addon Item 2|right )
[7] => Array ( [0] => 2|10|Addon Item 1|right [1] => 3|20|Addon Item 2|right )
)
[addon_qty] => Array (
[5] => Array ( [0] => 1 [1] => 1 )
[6] => Array ( [0] => 1 [1] => 1 )
[7] => Array ( [0] => 1 [1] => 1 )
)
[require_addon_6] => 2
[require_addon_7] => 2
[two_flavors] =>
[non_taxable] => 2
[addon_ids] => Array ( [0] => 2 [1] => 3 [2] => 2 [3] => 3 [4] => 2 [5] => 3 )
)
)
Now I've to get the values of 'item_id' , 'cooking_ref' , sub_item and addon_qty as an individual array . Now I would like to know how can we access these values . I have tried something like
<?php
print_r($details);
echo "item_id : ".$details[0]['item_id']" <br />"
echo "price : ".$details[0]['price']" <br />"
echo "cooking_ref : ".$details[0]['cooking_ref']" <br />"
echo "cooking_ref : ".$details[0]['cooking_ref']" <br />"
echo "ingredients : "" <br />";
foreach($details['sub_item'] as $sub_item)
{
print_r($sub_item);
echo "<br /><br />";
$sub_item_array_val +=1;
}
?>
But didnt workedout for me could any one please suggest me how to acces these values from the above array , thanks in advance
Try this:
$details = // your array;
foreach($details as $detail)
{
echo $detail['item_id']; // will return 8
echo $detail['cooking_ref']; // will return Cooking reference 1
}
Foreach should be
foreach($details[0]['sub_item'] as $sub_item)
{
print_r($sub_item);
echo "<br /><br />";
}
Here is one possible approach-
foreach($details as $detail)
{
echo "item_id: ".$detail['item_id']."<br/>";
echo "price: ".$detail['price']."<br/>";
echo "cooking_ref: ".$detail['cooking_ref']."<br/>";
foreach($detail[$sub_item] as $sub_item)
{
echo "ingredients: <br/>";
print_r($sub_item);
echo "<br/><br/>";
}
foreach($detail[$addon_qty] as $addon_qty)
{
echo "ingredients: <br/>";
print_r($addon_qty);
echo "<br/><br/>";
}
}
Related
I have this array in a foreach-loop with the 'as' $train
Now i want to read out the 'mat'-node.
I've tried this:
<?php
echo "<!-- ";
foreach ($train['mat'] as $mat) {
echo "Mat:" . $mat . "";
}
echo " -->";
?>
But it gives a empty foreach result in mijn HTML-source, between the comments.
Array
(
[status] => 0
[via] => Utrecht C., Houten, Geldermalsen
[bestemming] => Tiel
[vleugels] => Array
(
[0] => Array
(
[stopstations] => Array
(
[0] => Array
(
[naam] => Vleuten
[code] => VTN
)
[1] => Array
(
[naam] => Utrecht Terwijde
[code] => UTT
)
[2] => Array
(
[naam] => Utrecht Leidsche Rijn
[code] => UTLR
)
)
[bestemming] => Tiel
[mat] => Array
(
[0] => Array
(
[0] => SLT-4
[1] => Tiel
[2] => 2422
)
[1] => Array
(
[0] => SLT-4
[1] => Tiel
[2] => 2464
)
)
)
)
[vervoerder] => NS
[spoor] => 6
)
Hope this help.
But I think you need read more about PHP Array and Array index
echo "<!-- ";
foreach($train['vleugels'][0]['mat'] as $mat) {
echo "Mat:".$mat[2]."<br>";
}
echo " -->";
// <!-- 2422 2464 -->
Hello guys i have this multiple array but i don't realy know how to access the values can i get all values of the company for example i will build an table with this values as shown bellow: is it possible to
build an nested while loop?
<?php
while (($company_name = current($aCompanys)) !== FALSE ){
echo key($aCompanys).'<br />';
next($aCompanys);
}
?>
[CompanyName1+] => Array (
[Zen] => Array (
[article] => Array (
[0] => Array (
[0] => Kalender
[1] => 9.99
[2] => 2017
)
[1] => Array (
[0] => Notizbuch DINA A4
[1] => 24.99
[2] => 2017
)
)
)
[PlenkS] => Array (
[article] => Array (
[0] => Array (
[0] => Kugelschreiber
[1] => 19.99
[2] => 2017
)
)
)
)
[CompanyName2] => Array (
[asdasd] => Array (
[article] => Array (
[0] => Array (
[0] => Kugelschreiber
[1] => 19.99
[2] => 2017
)
)
)
)
Since you want to print the company names as CompanyName1 : Zen , CompanyName2 : Plenks
Please find below complete code for your array, it works fine and gives the desired output.
<?php
$main_array = array("CompanyName"=>array("Zen"=>array("Article"=>array(array('kalendar','9.99','2017'),array('Notizbuch DINA A4','24.99','2017'))),"Plensk"=>array("Article"=>array(array('Kugelschreiber','9.99','2017')))),"CompanyName2"=>array("Zen2"=>array(1,2,3),"Plensk2"=>array(1,2,3)));
var_dump($main_array);
echo "<br /><br />";
foreach ($main_array as $i => $values){
foreach ($values as $key => $t){
echo $i . ":" . $key . "<br />";
}
}
?>
you can access them for example:
$aCompanys[CompanyName1+][Zen][article][0][2];
to get this value: "2017"
I have the following multidimensional array:
Array
(
[0] => 57950340
[1] => SALE-86
[2] => COMPLETE
[3] =>
[4] => 333
[5] => 819
[6] => Array
(
[0] => Array
(
[number] => 1
[product] => Array
(
[id] => 90316
[name] => CLASSIC COCKTAIL
)
[quantity] => 1
[price_variation] => 1
[modifiers] => Array( )
[notes] =>
[unit_price] => 16.3636
[unit_tax] => 1.63636
)
[1] => Array
(
[number] => 2
[product] => Array
(
[id] => 90316
[name] => CLASSIC COCKTAIL
)
[quantity] => 1
[price_variation] => 1
[modifiers] => Array ( )
[notes] =>
[unit_price] => 16.3636
[unit_tax] => 1.63636
)
)
)
I'm trying to loop through the array so that I can echo the name of the product items (held within the array at key 6 and echo each of these out on a separate line with the unit price and an the initial order ID (key 0 of the initial array).
I've been trying to do this for a few hours but am going round in very confusing circles, can anyone shed any light on how I can do this?
<?PHP
$multi_dimensional_array = [...]; // Your array here
$order_id = $multi_dimensional_array[0];
$products_array = $multi_dimensional_array[6];
foreach($products_array as $product) {
echo $product['product']['name']." costs ".$product['unit_price'];
echo " - ORDER: ".$order_id;
echo "<br/>";
}
?>
Useforeach and is_array() to check if the values is array then foreach to access the inside variables then lastly you can echo it.
foreach($array as $key => $val)
{
if(is_array($val){
foreach($val as $key2 => $val2)
{
//print_r($val2); to see the actual data you are accessing
echo "ID: " . $val2['product']['id']. ' Product Name: ' . $val2['product']['name'] . ' Quantity: ' . $val2['quantity'];
}
}
}
I have an array stored in $result like this
$result=$array
by using echo $result i get the following array
Array
(
[success] => 1
[product] => Array
(
[id] => 83
[seo_h1] =>
[name] => Beer Week
[manufacturer] => The Boxer Store
[model] => WPEB/0413/74/BW
[sku] => WPEB/0413/74/BW
[reward] => 0
[points] => 0
[image] => asd
[images] => Array
(
[0] => asd
)
[quantity] => 4
[price] => Rs.599
[special] =>
[discounts] => Array
(
)
[options] => Array
(
[0] => Array
(
[product_option_id] => 42
[option_id] => 25
[name] => Size Option
[type] => select
[option_value] => Array
(
[0] => Array
(
[product_option_value_id] => 165
[option_value_id] => 72
[name] => Large
[option_sku] =>
[image] => asd
[price] =>
[price_prefix] => +
)
[1] => Array
(
[product_option_value_id] => 166
[option_value_id] => 73
[name] => XL
[option_sku] =>
[image] => asd
[price] =>
[price_prefix] => +
)
[2] => Array
(
[product_option_value_id] => 163
[option_value_id] => 70
[name] => Small
[option_sku] =>
[image] => asd
[price] =>
[price_prefix] => +
)
[3] => Array
(
[product_option_value_id] => 164
[option_value_id] => 71
[name] => Medium
[option_sku] =>
[image] => asd
[price] =>
[price_prefix] => +
)
)
[required] => 1
)
)
[minimum] => 1
[rating] => 0
[description] => as
[attribute_groups] => Array
(
)
[date_added] => 2014-09-30 12:35:12
[date_modified] => 2014-10-17 17:33:46
[currency] => INR
[status] => 1
)
)
i am able to fetch the result individually like this
$finalid = $array['product']['id'];
echo $finalid;
but wish to fetch each and every data even the inner most values using for loop. for this i tried this
$c=count($result);
for ( $i=0; $i < $c; $i++)
{
echo $array[$i]['id'];
echo $array[$i]['images'][0];
echo $array[$i]['options'][0]['product_option_id'];
echo $array[$i]['images'][0]['option_value'][0]['product_option_value_id'];
echo $array[$i]['images'][0]['option_value'][1]['product_option_value_id'];
}
but it didn't displayed any result can anyone tell how it can be done
Use:
$products = $result['product'];
foreach ( $products as $product )
{
echo $product['id'];
echo $product['images'][0];
echo $product['options'][0]['product_option_id'];
echo $product['images'][0]['option_value'][0]['product_option_value_id'];
echo $product['images'][0]['option_value'][1]['product_option_value_id'];
}
You may want a recursive function like this to print your multidimensional arrays:
function printIt($data){
if (is_array($data)){
foreach ($data as $index=>$slice){
if (is_array($slice)){
printIt($slice);
}else{
echo $index.": ".$slice."<br>";
}
}
}else{
echo $data."<br>";
}
}
printIt($yourArray);
Use foreach instead,
foreach ( $array['product'] as $product)
{
echo $product['id'];
echo $product['images'][0];
echo $product['options'][0]['product_option_id'];
echo $product['options'][0]['option_value'][0]['product_option_value_id'];
echo $product['options'][0]['option_value'][1]['product_option_value_id'];
}
Here you will be looping inside each element of $array['product'] and for each loop $product will have the current element of $array['product'].
The mistake in your attempt was in [$i]. For each loop $i would have 0,1,2 etc and the array index would be $array['product'][0],etc but there is no element of that sort. Your method works if your array is an Indexed array, but here it is an Associative Array.
Try this,
<?php
foreach($result as $res){
echo '<pre>'; echo $res['id'];
if(is_array($res['options']) && !empty($res['options'])) {
foreach($res['options'] as $option) {
echo '<pre>'; echo ($option['product_option_id']);
if(is_array($option['option_value']) && !empty($option['option_value'])) {
foreach($option['option_value'] as $option_value) {
echo '<pre>'; echo $option_value['product_option_value_id'];
}
}
}
}
?>
I have the following code:
foreach ($cardSuits as $cardSuit) {
$keyCardValues = array_keys($cardValues);
foreach ($keyCardValues as $cardValue) {
$deck[] = array( "cardValue" => $cardValue, "cardSuit" => $cardSuit);
shuffle($deck);
}
}
if ($deal == "Deal") {
shuffle($deck);
$cards1 = array_shift($deck);
$_SESSION['value'][] = $cards1;
I've tried:
echo "<br />" . $_SESSION['value']['cardValue'];
But it's giving me an undefined index error. However, if I do a print_r, it works fine..
How do I echo it so the session can give me the $cardValue in the array?
Thanks
edit for print_r:
Array ( [value] => Array ( [0] => Array ( [cardValue] => nine [cardSuit] => hearts ) ) [cards] => Array ( [0] => 9 [1] => 2 [2] => 10 [3] => 4 [4] => 3 [5] => 10 [6] => 5 [7] => 2 [8] => 10 [9] => 5 ) )
EDIT for echo print_r:
Array ( [value] => Array ( [0] => Array ( [cardValue] => nine [cardSuit] => diamonds ) ) [cards] => Array ( [0] => 9 [1] => 3 [2] => 7 [3] => 10 [4] => 9 [5] => 11 [6] => 7 [7] => 10 [8] => 10 [9] => 5 ) )
try
echo "<br />" . $_SESSION['value'][0]['cardValue'];
Since your $cards1 is an array and that you are assigning this array to $_SESSION['value'][], you want to access cardValue using the following:
echo "<br />" . $_SESSION['value'][0]['cardValue'];
Use this
echo "<br />" . $_SESSION['value'][0]['cardValue'];
Array value is inside the index 0