How to get specific array value in php - php

I want to output a specific value from array in php
Below is my code and array is $content
<?php
$content = $_POST;
for($i=1; $i < $content['itemCount'] + 1; $i++) {
$name = 'item_name_'.$i;
$quantity = 'item_quantity_'.$i;
$price = 'item_price_'.$i;
$image='item_image_'.$i;
$option='item_options_'.$i;
$total = $content[$quantity]*$content[$price];
}
?>
<?php
print_r( $content );
?>
Output is showing as below:
Array ( [currency] => INR
[shipping] => 0
[tax] => 0
[taxRate] => 0
[itemCount] => 3
[item_name_1] => Our Nest
[item_quantity_1] => 1
[item_price_1] => 1900
[item_options_1] => image: CF01108.jpg, productcode: 602793420
[item_name_2] => Our Nest
[item_quantity_2] => 1
[item_price_2] => 2100
[item_options_2] => image: CF01110.jpg, productcode: 123870196
[item_name_3] => Our Nest
[item_quantity_3] => 1
[item_price_3] => 1800
[item_options_3] => image: CF01106.jpg, productcode: 416267436 )
How to get productcode value in a php variable and echo it?
example:
602793420, 123870196, 416267436

You can get the productcode using explode() function, like this,
$product_code = explode("productcode: ", $option)[1];
Here's the reference:
explode()
So your code should be like this:
<?php
$content = $_POST;
for($i=1; $i < $content['itemCount'] + 1; $i++) {
$name = 'item_name_'.$i;
$quantity = 'item_quantity_'.$i;
$price = 'item_price_'.$i;
$image='item_image_'.$i;
$option='item_options_'.$i;
$product_code = explode("productcode: ", $option)[1];
$total = $content[$quantity]*$content[$price];
}
?>

I would rather suggest this, in case the option of the item will have more values in future.
$option = "image: CF01110.jpg, productcode: 123870196";
$options = explode(",", $option);
echo $product_code = explode("productcode: ", $options[1])[1];
Thanks
Amit

Related

How to merge 3 arrays keeping their meta key?

I'm Getting some arrays from some wordpress custom fields:
$content = array(get_post_meta($postId, 'content'));
$media = array(get_post_meta($postId, 'media'));
$yt = array(get_post_meta($postId, 'youtube'));
I then need to have it printing in sequence, like:
media
content
LInk
Embed
And repeat the sequence for each value
media
content
LInk
Embed
For the sequence I'd use this:
echo '<ul>';
for ($i = 0; $i < count($all_array['media']); $i++) {
for ($j = 0; $j < count($all_array['content']); $j++) {
for ($k = 0; $k < count($all_array['youtube']); $k++) {
echo '<li>media->' . $all_array['media'][$i] . '</li>';
echo '<li>content->' . $all_array['content'][$j] . '</li>';
echo '<li>link->' . $all_array['link'][$k] . '</li>';
}
}
}
echo '</ul>';
But I'm doing something wrong with the merging of the 3 fields as if I do a var_dump before to run the for bit, like
echo '<pre>' . var_export($all_array, true) . '</pre>';
Then this is what I get and I cannot iterate as I wish:
array (
0 =>
array (
0 =>
array (
0 => '
brother
',
1 => '
Lorem
',
2 => '
End it
',
),
1 =>
array (
0 => '337',
1 => '339',
),
2 =>
array (
0 => 'https://www.youtube.com/watch?v=94q6fzbJUfg',
),
),
)
Literally the layout in html that I'm looking for is:
image
content
link
image
content
link
...
UPDATE
This how I am merging the arrays:
foreach ( $content as $idx => $val ) {
$all_array[] = [ $val, $media[$idx], $yt[$idx] ];
}
This is the associative array how it looks like:
Content:
array (
0 =>
array (
0 => '
brother
',
1 => '
Lorem
',
2 => '
End it
',
),
)
Media
array (
0 =>
array (
0 => '337',
1 => '339',
),
)
Youtube
array (
0 =>
array (
0 => 'https://www.youtube.com/watch?v=94q6fzbJUfg',
),
)
The way I've resolved it is first I calculate the tot. items within each array, then I get the array with max items and loop and add the items in sequence:
//GET CUSTOM FIELDS
$content = get_post_meta($post_to_edit->ID, 'content', false);
$media = get_post_meta($post_to_edit->ID, 'media', false);
$yt = get_post_meta($post_to_edit->ID, 'youtube', false);
$max = max(count($content), count($media), count($yt));
$combined = [];
//
// CREATE CUSTOM FIELDS UNIQUE ARRAY
for($i = 0; $i <= $max; $i++) {
if(isset($media[$i])) {
$combined[] = ["type" => "media", "value" => $media[$i]];
}
if(isset($content[$i])) {
$combined[] = ["type" => "content", "value" => $content[$i]];
}
if(isset($yt[$i])) {
$combined[] = ["type" => "youtube", "value" => $yt[$i]];
}
}
Finally I can loop:
foreach ($combined as $key => $val) {
if($val['type'] === "media") {
...
}
if($val['type'] === "content") {
...
You don't need to merge the arrays together. It will work fine in separate arrays. However, your for loops don't have the right logic. Try:
for ($i = 0; $i < count($media); $i++) {
for ($j = 0; $j < count($media[$i]); $j++) {
echo '<li>media->' . $media[$i][$j] . '</li>';
}
for ($j = 0; $j < count($content[$i]); $j++) {
echo '<li>content->' . $content[$i][$j] . '</li>';
}
for ($j = 0; $j < count($youtube[$i]); $j++) {
echo '<li>link->' . $youtube[$i][$j] . '</li>';
}
}

Pointer of Array conditional sum qty

I have a question about how to make an iteration. I want to place a total row after each item in the array if the next element in the array matches a specific condition. Spesific conditions have logic like this
the data like this
if i request a qty for example = 60 the result i hope like this
you can see
data[2] = 01/03/2020 just took 10 out of 40
$iter = new \ArrayIterator($values);
$sum = 0;
foreach($values as $key => $value) {
$nextValue = $iter->current();
$iter->next();
$nextKey = $iter->key();
if(condition) {
$sum += $value;
}
}
dd($iter);
how to make this logic work on php language/ laravel?
Following logic might help you on your way:
<?php
$stock = [
'01/01/2020' => 20,
'01/02/2020' => 30,
'01/03/2020' => 40
];
showStatus($stock, 'in stock - before transaction');
$demand = 60;
foreach ($stock as $key => $value) {
if ($value <= $demand) {
$stock[$key] = 0;
$supplied[$key] = $value;
$demand -= $value;
} else {
$stock[$key] -= $demand;
$supplied[$key] = $value - ($value - $demand);
$demand = 0;
}
}
showStatus($supplied, 'supplied');
showStatus($stock, 'in stock - after transaction');
function showStatus($arr = [], $msg = '')
{
echo $msg;
echo '<pre>';
print_r($arr);
echo '</pre>';
}
?>
**Output:**
in stock - before transaction
Array
(
[01/01/2020] => 20
[01/02/2020] => 30
[01/03/2020] => 40
)
supplied
Array
(
[01/01/2020] => 20
[01/02/2020] => 30
[01/03/2020] => 10
)
in stock - after transaction
Array
(
[01/01/2020] => 0
[01/02/2020] => 0
[01/03/2020] => 30
)
Working demo
I'm not sure I've understood you correctly but this might help:
$values = [
'01/01/2020' => 20,
'01/02/2020' => 30,
'01/03/2020' => 40
];
$demand = 60;
$total = array_sum($values);
$decrease = $total - $demand; //(20+30+40) - 60 = 30
$last_key = array_keys($values,end($values))[0]; //Is 01/03/2020 in this case
$values[$last_key] -= $decrease; //Decrease value with 30 calulated above
Would output:
Array
(
[01/01/2020] => 20
[01/02/2020] => 30
[01/03/2020] => 10
)

create block by block array from foreach loop

Hi I am trying to print multiple array with selected block e.x. 2/3
But I am not getting exact result. I need some help.
Here is my program
<?php
$process_block = 2;// this is the block
$args = array(
0=> 16083,
1=> 16090);
$user_id_start = $args[0];
$user_id_end = $args[1];
$end_page = ($user_id_end - $user_id_start)/$process_block ;
if ($end_page > floor($end_page)){
$end_page = floor($end_page)+1;
}
for($i=1; $i<=$end_page; $i++){
if($i==$end_page){
$id_from = ($user_id_start + ($i-1) * $process_block + 1);
$id_to = $user_id_end;
}elseif($i==1){
$id_from = $user_id_start;
$id_to = $user_id_start + $i * $process_block;
}else{
$id_from = ($user_id_start + ($i-1) * $process_block + 1);
$id_to = $user_id_start + $i * $process_block;
}
$param['id_from'] = isset($id_from) ? $id_from : '';
$param['id_to'] = isset($id_to) ? $id_to : '';
print_r($param);
}
?>
And the output it is producing:
Array
(
[id_from] => 16083
[id_to] => 16085
)
Array
(
[id_from] => 16086
[id_to] => 16087
)
Array
(
[id_from] => 16088
[id_to] => 16089
)
Array
(
[id_from] => 16090
[id_to] => 16090
)
My expected array should like this may be. with 2 difference between numbers
Array
(
[id_from] => 16083
[id_to] => 16085
)
Array
(
[id_from] => 16086
[id_to] => 16088
)
Array
(
[id_from] => 16089
[id_to] => 16090
)
Fiddle
I thought I'd whip this up in a jiffy. But this was maybe version... 4? I feel like there should be a simpler solution, but I sure didn't find it. It should handle changes in the size of your range too.
<?php
$process_block = 3;// this is the block
$args = array(
0=> 16083,
1=> 16090);
$user_id_start = $args[0];
$user_id_end = $args[1];
$diff = $user_id_end - $user_id_start;
$pages = ceil(($user_id_end - $user_id_start) / $process_block);
for($i=0; $i<=$pages; $i++){
if (($user_id_start+$i*$process_block)>$user_id_end)break;
echo $i.'--'.($user_id_start+$i*$process_block).':::';
$param['id_from'] = $i*$process_block+$user_id_start;
$page_end = ($i+1) * $process_block+$user_id_start-1;
$param['id_to'] = $page_end>$user_id_end ? $user_id_end : $page_end;
print_r($param);
}
?>

proper way of passing more than one data in array params: php

This is my foreach
foreach( $artist_price as $paypal_email => $price ) {
// Calculation.
$paypal_id_array[] = $paypal_email;
$commission = ($commission_percent/100) * $price;
$remaining_price = $price - $commission;
$remaining_price_array[] = $remaining_price;
}
This is how i am passing my values:
// Params.
$bodyParams = array(
"receiverList.receiver(1).amount" => $remaining_price_array[0],
"receiverList.receiver(2).email" => $paypal_id_array[1],
"receiverList.receiver(2).amount" => $remaining_price_array[1],
"receiverList.receiver(3).email" => $paypal_id_array[2],
"receiverList.receiver(3).amount" => $remaining_price_array[2],
"receiverList.receiver(4).email" => $paypal_id_array[3],
"receiverList.receiver(4).amount" => $remaining_price_array[3],
"receiverList.receiver(5).email" => $paypal_id_array[4],
"receiverList.receiver(5).amount" => $remaining_price_array[4],
"receiverList.receiver(6).email" => $paypal_id_array[5],
"receiverList.receiver(6).amount" => $remaining_price_array[5],
"receiverList.receiver(7).email" => $paypal_id_array[6],
"receiverList.receiver(7).amount" => $remaining_price_array[6],
"receiverList.receiver(8).email" => $paypal_id_array[7],
"receiverList.receiver(8).amount" => $remaining_price_array[7]
);
Is there any proper way to add "receiverList.receiver(8).email" and "receiverList.receiver(8).amount" how much it contains in the foreach loop. e.g: if there are only two merchants than just it goes upto 2 merchants. the way i am doing is static and it also gives error of undefined values. although the code is running.
You can do everything in the foreach loop:
$bodyParams = array();
$count = 1;
foreach( $artist_price as $paypal_email => $price ) {
// Calculation.
$commission = ($commission_percent/100) * $price;
$remaining_price = $price - $commission;
$bodyParams["receiverList.receiver($count).email"] = $paypal_email;
$bodyParams["receiverList.receiver($count).amount"] = $remaining_price;
$count++;
}

Pulling $_GET data and creating multidimensional array using loop

I'm creating a checkout for customers and the data about what's in their cart is being sent to a page (for just now) via $_GET.
I want to extract that data and then populate a multidimensional array with it using a loop.
Here's how I'm naming the data:
$itemCount = $_GET['itemCount'];
$i = 1;
while ($i <= $itemCount) {
${'item_name_'.$i} = $_GET["item_name_{$i}"];
${'item_quantity_'.$i} = $_GET["item_quantity_{$i}"];
${'item_price_'.$i} = $_GET["item_price_{$i}"];
//echo "<br />Name: " .${'item_name_'.$i}. " - Quantity: " .${'item_quantity_'.$i}. " - Price: ".${'item_price_'.$i};
$i++;
}
From here I'd like to create a multidimensional array like such:
Array
(
[Item_1] => Array
(
[item_name] => Shoe
[item_quantity] => 2
[item_price] => 40.00
)
[Item_2] => Array
(
[item_name] => Bag
[item_quantity] => 1
[item_price] => 60.00
)
[Item_3] => Array
(
[item_name] => Parrot
[item_quantity] => 4
[item_price] => 90.00
)
.
.
.
)
What I'd like to know is if there is a way I can create this array in the existing while loop? I'm aware of being able to add data to an array like $data = [] after delacring an empty array but the actual syntax eludes me.
Maybe I'm completely off the right track and there is a better way of doing it?
Thanks
Try something like this...
$itemCount = $_GET['itemCount'];
$i = 1;
$items = array();
while ($i <= $itemCount) {
$items['Item_'.$i]['item_name'] = $_GET["item_name_{$i}"];
$items['Item_'.$i]['item_quantity'] = $_GET["item_quantity_{$i}"];
$items['Item_'.$i]['item_price'] = $_GET["item_price_{$i}"];
$i++;
}
$result = array();
$itemCount = $_GET['itemCount'];
$i = 1;
while ($i <= $itemCount) {
$tmp = array();
$tmp['item_name'] = $_GET["item_name_{$i}"];
$tmp['item_quantity'] = $_GET["item_quantity_{$i}"];
$tmp['item_price'] = $_GET["item_price_{$i}"];
//echo "<br />Name: " .${'item_name_'.$i}. " - Quantity: " .${'item_quantity_'.$i}. " - Price: ".${'item_price_'.$i};
$i++;
$result['Item_{$i}'] = $tmp;
}
$itemCount = $_GET['itemCount'];
$i = 1;
my_array = [];
while ($i <= $itemCount) {
${'item_name_'.$i} = $_GET["item_name_{$i}"];
${'item_quantity_'.$i} = $_GET["item_quantity_{$i}"];
${'item_price_'.$i} = $_GET["item_price_{$i}"];
//echo "<br />Name: " .${'item_name_'.$i}. " - Quantity: " .${'item_quantity_'.$i}. " - Price: ".${'item_price_'.$i};
my_array["Item_".$i] = array(
"item_name"=>$_GET["item_name_{$i}"],
"item_quantity"=>$_GET["item_quantity_{$i}"],
"item_price"=>$_GET["item_price_{$i}"]
);
$i++;
}
var_dump(my_array);
$arr = array();
for($i = 1; isset(${'item_name_'.$i}); $i++){
$arr['Item_'.$i] = array(
'item_name' => ${'item_name_'.$i},
'item_quantity' => ${'item_quantity_'.$i},
'item_price' => ${'item_price_'.$i},
);
}

Categories