How to associate values from one array to another array as keys? - php

So, to simply put it, it looks like this:
Array1:
Array
(
[0] => id
[1] => name
[2] => email
)
Array2:
Array
(
[0] => 1
[1] => paula
[2] => paula#paula.com
[3] => 2
[4] => martin
[5] => martin#google.com
[6] => 3
[7] => kasandra
[8] => kasandra#google.com
[9] => 4
[10] => helena
[11] => helena#google.com
[12] => 5
[13] => sophia
[14] => sophia#google.com
[15] => 6
[16] => denis
[17] => denis#google.com
)
How to make those values from Array1 as keys in Array2 accordingly so that the end result looks like this:
Array
(
[id] => 1
[name] => paula
[email] => paula#paula.com
[id] => 2
[name] => martin
[email] => martin#google.com
[id] => 3
[name] => kasandra
[email] => kasandra#google.com
[id] => 4
[name] => helena
[email] => helena#google.com
[id] => 5
[name] => sophia
[email] => sophia#google.com
[id] => 6
[name] => denis
[email] => denis#google.com
)

you can use a for loop with step 3
$numCoords = count($array2)/3
for ($i = 0; $i < $numCoords; $i++ ){
$array[$i]['id'] = $array2[$i*3];
$array[$i]['name'] = $array2[($i*3)+1];
$array[$i]['email'] = $array2[($i*3)+2];
}

I would go with array_chunk and get used to numeric indexes,
but if you really want to have them as words you may map them:
$keys = [
0 => 'id',
1 => 'name',
2 => 'email'
];
$chunked = array_chunk($array2, $length=3);
$result = array_map(function ($chunk) {
global $keys;
return array_combine($keys, $chunk);
}, $chunked);

Related

Get sum of subarray column for indexed rows

I have an array of customers/people with a subarray of projects pertaining to that customer. Projects come in two types "typeJob" or "typePipeline". I need to sum the values of individual columns in the projects subarray (specifically columns 5, 6, 7 - representing revenue for month 1, month 2, and month 3) by Client.
I have tried various possible avenues including the one below, but I seem to always end up either testing if the condition is satisfied for the array as a whole rather than for each row - or if I go the way of for each loops generate errors because either there's an array/single value mismatch or the condition just doesn't do anything.
Here is what I have tried most recently. It works in the sense that it checks if "typeJob" is in the array but then sums all rows (even those without "typeJob") if "typeJob" is found for any of the Client's projects. I want to exclude rows where "typeJob" is not found.
foreach ($arrays as $row) {
$indexJob = array_search('typeJob', array_column($row['projects'],0));
if ($indexJob !== false) {
$job_total1[$row['Client']] = array_sum(array_column($row['projects'],5));
$job_total2[$row['Client']] = array_sum(array_column($row['projects'],6));
$job_total3[$row['Client']] = array_sum(array_column($row['projects'],7));
}
}
And here is the array:
(
[331] => Array
(
[KeyAccountID] => 1234
[KeyAccountName] => John Lennon
[ClientID] => 9999
[Client] => BBC
[projects] => Array
(
[0] => Array
(
[0] => typePipeline
[1] => 915
[2] => Zyxeldy
[3] =>
[4] =>
[5] => 15000
[6] =>
[7] =>
[8] =>
)
[1] => Array
(
[0] => typeJob
[1] => 956
[2] => Awesome project, Step 1
[3] =>
[4] =>
[5] => 1833.3333
[6] => 1833.3333
[7] => 1833.3333
[8] =>
)
[2] => Array
(
[0] => typePipeline
[1] => 957
[2] => Awesome project, Step 2
[3] =>
[4] =>
[5] => 7000
[6] =>
[7] =>
[8] =>
)
)
)
[344] => Array
(
[KeyAccountID] => 1234
[KeyAccountName] => John Lennon
[ClientID] => 9998
[Client] => ABC
[projects] => Array
(
[0] => Array
(
[0] => typePipeline
[1] => 487
[2] => CRM integration
[3] =>
[4] =>
[5] =>
[6] => 98750
[7] => 98750
[8] =>
)
[1] => Array
(
[0] => typeJob
[1] => 839
[2] => Data Warehouse
[3] =>
[4] =>
[5] =>
[6] => 11643.0601
[7] =>
[8] =>
)
)
)
[350] => Array
(
[KeyAccountID] => 1236
[KeyAccountName] => Ringo Starr
[ClientID] => 9997
[Client] => XYY
[projects] => Array
(
[0] => Array
(
[0] => typeJob
[1] => 867
[2] => Data Mining
[3] =>
[4] =>
[5] => 10000
[6] =>
[7] =>
[8] =>
)
)
)
[351] => Array
(
[KeyAccountID] => 1235
[KeyAccountName] => Poul McCartney
[ClientID] => 9996
[Client] => XYZ
[projects] => Array
(
[0] => Array
(
[0] => typePipeline
[1] => 715
[2] => XYZ, CSM
[3] =>
[4] =>
[5] => 22083.3333
[6] => 22083.3333
[7] => 22083.3333
[8] =>
)
)
)
etc.
You could filter the $row['projects'] array before summing the values:
foreach ($arrays as $row) {
$typeJobs = array_filter($row['projects'], function ($v) { return $v[0] == 'typeJob'; });
$job_total1[$row['Client']] = array_sum(array_column($typeJobs,5));
$job_total2[$row['Client']] = array_sum(array_column($typeJobs,6));
$job_total3[$row['Client']] = array_sum(array_column($typeJobs,7));
}
It may be quicker to just loop through them and keep a running total...
foreach ($arrays as $row) {
$totals = [0,0,0];
foreach ( $row['projects'] as $project ) {
if ( $project[0] == 'typeJob' ) {
$totals[0] += $project[5];
$totals[1] += $project[6];
$totals[2] += $project[7];
}
}
$job_total1[$row['Client']] = $totals[0];
$job_total2[$row['Client']] = $totals[1];
$job_total3[$row['Client']] = $totals[2];
}
it doesn't look as slim as using the array_... methods, but this only processes the array once instead of once for each call.

How to store all data with same key together

I have an array which looks like this:
Array
(
[companyid] => 22
[userid] => Array
(
[0] => 60
[1] => 61
[2] => 65
[3] => 63
[4] => 64
[5] => 66
[6] => 68
[7] => 69
[8] => 70
[9] => 71
)
[username] => Array
(
[0] => nu1234
[1] => nu12345
[2] => username1235
[3] => testtttttt
[4] => username123
[5] => nu123
[6] => nu
[7] => sdgsdgsdg
[8] => testtttttt1234
[9] => nu1235678910
[10] =>
)
[password] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
)
)
How can I merge the same keys together, and for all of them add the first value which in this case is 22?
Needed end result:
Array
(
[0] => Array
(
[companyid] => 22
[userid] => 35
[username] => dfhdfhdf
[password] => dfhdfhdfhdf
)
[1] => Array
(
[companyid] => 22
[userid] => 35
[username] => dfhdfhdf
[password] => dfhdfhdfhdf
)
[2] => Array
(
[companyid] => 22
[userid] => 35
[username] => dfhdfhdf
[password] => dfhdfhdfhdf
)
)
I've tried multiple things but nothing gives me the desired result.
Before when I had only two values I did this:
foreach($useroutput['username'] as $key => $val){
if(!empty($val)){
$users[] = array_combine(['username','password'],array_column($useroutput, $key));
}
}
But I cannot get it to work for more than 2 values. The array just shows empty when I print it.
I've also tried multiple combinations of the code below:
foreach($useroutput as $key => $val){
$newarray[$key][]['companyid'] = $useroutput['companyid'];
$newarray[$key][]['userid'] = $useroutput['userid'];
$newarray[$key][]['username'] = $useroutput['username'];
$newarray[$key][]['password'] = $useroutput['password'];
}
array_merge also didn't do what I need.
$new_array = [];
foreach ($useroutput['userid'] as $key => $val) {
$new_array[] = [
'companyid' => $useroutput['companyid'],
'userid' => $val,
'username' => $useroutput['username'][$key],
'password' => $useroutput['password'][$key],
];
}

PHP Replace Array with multiple Array by key

i have Main Array and i want to replace
Array
(
[0] => {title-1}
[1] => zebra
[2] => {title-1}
[3] => fruit
[4] => {title-2}
[5] => cars
[6] => {title-3}
[7] => city
[8] => {title-3}
[9] => amazing
[10] => gold
[11] => {title-2}
)
and then i have 3 array like that
Array
(
[0] => Blue
[2] => Red
)
Array
(
[6] => lamborghini
[8] => bugati
)
Array (
[4] => Yellow
[11] => dodge
)
i want to output like that
(
[0] => blue
[1] => zebra
[2] => red
[3] => fruit
[4] => yellow
[5] => cars
[6] => lamborghini
[7] => city
[8] => bugati
[9] => amazing
[10] => gold
[11] => dodge
)
i try used array_replace_recursive() but only work if one array
can any body help me
As you stated you have 3 array wich do contain values for your main array. There has nothing recoursive to be done.
You can simply use "array-replace":
$arrayMain = array(0 => "{title-1}", 1 => "zebra", 2 => "{title-1}");
$arrayReplace = array(0 => "Blue", 2 => "Red");
$finishedArray = array_replace($arrayMain, $arrayReplace);
This will give you:
array(
0 => "Blue",
1 => "zebra",
2 => "Red",
);
You can actually use this to replace multiple array_keys from multiple arrays like array_replace($mainArray, $replaceArray1, $replaceArray2); and so on.
Check the PHP Docs:
http://php.net/manual/en/function.array-replace.php
To use array_replace_recursive both array keys should be same. refer to below code.
$base = array('0' => '{title-1}', '1' => 'zebra','2'=>'{title-1}','3'=> 'fruit','4' => '{title-2}',5 => 'cars',6 => '{title-3}',7 => 'city');
$tem=array ( 0 => 'Blue', 2 => 'Red' ) ;
$tem1=array (4 => 'Yellow',6 => 'dodge');
$basket = array_replace_recursive($base, $tem);
$basket1 = array_replace($basket, $tem1);
print_r($basket1);
output:
Array
(
[0] => Blue
[1] => zebra
[2] => Red
[3] => fruit
[4] => Yellow
[5] => cars
[6] => dodge
[7] => city
)

array manipulation

I have a two column array (array1), for each row of that array I need to compare the value in the 2nd column with a column value in each row of another array(array1) , when they equal I want to append another column value (from array2) to the first array.
in english:
if array1[x][1] = array2[y][0]
then array1[x][2] = array2[y][2]
screen dumps of both arrays
array1 (
[1] => Array (
[0] => 1 [1] => 2
)
[2] => Array (
[0] => 2 [1] => 3
)
[3] => Array (
[0] => 3 [1] =>
) [7] => Array (
[0] => 7 [1] => 1
)
[8] => Array (
[0] => 8 [1] => 1
)
[9] => Array (
[0] => 9 [1] => 10
)
[10] => Array (
[0] => 10 [1] => 2
)
)
array2 (
[0] => Array (
[0] => 1
[1] => 2
[2] => 2
[3] => Jane
[4] => Smith
[5] => jsmith#internet.com
[6] => jsmith
[7] => 12345
[8] => 1
[9] => no
)
[1] => Array (
[0] => 2
[1] => 2
[2] => 3
[3] => James
[4] => Beard
[5] => jasb#bellsouth.net
[6] => jbeard03
[7] => keeper
[8] => 1
[9] => no
)
[2] => Array (
[0] => 3
[1] => 2
[2] =>
[3] => Peter
[4] => Allen
[5] => pallen#rfgg.com
[6] => pallen
[7] => pallen
[8] => 1
[9] => no
)
[3] => Array (
[0] => 7
[1] => 2
[2] => 1
[3] => Joe
[4] => Blow
[5] => jasb#bellsouth.net
[6] => jblow
[7] => blow123
[8] => 5
[9] => yes
)
[4] => Array (
[0] => 8
[1] => 2
[2] => 1
[3] => John
[4] => Smith
[5] => logtest#bellsouth.net
[6] => jnsmith
[7] => jsmith123
[8] => 4
[9] => yes
)
[5] => Array (
[0] => 9
[1] => 2
[2] => 10
[3] => Frank
[4] => Smith
[5] => pallen#test.com
[6] => fsmith
[7] => fsmith123
[8] => 4
[9] => yes
)
[6] => Array (
[0] => 10
[1] => 2
[2] => 2
[3] => Loretta
[4] => Beard
[5] => lbeard#me.net
[6] => lbeard
[7] => lbeard123
[8] => 1
[9] => no
)
)
Does this work? I'm not sure I'm entirely clear on what you're looking for.
foreach($array1 as $x => $xarray) {
foreach($array2 as $y => $yarray) {
if($xarray[1] == $yarray[0]) {
$array1[$x][2] = $array[$y][2];
}
}
}
foreach ($array1 as &$a1) {
foreach ($array2 as $a2) {
if ($a1[1] == $a2[0]) {
$a1[] = $a2[2];
continue 2;
}
}
}
BTW, you should try to use explicit keys that actually mean something, e.g. $a1['id'] instead of $a1[1]. You'll thank yourself when you look at the code again two months down the road.
And because I threatened to do so:
btwyoushouldtrytouseexplicitkeysthatactuallymeansomethingeg$a1['id']insteadof$a1[1]youllthankyourselfwhenyoulookatthecodeagaintwomonthsdowntheroad ;-P

Create an array using the index values from another array

OK here is what I would like to do. I have an array. What i want to do is make another array of the index values from the first one. Take the below I want to create and array from this :
Array (
[identifier] => ID
[label] => HouseNum
[items] => Array (
[0] => Array (
[ID] => 1
[HouseNum] => 17
[AptNum] =>
[Street] => birch glen dr
[City] => Clifton Park
[State] => NY [Zip5] =>
[EID] => E083223
[RequestDate] => 02/05/09
[Status] => In-Qeue
[DateCompleted] =>
[CompletedBy] =>
[ContactName] => Suzy Q
[ContactNumber] => 555-867-5309
[ContactTime] => 9-9 )
)
);
That will end up looking like this :
Array(
[0] => [ID]
[1] => [HouseNum]
[2] => [AptNum]
[3] => [Street]
[4] => [City]
[5] => [State]
[6] => [Zip5]
[7] => [EID]
[8] => [RequestDate]
[9] => [Status]
[10] => [DateCompleted]
[11] => [CompletedBy]
[12] => [ContactName]
[13] => [ContactNumber]
[14] => [ContactTime]
);
Any thoughts on how to achieve this ? I mostly need to know how to get just the index values.
$indexes = array_keys($whatever['items'][0]);
http://us.php.net/manual/en/function.array-keys.php
foreach($items[0] as $idx => $val)
{
$indexes[] = $idx;
}
Or:
$indexes = array_keys($items[0]);
$result = array_keys($input['items'][0]);

Categories