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
)
Related
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.
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);
my code is:
array set 1:
Array
(
[0] => 15-3
[1] => 16-3
[2] => 15-4
[3] => 16-4
[4] => 15-3
[5] => 16-3
[6] => 15-4
[7] => 16-4
[8] => 15-3
[9] => 16-3
[10] => 15-4
[11] => 16-4
)
My second array set is:
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 2
[5] => 2
[6] => 2
[7] => 2
[8] => 3
[9] => 3
[10] => 3
[11] => 3
)
i just combine the above both the two array into one like below
$data1=array_combine($store_attri_ids, $store_ids);
but its shows like that
Array
(
[15-3] => 3
[16-3] => 3
[15-4] => 3
[16-4] => 3
)
The remaining values are not combined, wt we do nw????
You can try this by creating a sub-array -
$data1 = array();
foreach($store_attri_ids as $key => $id) {
$data1[$id][] = $store_ids[$key];
}
The output would be like -
Array
(
[15-3] => array(1, 2, 3),
[16-3] => array(...),
[15-4] => array(...),
[16-4] => array(...)
)
If you use array_combine the result is totally correct, like putting here: http://php.net/manual/en/function.array-combine.php
You need use array_merge, take a look at the documentation :http://php.net/manual/en/function.array-merge.php
Use like this:
$data1=array_merge($store_attri_ids, $store_ids);
print_r($data1);
How to explode this array value:
$row = array(
[0] = 1,1,Carlyle,Rogers,1,"Carlyle, Rogers",0000-00-00,,carlyle.rogers#stafford-trust.com,,non premium)
I tried this code
$values = explode(',', $row[0]);
and give me this wrong output:
Array (
[0] => 1
[1] => 1
[2] => Carlyle
[3] => Rogers
[4] => 1
[5] => "Carlyle
[6] => Rogers"
[7] => 0000-00-00
[8] =>
[9] => carlyle.rogers#stafford-trust.com
[10] =>
[11] => non premium
)
What I want is to Output must be like this one:
Array (
[0] => 1
[1] => 1
[2] => Carlyle
[3] => Rogers
[4] => 1
[5] => "Carlyle, Rogers"
[6] => 0000-00-00
[7] =>
[8] => carlyle.rogers#stafford-trust.com
[9] =>
[10] => non premium
)
You can't use explode like that because your input seems to be CSV-formatted and explode knows nothing about that "format". Use str_getcsv instead:
$values = str_getcsv($row[0]);
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