Can't access multi dimensional associative arrays - php

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

Related

Getting individual values from multidimensional array

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

PHP Combine Arrays For Foreach Loop

How to combine two arrays for foreach loop.
I have two arrays for to be resulted in foreach loop.
Thank you in advanced for your help.
Primary Array:
Array
(
[0] => Array
(
[id] => 1
[name] => Grape
[date_created] => 2016-03-30 14:19:12
)
[1] => Array
(
[id] => 2
[name] => Coconut
[date_created] => 2016-03-30 14:22:54
)
--
Secondary Array:
Array
(
[0] => Array
(
[id] => 1
[fruit_id] => 1
[item_id] => 1
[ppk] => 0
[ppo] => 2342420
[image] => 6450983014191211.jpg
[url] =>
)
[1] => Array
(
[id] => 2
[fruit_id] => 1
[item_id] => 10
[ppk] => 343353
[ppo] => 0
[image] => 64509830141912110.jpg
[url] => http://yahoo.com
)
[2] => Array
(
[id] => 3
[fruit_id] => 2
[item_id] => 1
[date_created] => 2016-03-30 14:22:54
[date_last_change] => 2016-03-30 14:14:48
[ppk] => 0
[ppo] => 2323120
[image] => 6450983014225421.jpg
[url] =>
)
[3] => Array
(
[id] => 4
[fruit_id] => 2
[item_id] => 11
[date_created] => 2016-03-30 14:22:54
[date_last_change] => 2016-03-30 14:14:48
[ppk] => 232342000
[ppo] => 0
[image] => 64509830142254211.jpg
[url] => http://msn.com
)
[4] => Array
(
[id] => 5
[fruit_id] => 2
[item_id] => 12
[date_created] => 2016-03-30 14:22:54
[date_last_change] => 2016-03-30 14:14:48
[ppk] => 34343400
[ppo] => 0
[image] => 64509830142254212.jpg
[url] => http://fussball.com
)
Notes:
field "fruit_id" is taken from field of "id" in Primary Array
And the result:
//When I'm doing foreach loop, it should must result like this:
ID: 1
Fruit Name: Grape
Item ID: 1|10
PPK: 0|343353
PPO: 2342420|0
Image: 6450983014191211.jpg|64509830141912110.jpg
URL: ""|http://yahoo.com
------------------------------------------------------------------------
ID: 2
Fruit Name: Coconut
Item ID: 1|11|12
PPK: 0|232342000|232342000
PPO: 2323120|0|0
Image: 6450983014225421.jpg|64509830142254211.jpg|64509830142254212.jpg
URL: ""|http://msn.com|http://fussball.com
Please help.
Thank you in advanced.
So there are a few different things you need to use to get your expected output.
To get all related arrays from your second array for each id of your first array, you can use array_filter() to filter out exactly those subArrays.
Then when it comes down to printing out the data from the related arrays, you can use array_column() to get the specific data which you want to show from each subArray and implode() to convert it into a string.
Now if you want all empty values to be shown as "" you can quickly loop through the data which you want to print out with array_map() and just replace that.
And for the separator you can just check if it's the last element or not and if not print out the separator.
$last = count($firstArray) - 1;
foreach($firstArray as $k => $v){
$related = array_filter($secondArray, function($value)use($v){
return $value["fruit_id"] == $v["id"];
});
echo "ID: " . $v["id"] . PHP_EOL;
echo "Fruit Name: " . $v["name"] . PHP_EOL;
echo "Item ID: " . implode("|", array_column($related, "item_id")) . PHP_EOL;
echo "PPK: " . implode("|", array_column($related, "ppk")) . PHP_EOL;
echo "PPO: " . implode("|", array_column($related, "ppo")) . PHP_EOL;
echo "Image: " . implode("|", array_column($related, "image")) . PHP_EOL;
echo "Url: " . implode("|", array_map(function($v){return $v == "" ? '""' : $v;}, array_column($related, "url"))) . PHP_EOL;
if($k != $last)
echo PHP_EOL . "------------------------------------------------------------------------" . PHP_EOL . PHP_EOL;
}

PHP traversing a multidimensional array

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'];
}
}
}

Access json data without key value in php?

echo "<table title='mxit:table:full' style='width: 100%' width='100%'><colgroup span='2' width='50%'></colgroup>";
echo "<tr>";
echo "<td align='center;' style='color:black;'>Rank</td>";
echo "<td align='center;' style='color:black;'>Branch</td>";
echo "</tr>";
$find = array_slice($_SESSION["data"], $rank - 2, 5);
foreach($find as $key => $element)
{
echo "<tr>";
foreach($element as $subkey => $subelement)
{
echo $subelement;
if ($subkey++ < 2)
{
if ($subkey == 1)
{
echo "<td align='center;' style='color:black;'>$subelement</td>";
}
else
{
echo "<td align='center;' style='color:black;'><a href='getdata.php?key=$key'>" . $subelement . "</a></td>";
}
}
}
echo "</tr>";
}
echo "</table>";
I want to get the data using a key which in the sense is the rank the issue i am facing is i don't have a key in my data. I am using the rank as a key for full data display however in this case i cannot do that as my key is coming as 0 to 5 rather than say 7 to 12
My $find Array:
Array ( [0] => Array ( [0] => 7 [1] => KOTTAYAM TIEDAGENTSII [2] => V01393 - Venu M Nair [3] => R02208 - Reji Kumar [4] => 0.8854 [5] => 0.77570425252759 [6] => 1 [7] => 0.925 [8] => 0.9246 [9] => 1 [10] => 9.0926620166705 ) [1] => Array ( [0] => 8 [1] => PITAMPURA DLHI DIRECT [2] => C00405 - CHANDER MANI [3] => S00927 - Sachin Kumar [4] => 0.9478 [5] => 0.71204754186926 [6] => 0.94999999925494 [7] => 0.9845 [8] => 0.9692 [9] => 0.9400000013411 [10] => 9.0808534450678 ) [2] => Array ( [0] => 9 [1] => MUMBAI BHANDUP DIRECT [2] => B00841 - BHAVESH M KOTHARI [3] => A02234 - AMOL PRAKASH KANADE [4] => 0.9881 [5] => 1 [6] => 0.89000000059605 [7] => 0.9968 [8] => 1 [9] => 0.62000000476837 [10] => 9.0665850088513 ) [3] => Array ( [0] => 10 [1] => RATLAM DIRECT [2] => R01754 - Rajesh Joshi [3] => S03643 - Shishir Jain [4] => 1 [5] => 1 [6] => 0.74000000953674 [7] => 0.9779 [8] => 0.9902 [9] => 0.68999999761581 [10] => 8.9068650118017 ) [4] => Array ( [0] => 11 [1] => LUDHIANA DIRECT [2] => R02596 - RAKESH SHARMA [3] => J00753 - JAIPAL SINGH [4] => 1 [5] => 1 [6] => 0.72999998927116 [7] => 1 [8] => 0.9949 [9] => 0.65999999642372 [10] => 8.8850849763966 ) )
EDIT: My element array i want to make 7, 8 , 9 as the keys of the array such that i can use it effectively
Array(
[0] => 7[1] => KOTTAYAMTIEDAGENTSII[2] => V01393 - VenuMNair[3] => R02208 - RejiKumar[4] => 0.8854[5] => 0.77570425252759[6] => 1[7] => 0.925[8] => 0.9246[9] => 1[10] => 9.0926620166705
) Array(
[0] => 8[1] => PITAMPURADLHIDIRECT[2] => C00405 - CHANDERMANI[3] => S00927 - SachinKumar[4] => 0.9478[5] => 0.71204754186926[6] => 0.94999999925494[7] => 0.9845[8] => 0.9692[9] => 0.9400000013411[10] => 9.0808534450678
) Array(
[0] => 9[1] => MUMBAIBHANDUPDIRECT[2] => B00841 - BHAVESHMKOTHARI[3] => A02234 - AMOLPRAKASHKANADE[4] => 0.9881[5] => 1[6] => 0.89000000059605[7] => 0.9968[8] => 1[9] => 0.62000000476837[10] => 9.0665850088513
) Array(
[0] => 10[1] => RATLAMDIRECT[2] => R01754 - RajeshJoshi[3] => S03643 - ShishirJain[4] => 1[5] => 1[6] => 0.74000000953674[7] => 0.9779[8] => 0.9902[9] => 0.68999999761581[10] => 8.9068650118017
) Array(
[0] => 11[1] => LUDHIANADIRECT[2] => R02596 - RAKESHSHARMA[3] => J00753 - JAIPALSINGH[4] => 1[5] => 1[6] => 0.72999998927116[7] => 1[8] => 0.9949[9] => 0.65999999642372[10] => 8.8850849763966
)
<?php
$data=Array (Array (7,'KOTTAYAM TIEDAGENTSII'),
Array (8,'PITAMPURA DLHI DIRECT '),
Array (9,'PITAMPURA DLHI DIRECT1 '),
Array (10,'PITAMPURA DLHI DIRECT2 '),
Array (11,'PITAMPURA DLHI DIRECT3'),
);
print_r($data);
echo "<br/><br/>";
$formateddata;
foreach ($data as $key => $value) {
$tmp=$value[0];
$formateddata[$tmp]=$value;
}
print_r($formateddata);
echo "<table title='mxit:table:full' style='width: 100%' width='100%'><colgroup span='2' width='50%'></colgroup>";
echo "<tr>";
echo "<td align='center;' style='color:black;'>Rank</td>";
echo "<td align='center;' style='color:black;'>Branch</td>";
echo "</tr>";
//$find = array_slice(formateddata, $rank - 2, 5);
foreach($formateddata as $key => $element)
{
echo "<tr>";
foreach($element as $subkey => $subelement)
{
echo $subelement;
if ($subkey++ < 2)
{
if ($subkey == 1)
{
echo "<td align='center;' style='color:black;'>$subelement</td>";
}
else
{
echo "<td align='center;' style='color:black;'><a href='getdata.php?
key=$key'>" . $subelement . "</a></td>";
}
}
}
echo "</tr>";
}
echo "</table>";
?>
...copy the full code , and test in ur local server , hope u'll get the output as u wanted
I think before u do some operation just arrange the data the way you want , as u say like 7 to 12 .. here is the solution ,
<?php
$data=Array (Array (7,'KOTTAYAM TIEDAGENTSII'),
Array (8,'PITAMPURA DLHI DIRECT '),
Array (9,'PITAMPURA DLHI DIRECT '),
Array (10,'PITAMPURA DLHI DIRECT '),
Array (11,'PITAMPURA DLHI DIRECT '),
);
print_r($data);
echo "<br/><br/>";
$formateddata;
foreach ($data as $key => $value) {
$tmp=$value[0];
$formateddata[$tmp]=$value;
}
print_r($formateddata);
?>
You session $data coming as
Array (
[0] => Array (
[0] => 7
[1] => KOTTAYAM TIEDAGENTSII )
[1] => Array (
[0] => 8
[1] => PITAMPURA DLHI DIRECT )
)
now rearrange the $data as you like . For this example in $formateddata
Array (
[7] => Array (
[0] => 7
[1] => KOTTAYAM TIEDAGENTSII )
[8] => Array (
[0] => 8
[1] => PITAMPURA DLHI DIRECT
)
--hope now you got the array as u wanted ?

How to use array_push in 2D array?

I am having an array like this
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 6
[5] => 7
[6] => 8
[7] => 9
)
[4] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
[5] => 7
[6] => 8
[7] => 9
[8] => 10
[9] => 11
)
)
Now i want to put this into another array using array_push keyword...
How can i achieve this?
<?php
foreach($yourArray as $array) {
array_push($firstArray, $array);
}
?>
or
<?php
foreach($yourArray as $array) {
$firstArray[] = $array;
}
?>
or
<?php
array_push($firstArray, $array);
?>
$shiftedarray=array();
$aftershift=array();
foreach($twodarray as $key=>$val)
{
//Remove 0th index in array
$shiftedarray[]=array_shift($val);
//Array After Removed 0th index
$aftershift[]=$val;
}
echo "<pre>";
print_r($shiftedarray);
print_r($aftershift);
$oneDimensionalArray = call_user_func_array('array_merge', $aftershift);
print_r($oneDimensionalArray);

Categories