How to read array posted from form repeater - php

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

Related

create array from multiple arrays & submit in mysql table

I am trying to fetch values from few - dynamically created HTML FORM & in action file, i am grabbing those values via $_POST. Please consider humbly, that i am using array in dynamic inputs like :
<input name="array1[]" />
<input name="array2[]" />
<input name="array3[]" />
So, that, after FORM SUBMIT, in action file, its giving :
$a = Array
(
[0] => 04/21/2017
[1] => 04/19/2017
[2] => 04/25/2017
[3] => 04/25/2017
[4] => 10/25/2017
)
$b= Array
(
[0] => 11
[1] => 34
[2] => 12
[3] => 12
[4] => 2
)
$c= Array
(
[0] => fghgthg
[1] => ggfg
[2] => fgfgfdgf
[3] => fgfdgdgfgdh
[4] => rgrgfgf
)
Now, m having troubles in arranging this way :
$FinalArray = array(
array($a[0], $b[0], $c[0]),
array($a[1], $b[1], $c[1]),
array($a[2], $b[2], $c[2]),
array($a[3], $b[3], $c[3]),
......last line without comma
);
And submitting in mysql table, so that i can easily retrieve it like :
step-1 : $FinalArray[0];
step-2 : $FinalArray[1];
.......goes on
Thanks, for help, in Advance.
This simple foreach help you in achieving your desired output.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$a = Array
(
0 => "04/21/2017",
1 => "04/19/2017",
2 => "04/25/2017",
3 => '04/25/2017',
4 => "10/25/2017"
);
$b = Array
(
0 => 11,
1 => 34,
2 => 12,
3 => 12,
4 => 2
);
$c = Array
(
0 => "fghgthg",
1 => "ggfg",
2 => "fgfgfdgf",
3 => "fgfdgdgfgdh",
4 => "rgrgfgf",
);
$result=array();
foreach($a as $key => $value)
{
$result[]=array($value,$b[$key],$c[$key]);
}
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => 04/21/2017
[1] => 11
[2] => fghgthg
)
[1] => Array
(
[0] => 04/19/2017
[1] => 34
[2] => ggfg
)
[2] => Array
(
[0] => 04/25/2017
[1] => 12
[2] => fgfgfdgf
)
[3] => Array
(
[0] => 04/25/2017
[1] => 12
[2] => fgfdgdgfgdh
)
[4] => Array
(
[0] => 10/25/2017
[1] => 2
[2] => rgrgfgf
)
)

How to group posted array names by index in PHP

I am currently working on a project where the fields scale when clicking on the "Add" button.
I am grouping each field like this: name="packaging[]", name="packaging[1]", name="packaging[2]" and so on. When I submit the form, this is how the data looks like when posted:
Array
(
[packaging] => Array
(
[0] => 1
[1] => 2
)
[quantity] => Array
(
[0] => 1
[1] => 2
)
[total-weight] => Array
(
[0] => 1
[1] => 2
)
[length] => Array
(
[0] => 1
[1] => 2
)
)
Using PHP I would like to convert the above code to look like this:
Array
(
[0] => Array
(
[packaging] => 1,
[quantity] => 1,
[total-weight] => 1,
[length] => 1,
)
[1] => Array
(
[packaging] => 2,
[quantity] => 2,
[total-weight] => 2,
[length] => 2,
)
)
Any help would be greatly appreciated.
Try this....
$array=array();
foreach($data as $key=>$value){
foreach($value as $k=>$val){
$array[$k][$key]=$val;
}
}
DEMO
Try this code:
$rows = array ('packaging' => array ('0'=> 1,'1' => 2),'quantity' => array('0'=> 1,'1' => 2),'total-weight' => array ('0'=> 1,'1' => 2),
'length' =>array ('0'=> 1,'1' => 2)
);
$res_array = array();
$total_records = count($rows['packaging']);
for($i=0;$i<$total_records;$i++)
{
$res_array[] = array('packaging'=>$rows['packaging'] [$i],'quantity'=>$rows['quantity'][$i],
'total-weight'=>$rows['total-weight'][$i],'length'=>$rows['length'] [$i]);
}
print_r($res_array);

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

Can't access multi dimensional associative arrays

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

Categories