I'm trying to do something to each "Order1" in the array before moving on to "Order2". Is there a way to do this in PHP?
Note: It's not always going to be called "Order1". I'm just using this as a generic term right now. Will most likely be an order#
Here's the original array:
Array
(
[100 Series] => Array
(
[Order1] => 4
[Order2] => 4
[Order3] => 4
[Order4] => 4
)
[50 Series] => Array
(
[Order1] => 4
[Order2] => 4
[Order3] => 4
[Order4] => 4
)
)
And here's what it should look like after going through once:
Array
(
[100 Series] => Array
(
[Order1] => 0
[Order2] => 4
[Order3] => 4
[Order4] => 4
)
[50 Series] => Array
(
[Order1] => 0
[Order2] => 4
[Order3] => 4
[Order4] => 4
)
)
Here's some logic added.. it's using some other array data not mentioned.
foreach($orders[$project] as $order => $hoursA)
{
if(array_key_exists($project, $orders) && !empty($orders[$project]))
{
if($resources[$name]["Schedule"][$day] >= $hoursA && $resources[$name]["Schedule"][$day] != 0)
{
$workFlow[$day][$project][$order][$name] = $hoursA;
$resources[$name]["Schedule"][$day] = (($resources[$name]["Schedule"][$day] - $hoursA < 0.000000001) ? 0 : $resources[$name]["Schedule"][$day] - $hoursA);
$orders[$project][$order] = 0;
} else if($resources[$name]["Schedule"][$day] < $hoursA)
{
$workFlow[$day][$project][$order][$name] = $resources[$name]["Schedule"][$day];
$orders[$project][$order] = $orders[$project][$order] - $resources[$name]["Schedule"][$day];
$resources[$name]["Schedule"][$day] = $resources[$name]["Schedule"][$day] - $resources[$name]["Schedule"][$day];
}
}
}
$searchFlag = 'Order1';
foreach($arrays as $array)
{
foreach($array as $key => $val)
{
if($key == $searchFlag) {
//do something to make 0
$arrays[$array][$key] = 0;
}
}
}
If it's always a specific key that you need setting to zero:
array_walk_recursive(
$data,
function(&$value, $key) {
if ($key == 'Order1') $value = 0;
}
);
Something like this:
foreach ($arr as $key => &$value) {
$_key = array_search(reset($value), $value);
$value[$_key] = 0;
}
Building off of Mark Baker's solution -- but making it more applicable for any number of arbitrary key names:
function setOrderToZeroForKey( $orderKey ) {
array_walk_recursive( $data, function( &$value, $key ) {
if( $key === $orderKey ) $value = 0;
}
}
array_walk( array( 'Order1', 'Order2', 'Order3', 'Order4' ), setOrderToZeroForKey );
(may not scale well -- if not, consider a more efficient data structure)
Related
So say I have an array below. As an example, is it possible in PHP to replace the -2.7 value of the first array part to 100 without knowing the #RR key or #BR for the other part? Such as:
$array[8314][WILDCARD][-1] = 100;
Array
(
[8314] => Array
(
[#RR] => Array
(
[-1] => -2.7
[0] => 0
)
)
[8810] => Array
(
[#BR] => Array
(
[-1] => 32500
[0] => 0
)
)
)
I think that you are looking for the below solution using array_key_first:
$firstKey = array_key_first($array); //say we found 8314;
$WILDCARD = array_key_first($array[$firstKey]);
//replace the old value with 100;
$array[$firstKey][$WILDCARD][-1] = 100;
Yes:
foreach ($input as $outerKey => $outerValue) {
foreach ($outerValue as $innerKey => $innerValue) {
foreach ($innerValue as $key => $value) {
if ($value === 2.7) $input[$outerKey][$innerKey][$key] = 100;
}
}
}
The code above assumes that you want to change 2.7 => 100 for all elements. If, instead of that you need to do it just for the first, then:
$visited = false;
foreach ($outerValue as $innerKey => $innerValue) {
if ($visited) break;
$visited = true;
foreach ($innerValue as $key => $value) {
if ($value === 2.7) $input[$outerKey][$innerKey][$key] = 100;
}
}
I have an Array that contains some entries. Some are string other are int. and those are defined in MySQL with type (int, varchar). The array i create looks like this: (minified the array because too long)
[3] => Array
(
[account_id] => *******
[month_id] => 201903
[month_comment] => 4,5% spend flat
[month_spend] => 23000
[month_budget] => 0
[month_adops] => 1035
[month_adops_forecast] => 1035
)
[4] => Array
(
[account_id] => ******
[month_id] => 201905
[month_comment] =>
[month_spend] =>
[month_budget] => 0
[month_adops] =>
[month_adops_forecast] => 45
)
[5] => Array
(
[account_id] => *******
[month_id] => 201906
[month_comment] =>
[month_spend] =>
[month_budget] => 0
[month_adops] =>
[month_adops_forecast] => 92
)
As you can see some of "month_spend" and/or "month_adops" is empty, the problem is that in PHP the value is converted to "NULL" when the field is Integer in database, so result gives me:
Incorrect integer value: '' for column 'month_spend' at row 2"
So i tried to change this inside an foreach loop like this:
$result_replace = array();
foreach ($data_replace as $key => $result) {
$result['month_id'] = substr_replace($result['month_id'], '2019', 0, 4);
if(empty($result['month_budget'])) {
$result['month_budget'] = 0;
$result_replace[] = $result;
}
else if(empty($result['month_spend'])) {
$result['month_spend'] = 0;
$result_replace[] = $result;
}
}
but looks like the foreach loop does not edit the data?
Use following to check both instead of else if
$result_replace = array();
foreach ($data_replace as $key => &$result) {
$result['month_id'] = substr_replace($result['month_id'], '2019', 0, 4);
if(empty($result['month_budget'])) {
$result['month_budget'] = 0;
}
if(empty($result['month_spend'])) {
$result['month_spend'] = 0;
}
$result_replace[] = $result;
}
Try replacing
empty()
by
is_null()
assumin your array is name $myArray
foreach ($myArray as $key => $result) {
if(empty($result['month_budget'])) {
$myArray[$key]['month_budget'] = 0
}
if(empty($result['month_spend'])) {
$myArray[$key]['month_spend'] = 0
}
}
var_dump($myArray)
in your case you are just assignt an empty array to $result_replace nothing else
empty from 0 will return false. So your code will be redundant with this function. Try to use is_integer. If not integer assign zero. The bad part is that you cannot avoid the loop with foreach.
is it possible to avoid the foreach from php by using a more concrete mysql query with e.g. IF(ISNULL(your_column), 0, your_column).
array_walk_recursive($array, function (&$value, $key){
if(!isset($value) && ('month_spend' === $key || 'month_adops' === $key)) {
$value = 0;
}
});
I have been trying to replace the values in the array
I'll name this array as $currencies when i print this it looks like.
Array
(
[0] => Array
(
[currencylabel] => USA, Dollars
[currencycode] => USD
[currencysymbol] => $
[curid] => 1
[curname] => curname1
[check_value] =>
[curvalue] => 0
[conversionrate] => 1
[is_basecurrency] => 1
)
[1] => Array
(
[currencylabel] => India, Rupees
[currencycode] => INR
[currencysymbol] => ₨
[curid] => 2
[curname] => curname2
[check_value] =>
[curvalue] => 0
[conversionrate] => 50
[is_basecurrency] =>
)
[2] => Array
(
[currencylabel] => Zimbabwe Dollars
[currencycode] => ZWD
[currencysymbol] => Z$
[curid] => 3
[curname] => curname3
[check_value] =>
[curvalue] => 0
[conversionrate] => 22
[is_basecurrency] =>
)
)
Here I am having a $conversionRate to which i need to divide the values present in the array $currencies [0] -> Array -> [conversionrate] and replace in the same place in array.
and the same operation for [1] -> Array -> [conversionrate] and so on..
for which my current approach is as follows
$conversionRate = 50;
foreach ($currencies as $key => $val) {
$key['conversionrate'] = $key['conversionrate'] / $conversionRate;
if($key['conversionrate'] == 1) {
$key['is_basecurrency'] = 1;
} else {
$key['is_basecurrency'] = '';
}
}
print_r($key);
die;
Currently this is not working kindly help
Your loop is all wrong, there is no $key['conversionrate'], it's $val['conversionrate']. In fact there doesn't seems to be a reason for the $key variable, you can just loop through the array with
foreach ($currencies as &$val)
Also, you probably want to print_r($currencies), not $key
Do not compare floating point numbers with == to 1, it might not work due to rounding errors.
You mixed up key and value and you need to use &$val to be able to change the array.
$conversionRate = 4;
foreach ($currencies as $key => &$val) {
if($val['conversionrate'] == $conversionRate) {
$val['is_basecurrency'] = 1;
} else {
$val['is_basecurrency'] = '';
}
$val['conversionrate'] = $val['conversionrate'] / $conversionRate;
}
unset($val);
print_r($currencies);
die;
$key is an index identofoer of an array and $val contain array values
so use like this
$conversionRate = 4;
foreach ($currencies as $key => $val) {
$val['conversionrate'] = $val['conversionrate'] / $conversionRate;
if($val['conversionrate'] == 1) {
$val['is_basecurrency'] = 1;
} else {
$val['is_basecurrency'] = '';
}
}
print_r($val);
die;
Hi,
How can we find the count of duplicate elements in a multidimensional array ?
I have an array like this
Array
(
[0] => Array
(
[lid] => 192
[lname] => sdsss
)
[1] => Array
(
[lid] => 202
[lname] => testing
)
[2] => Array
(
[lid] => 192
[lname] => sdsss
)
[3] => Array
(
[lid] => 202
[lname] => testing
)
)
How to find the count of each elements ?
i.e, count of entries with id 192,202 etc
You can adopt this trick; map each item of the array (which is an array itself) to its respective ['lid'] member and then use array_count_value() to do the counting for you.
array_count_values(array_map(function($item) {
return $item['lid'];
}, $arr);
Plus, it's a one-liner, thus adding to elite hacker status.
Update
Since 5.5 you can shorten it to:
array_count_values(array_column($arr, 'lid'));
foreach ($array as $value)
{
$numbers[$value[lid]]++;
}
foreach ($numbers as $key => $value)
{
echo 'numbers of '.$key.' equal '.$value.'<br/>';
}
Following code will count duplicate element of an array.Please review it and try this code
$arrayChars=array("green","red","yellow","green","red","yellow","green");
$arrLength=count($arrayChars);
$elementCount=array();
for($i=0;$i<$arrLength-1;$i++)
{
$key=$arrayChars[$i];
if($elementCount[$key]>=1)
{
$elementCount[$key]++;
} else {
$elementCount[$key]=1;
}
}
echo "<pre>";
print_r($elementCount);
OUTPUT:
Array
(
[green] => 3
[red] => 2
[yellow] => 2
)
You can also view similar questions with array handling on following link
http://solvemyquest.com/count-duplicant-element-array-php-without-using-built-function/
The following code will get the counts for all of them - anything > 1 at the end will be repeated.
<?php
$lidCount = array();
$lnameCount = array();
foreach ($yourArray as $arr) {
if (isset($lidCount[$arr['lid']])) {
$lidCount[$arr['lid']]++;
} else {
$lidCount[$arr['lid']] = 1;
}
if (isset($lnameCount [$arr['lname']])) {
$lnameCount [$arr['lname']]++;
} else {
$lnameCount [$arr['lname']] = 1;
}
}
$array = array('192', '202', '192', '202');
print_r(array_count_values($array));
$orders = array(
array(
'lid' => '',
'lname' => '',
))....
$foundIds = array();
foreach ( $orders as $index => $order )
{
if ( isset( $foundIds[$order['lid']] ) )
{
$orders[$index]['is_dupe'] = true;
$orders[$foundIds[$order['lid']]]['is_dupe'] = true;
} else {
$orders[$index]['is_dupe'] = false;
}
$foundIds[$order['lid']] = $index;
}
Try this code :
$array_count = array();
foreach ($array as $arr) :
if (in_array($arr, $array_count)) {
foreach ($array_count as $key => $count) :
if ($key == $arr) {
$array_count[$key]++;
break;
}
endforeach;
} else {
$array_count[$arr] = 1;
}
endforeach;
Check with in_array() function.
I have this array.
Array
(
[name] => Array
(
[isRequired] => 1
[isBetween] => 1
[isAlphaLower] =>
[isLength] =>
)
[email] => Array
(
[isEmail] => 1
)
[pPhone] => Array
(
[isPhone] =>
)
)
i want to split the array into two.
1. array with all boolean value true
Array
(
[name] => Array
(
[isRequired] => 1
[isBetween] => 1
)
[email] => Array
(
[isEmail] => 1
)
)
2. array with all boolean value false
Array
(
[name] => Array
(
[isAlphaLower] =>
[isLength] =>
)
[pPhone] => Array
(
[isPhone] =>
)
)
How do i do it?
thank you..
initialize the two new arrays
foreach the input array
foreach the inner array of each input array entry
according to the value set the one or the other of the two new arrays
done.
Example:
$arrayTrue = $arrayFalse = arrray(); # 1
foreach($arrayInput as $baseKey => $inner) # 2
foreach($inner as $key => $value) # 3
if ($value) $arrayTrue[$basekey][$key] = $value; # 4
else $arrayFalse[$basekey][$key] = $value;
function is_true($var) {
return $var;
}
function is_false($var) {
return !$var;
}
$result_true = array();
$result_false = array();
foreach ($array as $k => $a) {
$result_true[$k] = array_filter($a, 'is_true');
$result_false[$k] = array_filter($a, 'is_false');
};
or
$result_true = array();
$result_false = array();
foreach ($array as $k => $a) {
$result_true[$k] = array_filter($a);
$result_false[$k] = array_filter($a, function ($x) { return !$x; } );
};
As your array is a 2 level array, you will need to use 2 loops.
$trueValues = array();
$falseValues = array();
foreach($input AS $key=>$firstLevelValue) {
foreach($firstLevelValue AS $key2=>$secondLevelValue) {
if ($secondLevelValue)
$trueValues[$key][$key2] = $secondLevelValue;
else
$falseValues[$key][$key2] = $secondLevelValue;
}
}
A 3 level array would be:
$trueValues = array();
$falseValues = array();
foreach($input AS $key=>$firstLevelValue) {
foreach($firstLevelValue AS $key2=>$secondLevelValue) {
foreach($secondLevelValue AS $key3=>$thirdLevelValue) {
if ($thirdLevelValue)
$trueValues[$key][$key2][$key3] = $thirdLevelValue;
else
$falseValues[$key][$key2][$key3] = $thirdLevelValue;
}
}
}