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;
}
Related
How to read array posted from form repeater
the best is the array got from a form post..
Array (
[products] => Array (
[0] => Array (
[pid] => 1
[qty] => 2
)
[1] => Array (
[pid] => 1
[qty] => 2
)
[2] => Array (
[pid] => 1
[qty] => 2
)
)
)
my question is how to read value like this:
$pid[1]= 1
$qty[1] = 1
You can access the array values like this.
<?php
$array = array("products"=> [["pid" => 1 , "qty" => 2 ],
["pid" => 2 , "qty" => 3 ],
["pid" => 3 , "qty" => 4 ]]);
echo "PId: " . $array["products"][0]["pid"] . "<br>";
echo "qty: " . $array["products"][0]["qty"] . "<br><br>";
print_r($array);
?>
see the PHP fiddle for the working version
I want the values from this array for each index. I tried to access the values using foreach but I canĀ“t figure out how to get all values for each index (i.e. for 1st, 2nd, 3rd)
Array
(
[1] => Array
(
[0] => 244
[1] => 6014
[2] => 5067
[3] => 5059
[4] => 1726
[5] => 56
[6] => 1234
[7] => 56
)
[2] => Array
(
[0] => 122
[1] => 123
[2] => 56
[3] => 246
[4] => 7360
[5] => 8058
[6] => 1290
[7] => 1234
[8] => 5454
)
[3] => Array
(
[0] => 7890
[1] => 5454
)
)
Please try this. hopefully It will help u.
foreach ($array as $currencyId => $subArray){
echo $currencyId . "<br>";
foreach ($subArray as $value) {
echo $value . "<br>";
}
}
Follow this sample I made, if you still don't get it, leave a comment.
$array = array(
array('abc','def','xyz'),
array('asd','fun','bad'),
array('blue','red','green')
);
foreach($array as $key=>$value){
foreach($value as $subkey=>$subvalue) {
echo $subkey . " : " . $subvalue . "<br>";
}
}
If you want to get the currency pair key also, use this example:
$assoc_array = array(
"eurusd" => array('abc','def','xyz'),
"usdjpy" => array('good','bad','ugly'),
"aususd" => array('blue','red','green')
);
foreach($assoc_array as $key=>$value){
echo $key . ":: <br>";
foreach($value as $subkey=>$subvalue) {
echo $subkey . " : " . $subvalue . "<br>";
}
}
I want to show result this way
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/>";
}
}
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 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