How to find and replace specific value in multiple array - php

I want to find flag key from array and replace new value. for example if i found flag values == 2 then i'll put new value instead of old.
Following is my array.
Array
(
[0] => Array
(
[id] => 863
[flag] => 1
[qty] => 2
[size] => 8
[fly_name] => Bead Headed Prince Nymphs size 18
)
[1] => Array
(
[id] => 861
[flag] => 1
[qty] => 2
[size] => 8
[fly_name] => Bead Headed Prince Nymphs size 14
)
[2] => Array
(
[id] => 671
[flag] => 1
[qty] => 4
[size] => 8
[fly_name] => Royal Wulff size 12
)
[3] => Array
(
[id] => 661
[flag] => 1
[qty] => 2
[size] => 3
[fly_name] => Hare's Ear Tan size 16
)
)
I want to find if flag key == 2 then replace new value of flag key , i have tried following way but not work.
if(in_array(2, $fliesUserColumn)) { // search value in the array
foreach($fliesUserColumn as $key => $val)
{
if ($val == '2') $fliesUserColumn[$key] = 'search4';
}
}else{
echo "not";
}
echo "<pre>";
print_r ($flyOfStores);
echo "</pre>";
die;
And out put like with new values in array
Array
(
[0] => Array
(
[id] => 863
[flag] => newvalue
[qty] => 2
[size] => 8
[fly_name] => Bead Headed Prince Nymphs size 18
)
[1] => Array
(
[id] => 861
[flag] => newvalue
[qty] => 2
[size] => 8
[fly_name] => Bead Headed Prince Nymphs size 14
)
...
)

Try below example
foreach ($fliesUserColumn as $key => $val) {
if ($fliesUserColumn[$key]['flag'] == '2') {
$fliesUserColumn[$key]['flag'] = 'Newvalue';
}
}

Try this
foreach ($fliesUserColumn as $key => $val) {
if ($fliesUserColumn[$key]['flag'] == '2') {
$fliesUserColumn[$key] = 'search4';
} else {
echo "not";
}
}

Related

PHP Foreach query checking

Array
(
[0] => Array
(
[cart_id] => 24763
[product_id] => 740
[name] => Samsung S20
[model] => Samsung S20
[shipping] => 1
[image] => catalog/samsung/samsungs20.jpg
[option] => Array
(
)
[download] => Array
(
)
)
[1] => Array
(
[cart_id] => 24799
[product_id] => 749
[name] => Huawei P40
[model] => Huawei P40
[shipping] => 1
[image] => catalog/huawei/huaweip40.jpg
[option] => Array
(
)
[download] => Array
(
)
)
)
^ Above is the output of the array
$productdata = $this->cart->getProducts();
^ Above is the code for query out array
is there anyway to check product_id of each array ? Let say that if
product_id = 749 , alert ('P40');
Tried to do
foreach ($productdata as $productdatas) {
if ($productdata['product_id'] = 749)
{ alert('P40');
}
}
nothing happen
This part sets the key, not read it. You need to replace the = with ==:
// From
if ($productdata['product_id'] = 749)
// To
if ($productdata['product_id'] == 749)

How to do sum of array item's value until not reached at specified value?

I want to do sum of array item's value until not reached at specified value.
Let me explain in details.
I want 20 k.g value in each box , So following is my array.
Array
(
[0] => Array
(
[product_id] => 2
[customer_id] => 1820
[order_id] => M-AAH-959
[quantity] => 5
[weight] => 1.3
[cubic] => 0.00267
[total_weight] => 6.5
[total_cubic] => 0.00267
)
[1] => Array
(
[product_id] => 3
[customer_id] => 1820
[order_id] => M-AAH-959
[quantity] => 6
[weight] => 1.5
[cubic] => 0.00348
[total_weight] => 9
[total_cubic] => 0.00348
)
[2] => Array
(
[product_id] => 8
[customer_id] => 1820
[order_id] => M-AAH-959
[quantity] => 7
[weight] => 1.5
[cubic] => 0.00267
[total_weight] => 10.5
[total_cubic] => 0.00267
)
)
You can see in above array there are 3 fields quantity,weight & total_weight.
So during loop i want to set 20 value to single box from sum of each items weight. we can take quantity & weight from any three items for set 20 value for each.
So output will be..
Array (
[0] => Array('box_item'=>20)
[1] => Array('box_item'=>6)
)
Let me explain output array..
You can see total_weigh of First array 6.5 & second 9 & third 10.5
I want to set 20 value per box, So calculation will be
6.5 + 9 = 15.5 , then add 4.5 from third array like 1.5*3 = 4.5
So first box is for 20 & remain weight value is 6 so it'll be set is for second box.
Note: Currently i have taken only 3 items for example purpose but there will be more items here and I have to set 20 k.g value for each for from weight of items value.
I have used following code but not helpful. This code is not given proper output
// First calculate the total
foreach ($main as $key => $package) {
$packageInfo[$key]['total_weight'] = $package['quantity'] * $package['weight'];
}
// Then check count the packages ?
$packages = [];
$packageTotalWeight = 0;
$packageItemWeight = 0;
$cubicTotal = 0;
foreach ($packageInfo as $key => $package) {
if(($packageTotalWeight + $package['total_weight']) > 20){
$packages[]['final_total'] = $packageTotalWeight;
$packageTotalWeight = $package['total_weight'];
} else {
$packageTotalWeight += $package['total_weight'];
}
}
This function will "pack" the boxes for you, selecting from each product in turn until a box reaches the maximum box weight, then starting a new one:
function pack_boxes($products, $box_weight) {
$boxes = array();
$this_box_weight = 0;
$box_number = 0;
foreach ($products as $product) {
// will all this product fit?
if ($product['total_weight'] < $box_weight - $this_box_weight) {
// yes - add this product to the box
$boxes[$box_number][] = $product;
$this_box_weight += $product['total_weight'];
$boxes[$box_number]['box_weight'] = $this_box_weight;
}
else {
// no - add the part that will fit to this box
$num_products = floor(($box_weight - $this_box_weight) / $product['weight']);
$balance = $product['quantity'] - $num_products;
$product['quantity'] = $num_products;
$product['total_weight'] = $num_products * $product['weight'];
$boxes[$box_number][] = $product;
$boxes[$box_number]['box_weight'] = $this_box_weight + $num_products * $product['weight'];
// add the balance to a new box
$box_number++;
$product['quantity'] = $balance;
$this_box_weight = $product['total_weight'] = $balance * $product['weight'];
$boxes[$box_number][] = $product;
$boxes[$box_number]['box_weight'] = $this_box_weight;
}
}
return $boxes;
}
The total weight in each box is available in $boxes[*]['box_weight'].
Sample usage (12 is maximum weight per box) to get the box weights per box:
$boxes = pack_boxes($products, 12);
print_r(array_column($boxes, 'box_weight'));
Output (box weights only):
Array
(
[0] => 11
[1] => 12
[2] => 3
)
Or the full data result:
print_r($boxes);
Output
Array
(
[0] => Array
(
[0] => Array
(
[product_id] => 2
[customer_id] => 1820
[order_id] => M-AAH-959
[quantity] => 5
[weight] => 1.3
[cubic] => 0.00267
[total_weight] => 6.5
[total_cubic] => 0.00267
)
[box_weight] => 11
[1] => Array
(
[product_id] => 3
[customer_id] => 1820
[order_id] => M-AAH-959
[quantity] => 3
[weight] => 1.5
[cubic] => 0.00348
[total_weight] => 4.5
[total_cubic] => 0.00348
)
)
[1] => Array
(
[0] => Array
(
[product_id] => 3
[customer_id] => 1820
[order_id] => M-AAH-959
[quantity] => 3
[weight] => 1.5
[cubic] => 0.00348
[total_weight] => 4.5
[total_cubic] => 0.00348
)
[box_weight] => 12
[1] => Array
(
[product_id] => 8
[customer_id] => 1820
[order_id] => M-AAH-959
[quantity] => 5
[weight] => 1.5
[cubic] => 0.00267
[total_weight] => 7.5
[total_cubic] => 0.00267
)
)
[2] => Array
(
[0] => Array
(
[product_id] => 8
[customer_id] => 1820
[order_id] => M-AAH-959
[quantity] => 2
[weight] => 1.5
[cubic] => 0.00267
[total_weight] => 3
[total_cubic] => 0.00267
)
[box_weight] => 3
)
)
Demo on 3v4l.org
You can get full sum and then pass each 20 to one element in resultant array:
$total_sum = array_sum(array_column($packageInfo,'total_weight'));
$boxes = ceil($total_sum/20); // count of boxes
for($i=0;$i<$boxes;$i++){
$res[$i]['box_item'] = ($total_sum - $i*20)>20 ? 20 : ($total_sum - $i*20);
}
Demo
Output:
Array
(
[0] => Array
(
[box_item] => 20
)
[1] => Array
(
[box_item] => 6
)
)

Delete Multidimensional Array Item By Values In the Item

Hey guys I have this array :
Array
(
[qty] => 1
[id] => 2
[name] => sallate me gjera plot
[price] => 8
[category_id] => 25
[dish_name] => sallate me gjera plot
[dish_id] => 2
[dish_category_id] => 25
[dish_qty] => 1
[dish_price] => 8
)
Array
(
[qty] => 1
[id] => 1
[name] => sallate cezar
[price] => 12
[category_id] => 25
[dish_name] => sallate cezar
[dish_id] => 1
[dish_category_id] => 25
[dish_qty] => 1
[dish_price] => 12
)
And what I'm trying to do is to unset the item by dish_id.
This is the way I intend to do that :
if(isset($_SESSION["cart_products"]) && count($_SESSION["cart_products"])>0)
{
foreach ($_SESSION["cart_products"] as $key =>$cart_itm)
{
if($cart_itm["dish_id"]==$removable_id)
{
unset($cart_itm[$key]);
}
}
}
Can anyone tell me what I'm doing worng..thanks :D
Actually you need to unset data from actual array which is $_SESSION["cart_products"] not $cart_itm.
So change unset($cart_itm[$key]); to unset($_SESSION["cart_products"][$key])
As alternative, you can use array_filter() with an anonimous function:
$removable_id = 1;
$_SESSION["cart_products"] = array_filter
(
$_SESSION["cart_products"],
function( $row ) use( $removable_id )
{
return $row['dish_id'] != $removable_id;
}
);
print_r( $_SESSION["cart_products"] );
will print:
Array
(
[0] => Array
(
[qty] => 1
[id] => 2
[name] => sallate me gjera plot
[price] => 8
[category_id] => 25
[dish_name] => sallate me gjera plot
[dish_id] => 2
[dish_category_id] => 25
[dish_qty] => 1
[dish_price] => 8
)
)

php print array contents

I need help printing the contents of this function:
which came from:
http://drupalcontrib.org/api/drupal/contributions%21flag%21flag.module/function/flag_get_user_flags/7
$userFlags = flag_get_user_flags('user', null, $node->uid, null, false);
If I use print_r:
print '<pre>';
print_r(flag_get_user_flags('user', null, $node->uid, null, false));
print '</pre>';
I get -
Array
(
[follow] => Array
(
[13] => stdClass Object
(
[flagging_id] => 20
[fid] => 5
[entity_type] => user
[entity_id] => 13
[uid] => 1
[sid] => 0
[timestamp] => 1385845849
)
[15] => stdClass Object
(
[flagging_id] => 21
[fid] => 5
[entity_type] => user
[entity_id] => 15
[uid] => 1
[sid] => 0
[timestamp] => 1385912237
)
[17] => stdClass Object
(
[flagging_id] => 22
[fid] => 5
[entity_type] => user
[entity_id] => 17
[uid] => 1
[sid] => 0
[timestamp] => 1386040495
)
[18] => stdClass Object
(
[flagging_id] => 23
[fid] => 5
[entity_type] => user
[entity_id] => 18
[uid] => 1
[sid] => 0
[timestamp] => 1386040515
)
[21] => stdClass Object
(
[flagging_id] => 24
[fid] => 5
[entity_type] => user
[entity_id] => 21
[uid] => 1
[sid] => 0
[timestamp] => 1386043939
)
[14] => stdClass Object
(
[flagging_id] => 25
[fid] => 5
[entity_type] => user
[entity_id] => 14
[uid] => 1
[sid] => 0
[timestamp] => 1386129658
)
)
)
When I use:
foreach($userFlags as $item) {
echo $item;
}
All i get is the word "Array" printed. If your familiar with drupal ideally I need to convert each entity_id to its author. printing the 13,15 etc. is a good start for me.
thanks for any help-
You have an array in an array. Pull the inner array out before your foreach:
$follow = $userFlags['follow'];
foreach($follow as $item) {
echo $item->entity_id;
}
Or more succinctly:
foreach($userFlags['follow'] as $item) {
echo $item->entity_id;
}
//#parram $data-array,$d-if true then die by default it is false
//#author Your name
function p($data,$d = false){
echo "<pre>";
print_r($data);
echo "</pre>";
if($d == TRUE){
die();
}
} // END OF FUNCTION
Use this function every time whenver you need to string or array it will wroks just GREAT. There are 2 Patameters 1.$data - It can be Array or String 2.$d - By Default it is FALSE but if you set to true then it will execute die() function
In your case you can write like this..
$userFlags = flag_get_user_flags('user', null, $node->uid, null, false);
foreach($userFlags as $item) {
p($item['id']); // If it is array
p($item->id); // If it is an Object
// To get benefit of this code Use above function of p in your code.
}

Iterate through array and get key and value [duplicate]

This question already has answers here:
How to loop through an associative array and get the key?
(12 answers)
Closed 8 years ago.
I need to iterate through a dynamic array. The array will look something like the following:
Array
(
[2010091907] => Array
(
[home] => Array
(
[score] => Array
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
)
[abbr] => ATL
[to] => 2
)
[away] => Array
(
[score] => Array
(
[1] => 0
[2] => 7
[3] => 0
[4] => 0
[5] => 0
[T] => 7
)
[abbr] => ARZ
[to] => 2
)
[weather] =>
[media] => Array
(
[tv] => FOX
[sat] => 709
[sathd] => 709
[radio] => Array
(
[home] => 153
[away] => 90
)
)
[bp] => 13
[yl] =>
[qtr] => Final
[down] => 0
[togo] => 0
[clock] => 00:26
[posteam] => ARZ
[note] =>
[redzone] =>
[stadium] => Georgia Dome
)
I need it to be dynamic and for testing purposes, I need to be able to call it via:
echo "Key: $key; Value: $value<br />\n";
I'm going to later take this information and place it into a mysql database, but for now, I need to brush up on arrays and figure out how to format the data.
Any help is appreciated.
I´d go for a recursive function: If a value is an array, call it again and otherwise display the key / value pair.
Something like (not tested):
function display_array($your_array)
{
foreach ($your_array as $key => $value)
{
if is_array($value)
{
display_array($value);
}
else
{
echo "Key: $key; Value: $value<br />\n";
}
}
}
display_array($some_array);

Categories