PHP how to flatten out this indexed array? - php

I have the following array:
Array
(
[0] => Array
(
[ContractorName] => Joe Soap
[BonusAmount] => 73.92
)
[1] => Array
(
[ContractorName] => Mike Michaels
[BonusAmount] => 68.55
)
[2] => Array
(
[ContractorName] => John Smith
[BonusAmount] => 34.35
)
[3] => Array
(
[ContractorName] => Pete Peterson
[BonusAmount] => 24.61
)
[4] => Array
(
[ContractorName] => Pete Smith
[BonusAmount] => 22.76
)
)
How do I go about ending up with an array that looks like this:
Array
(
[Joe Soap] => 73.92
[Mike Michaels] => 68.55
[John Smith] => 34.35
[Pete Peterson] => 24.61
[Pete Smith] => 22.76
)
I'm a bit lost at the moment. I have tried creating a new array by looping over the first array, but I'm getting unwanted results. Any help greatly appreciated.

Use array_combine with array_column as
array_combine(array_column($records, 'ContractorName'),array_column($records, 'BonusAmount'));

Go through entire array using foreach and then use each piece to construct new array.
$out = [];
foreach ($inputArray as $v) {
$out[$v['ContractorName']] = $v['BonusAmount'];
}
Second solution is by using array_combine and array_column.
$keys = array_column($inputArray, 'ContractorName');
$values = array_column($inputArray, 'BonusAmount');
$output = array_combine($keys, $values);
//Or put everything in single line
$output = array_combine(array_column($inputArray, 'ContractorName'), array_column($inputArray, 'BonusAmount'));
Third option
$output = array_column($inputArray, 'BonusAmount', 'ContractorName');

You can use one array_column(), and with the third parameter to specify the index. Live Demo.
array_column($array, 'BonusAmount', 'ContractorName');

Related

Best way to convert an indexed array to a two-dimensional array in php

I'm trying to figure out how to build the "desired" array from my "current" array.
My current array is an indexed array, but each value is actually two values separated by a |. I currently explode() each array value to produce two separate values. I'd like to convert the current array to a two-dimensional array where the 1st array is indexed and the nested array is an associative array.
I've tried several ideas, but none work. Any help to convert it programmatically is greatly appreciated.
My "Current" Array
$appInfo = array("idNum1|dir/path1","idNum2|dir/path2","idNum3|dir/path3");
My "Desired" array
$apps = array(
array("appID" => "$someVarAppID","appDir" => "$someVarAppPath"),
array("appID" => "$someVarAppID","appDir" => "$someVarAppPath"),
array("appID" => "$someVarAppID","appDir" => "$someVarAppPath"),
array("appID" => "$someVarAppID","appDir" => "$someVarAppPath")
);
Something like this will work:
$apps = array();
foreach ($appInfo as $app) {
list($id, $path) = explode('|', $app);
$apps[] = array('appId' => $id, 'appDir' => $path);
}
Output:
Array
(
[0] => Array
(
[appId] => idNum1
[appDir] => dir/path1
)
[1] => Array
(
[appId] => idNum2
[appDir] => dir/path2
)
[2] => Array
(
[appId] => idNum3
[appDir] => dir/path3
)
)
Demo on 3v4l.org

php - remove indexes of an aray if they have the same value

How can i make the values of such an array unique.
Array ( [0] => Array ( [0] => wallet [1] => pen [2] => perfume [3] => pen) )
as there is pen twice i would like it to be deleted in this way :
( [0] => Array ( [0] => wallet [1] => pen [2] => perfume) )
OR
( [0] => Array ( [0] => wallet [1] => perfume [2] => pen) )
and i would like it to apply for any length.
thanks for your help
How about array_flip used twice:
$arr = Array(0 => wallet, 1 => pen, 2 => perfume, 3 => pen);
$arr = array_flip(array_flip($arr));
print_r($arr);
output:
Array
(
[0] => wallet
[3] => pen
[2] => perfume
)
If you want to renumbered the indexes, add this ligne after:
$arr = array_values($arr);
if you just want to select a unique value you need to pass the array that you want to compare the values, I am assuming you passed the main array, you need to pass the array where the problem is found which is in your case the index 0 of an array
$result = array_unique($input[0]);
$input will have an array of unique values so pen will not be 2
if you need to delete any duplicated values in the array you can do this.
$input[0] = array_unique($input[0]);
if you need to reset the index you can use this
$new_index = array_values($input[0]);
print_r($new_index);
$tmp = array ();
foreach ($array as $row)
array_push($tmp,array_unique($row));
Here is the solution for multi dimensopnal array
$res = array();
foreach($your_array as $key=>$val){
$res[$key] = array_unique($val);
}
echo "<pre>";
print_r($res);

Array_Unique filtering

I have an multidimensional array:
Array
(
[0] => Array
(
[Id] => 1
[MTime_Id] => 1
[MName] => Breakfast
[DName] => Other Cereals
[IName] =>
[Date] => 2013-02-05
)
[1] => Array
(
[Id] => 1
[MTime_Id] => 1
[MName] => Breakfast
[DName] => Porridge
[IName] => Oats,Milk,Sugar
[Date] => 2013-02-06
)
[2] => Array
(
[Id] => 1
[MTime_Id] => 1
[MName] => Breakfast
[DName] => Porridge
[IName] => Oats,Milk,Sugar,Oats,Milk,Sugar
[Date] => 2013-02-05
)
)
And I am trying to use array unique to filter this
[IName] => Oats,Milk,Sugar,Oats,Milk,Sugar
I am having no luck. How can I filter the duplicates?
Cheers.
If you filter input and therefore don't have extra spaces in IName field, you can use something as simple as this for filtering:
$array[2]['IName'] = implode(',', array_unique(explode(',', $array[2]['IName'])));
The problem is that you habe in array two Oats,Milk,Sugar as element of IName, in array three you have Oats,Milk,Sugar,Oats,Milk,Sugar. This is not the same!
"Oats,Milk,Sugar"=="Oats,Milk,Sugar,Oats,Milk,Sugar" (or "Oats,Milk,Sugar".equals("Oats,Milk,Sugar,Oats,Milk,Sugar")) is false.
If you want to have it unique you have to explode the single results and then do a unique on it or you have to store the single values in seperate fields...
BTW: Here is a link how to remove duplicates from a multi dimensional array How to remove duplicate values from a multi-dimensional array in PHP
I am not sure if a function exists for that, here is a simple solution,
you can loop the array, and get the result of each value, then explode result, and insert it into an array.
then use the array_unique function.
try this:
$result = array();
foreach($arrays as $array)
{
$tmp = $array['IName'];
$tmp2 = explode(',',$tmp);
foreach ($tmp2 as $t)
{
$result[]=$t;
}
}
$array_unique = array_unique($result);

php array merge with value of parent array

I need to merge an array with value of parent array.
$testArr=unserialize('a:6:{s:5:"queue";a:2:{i:6;s:1:"5";i:5;s:1:"2";}s:3:"sum";a:2:{i:6;s:3:"765";i:5;s:3:"2.1";}s:7:"sumAccD";a:2:{i:6;s:3:"543";i:5;s:3:"3.1";}s:7:"sumAccC";a:2:{i:6;s:2:"54";i:5;s:3:"3.3";}s:7:"comment";a:2:{i:6;s:12:"test comment";i:5;s:6:"111222";}s:3:"yt0";s:0:"";}');
$ret = array();
foreach ($testArr as $pkey => $pval) {
if (is_array($pval)) {
foreach ($pval as $pvkey => $pvval) {
$ret[$pvkey] = array($pkey => $pvval);
}
}
}
echo '<pre>', print_r($ret), '</pre>';
In this case it prints out
Array
(
[6] => Array
(
[comment] => test comment
)
[5] => Array
(
[comment] => 111222
)
)
1
Unfortunally it print out only comment. I need to add other rows: queue,sum,sumAccD,sumAccC. Array must look like this:
Array
(
[6] => Array
(
[queue] => 5
[sum] => ''
....
[comment] => test comment
)
[5] => Array
(
[queue] => 2
[sum] => 2.1
....
[comment] => 111222
)
)
1
Please help merge them.
Thanks.
Look at this line:
$ret[$pvkey] = array($pkey => $pvval);
You're assigning the key to a new array every time, overwriting what was previously there.
In your case, 'comment' is the last key that is processed, so that's going to be the only key in the final array.
Instead of this, you could define a new array only once outside the inner for, like this:
$ret[$pvkey] = array();
And then assign your values to that array in the inner for loop as you would normally do (so no more creating arrays there!)
Problem solved by replacing
$ret[$pvkey] = array($pkey => $pvval);
with
$ret[$pvkey][$pkey] = $pvval;

calculations between two Multidimensional arrays

I have this code:
$id = new matrix(array(0=>array(1,0.5,3), 1=>array(2,1,4), 2=>array(1/3,1/4,1)));
$soma = $id->times($id)->sumRows();
That outputs this:
matrix Object ( [numbers] => Array ( [0] => Array ( [0] => 12.75 [1] => 22.3333333333 [2] => 4.83333333333 ) ) [numColumns] => 3 [numRows] => 1 )
and:
$total = $id->times($id)->sumRows()->sumTotal($id);
That outputs this:
matrix Object ( [numbers] => Array ( [0] => Array ( [0] => 39.9166666667 ) ) [numColumns] => 3 [numRows] => 1 )
Now, i am trying to make:
foreach ($soma as $value){
$final = (int)$value/(int)$total;
print_r ((int)$final);
}
The output will be 000.
Must be:
12.75/39.9166666667 = 0,3269230769230769
22.3333333333 / 39.9166666667 = ...
and so on
Thanks!
Just some ideas, without really knowing much about the matrix class...
All those (int)s should probably be (float)s, as you seem to want a non-int answer.
$value is itself an object, so you'll probably need to use $value['numbers'][0][0 or 1 or 2]. Same goes for $total.
the issue is solved :
documentation:
get_data($..)

Categories