how we remove index in the array and merge - php

Bellow is my Array in this array
****i want to remove second level index 1 and 2 and merge two arrays in one array****
$array1 = Array
(
[1] => Array
(
[1] => Array
(
[2] => 4
[3] => 2
[4] => 3
[5] =>
)
[2] => Array
(
[9] => 4
[10] =>
[11] => 4
[12] => 4
)
)
[2] => Array
(
[1] => Array
(
[2] => 3
[3] =>
[4] => 4.1428571428571
[5] => 5
)
[2] => Array
(
[9] => 3
[10] => 5
[11] => 5
[12] => 3
)
)
[3] => Array
(
[1] => Array
(
[2] => 5
[3] => 5
[4] =>
[5] => 4.1428571428571
)
[2] => Array
(
[9] => 3
[10] => 4.4285714285714
[11] => 4.4285714285714
[12] => 5
)
)
my aspected output is bellow where second level index is removed and arrays are merged
$array1= Array
(
[1] => Array
(
[2] => 4
[3] => 2
[4] => 3
[5] =>
[9] => 4
[10] =>
[11] => 4
[12] => 4
)
[2] => Array
(
[2] => 3
[3] =>
[4] => 4.1428571428571
[5] => 5
[9] => 3
[10] => 5
[11] => 5
[12] => 3
)
)
[3] => Array
(
[2] => 5
[3] => 5
[4] =>
[5] => 4.1428571428571
[9] => 3
[10] => 4.4285714285714
[11] => 4.4285714285714
[12] => 5
)
And i tried :
foreach($array1 as $arrkey => $arrvalue)
{
foreach($arrvalue as $innerkey => $innervalue){
//unset($cutvalu[$cutck]);
foreach($innervalue as $subkey => $subvalue){
#$array1[$arrkey][$subkey];
}
}
}
echo "<pre>"; print_r($array1);echo "<pre>";

Try this:
foreach($array1 as $arrkey => $arrvalue)
{
foreach($arrvalue as $innerkey => $innervalue){
foreach($innervalue as $subkey => $subvalue){
$new_array[$arrkey][$subkey] = $subvalue; // Change this line
}
}
}
echo "<pre>"; print_r($new_array);echo "<pre>";
This will give you what you're looking for.

Related

PHP Create Sub Arrays from Master Array based on keys

This pertains to PHP.
I have an array of arrays that is a recordset from a db query:
Array
(
[0] => Array
(
[0] => Amazon
[1] => AmazonSendTracking
[2] =>
[3] => 1
[4] => IN
[5] => 2020-01-07 11:32:18
[6] => 7
[7] => 5
)
[1] => Array
(
[0] => Amazon
[1] => AmazonGetOrdersAndMove
[2] =>
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 6
[7] => 4
)
[2] => Array
(
[0] => Test
[1] => RedirectTest1
[2] => data=data1&data2=data2&testvar=testvariable
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 17
)
[3] => Array
(
[0] => Test
[1] => RedirectTest2
[2] => data=value&data2=value2&testvar=value3
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 25
)
[4] => Array
(
[0] => Amazon
[1] => AmazonPushInventory
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 26
)
[5] => Array
(
[0] => Amazon
[1] => CalculateFloorCeiling
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 27
)
[6] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXML
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 28
)
[7] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXMLResetBusinessAndFlipEm
[2] =>
[3] => 1440
[4] => OUT
[5] =>
[6] => 8
[7] => 6
)
)
the Value in the 7th position (6) of the sub-array represents a "group" that I want to break out into individual arrays.
So I want the above to become one array of items not in any group and then a new array for each group like this:
Array1 - All Items not in a group
Array1
(
[0] => Array
(
[0] => Amazon
[1] => AmazonSendTracking
[2] =>
[3] => 1
[4] => IN
[5] => 2020-01-07 11:32:18
[6] => 7
[7] => 5
)
[1] => Array
(
[0] => Amazon
[1] => AmazonGetOrdersAndMove
[2] =>
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 6
[7] => 4
)
[2] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXMLResetBusinessAndFlipEm
[2] =>
[3] => 1440
[4] => OUT
[5] =>
[6] => 8
[7] => 6
)
)
Array2 - 1st group
Array2(
[0] => Array
(
[0] => Test
[1] => RedirectTest1
[2] => data=data1&data2=data2&testvar=testvariable
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 17
)
[1] => Array
(
[0] => Test
[1] => RedirectTest2
[2] => data=value&data2=value2&testvar=value3
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 25
)
)
Array3 - 2nd group
Array3(
[0] => Array
(
[0] => Amazon
[1] => AmazonPushInventory
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 26
)
[1] => Array
(
[0] => Amazon
[1] => CalculateFloorCeiling
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 27
)
[2] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXML
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 28
)
)
Im just not sure what array functions would be best used to accomplish this. I have tried getting the count of each group using the following.
I have a loop that steps through the master array (array of arrays call $rs) and while in the loop I pull out the 7th position into a variable called $HeaderRecNbr and then do this:
$GroupCount=array_count_values(array_column($rs, 6))[$HeaderRecNbr];
but Im not sure where to go from here.
Just create an array indexed by the group from index 6. This will create a sub-array for each group indexed by the group number:
foreach($array as $v) {
$groups[$v[6]][] = $v;
}
Then to get the items that are alone in a group, check if there is exactly one sub-array and add it to another array. Then remove it from the group array:
foreach($groups as $k => $v) {
if(count($v) == 1) {
$other[] = $v; // or $other[$v[0][6]][] = $v;
unset($groups[$k]);
}
}
$mainArray = ... all rows variable here..
$groupedArray = [];
$countArray = [];
foreach ($mainArray as $k => $v) {
$groupedArray[$v[6]][] = $v;
$countArray[$v[6]]++;
}
$singleItems = [];
$multipleItems = [];
foreach ($groupedArray as $k => $v) {
if ($countArray[$k] > 1) $multipleItems[] = $v;
else $singleItems = $v;
}
... do something with single and multi - groups...
1: start by grouping all items in groups by the 6th item of sub array... keep count of them at same time
2. Separate single and multiple items groups in 2 arrays...
3. Do whatever you need with multi-items groups.
This function will group them by desired index, then combine the ones that dont have any other with same index, and return an array of arrays, ungrouped, then all grouped.
function sortByIndex( $data, $index ) {
$sortedData = array();
$ungroupedData = array();
// Make sure you can loop through
if ( ! is_array( $data ) ) {
return FALSE;
}
foreach ( $data as $key => $arrayToInspect ) {
if ( ! isset( $sortedData[$arrayToInspect[$index]] ) ) {
$sortedData[$arrayToInspect[$index]] = array();
}
$sortedData[$arrayToInspect[$index]][] = $arrayToInspect;
}
// Combine as desired
foreach ( $sortedData as $groupId => $data ) {
if ( count( $data ) < 2 ) {
$ungroupedData[] = $data;
unset( $sortedData[$groupId] );
}
}
return ( array_merge( [ $ungroupedData ], array_values( $sortedData ) ) );
}

Php array remove duplicate and put toghether in another array

I have this php array:
Array
(
[0] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 0
[9] => 1
[10] => 1
)
[1] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 1
[9] => 0
[10] => 0
)
[2] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 0
[9] => 0
[10] => 0
)
[3] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 1
[9] => 0
[10] => 0
)
[4] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 0
[9] => 1
[10] => 0
)
)
I want i new array:
1) if element [0] exists in the array I want to control elements 8, 9 and 10 and if one of this is 1 I want to have 1 in the final array, but I don't want to have the same array 2 times (the index [0] is the key).
My final array shound be:
Array
(
[0] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 1
[9] => 1
[10] => 1
)
[3] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 1
[9] => 1
[10] => 0
)
)
I have only one time the index 2505 and 2505 and index 8 and 9 and 10 are 1 for the 2505 and the index 8 and 9 are 1 for the 2525.
this does the job: (assuming that your first array is $arrays and your desired result is $result)
$result = array();
foreach($arrays as $array)
{
if(!isset($result[$array[0]]))
{
$result[$array[0]] = $array;
}
else
{
$result[$array[0]][8] = $array[8] == 1 ? 1 : $result[$array[0]][8];
$result[$array[0]][9] = $array[9] == 1 ? 1 : $result[$array[0]][9];
$result[$array[0]][10] = $array[10] == 1 ? 1 : $result[$array[0]][10];
}
}
but wherever you get your data from, e.g. a database, I would use element[0] as the key, so you don't get multiple entries in $arrays. Another option would be to create a class which holds your data...

Php array saving values only till 62 indices

I am sending a big array of 130+ indices from ajax to php. But while going to php, if i print, it became a 2D array with indices 63, 63, 6 respectively.
Below is snip
Array
(
[0] => Array
(
[0] => 943900200
[1] => 1297017000
[2] => 1299436200
[3] => 1302114600
[4] => 1304879400
[5] => 1307385000
................
[60] => 1452105000
[61] => 1454869800
[62] => 1457375400
)
[1] => Array
(
[0] => 943900200
[1] => 1297017000
[2] => 1299436200
[3] => 1302114600
[4] => 1304879400
[5] => 1307385000
......
[61] => 1454869800
[62] => 1457289000
)
[2] => Array
(
[0] => 1440441000
[1] => 1443033000
[2] => 1445970600
[3] => 1445970600
[4] => 1447007400
[5] => 1448908200
)
)
But i want them in a single D array...[0]--[127] together. I tried to copy them using foreach aswell. It copies the first 63 indices and stops. Any one please help. Thanks
You must use foreach twice:
INTPUT (example):
Array
(
[0] => Array
(
[0] => 5031750
[1] => 3972258
[2] => 1673731
[3] => 721866
[4] => 4031885
[5] => 1454990
)
[1] => Array
(
[0] => 1115002
[1] => 27608
[2] => 3531620
[3] => 4412066
[4] => 4032217
[5] => 2681734
)
[2] => Array
(
[0] => 3360879
[1] => 5190034
[2] => 3452229
[3] => 5112636
[4] => 628357
[5] => 4299124
)
)
PHP Code:
$output = array();
foreach($input as $key=>$sub){
foreach($sub as $k => $v){
$output[] = $v;
}
}
print_r($output);
Output:
Array
(
[0] => 5031750
[1] => 3972258
[2] => 1673731
[3] => 721866
[4] => 4031885
[5] => 1454990
[6] => 1115002
[7] => 27608
[8] => 3531620
[9] => 4412066
[10] => 4032217
[11] => 2681734
[12] => 3360879
[13] => 5190034
[14] => 3452229
[15] => 5112636
[16] => 628357
[17] => 4299124
)

how to get single array of comma seperate value from array

this is my array i want to process.
Array ( [0] => Array ( [checklist_id] => 3 [order_id] => [id] => 1 )
[1] => Array ( [checklist_id] => 4 [order_id] => [id] => 2 ) [2] =>
Array ( [checklist_id] => 7 [order_id] => 8,9,10,11,12 [id] => 4 ) );
after trying array_push
$alreadyassingorder=array();
foreach ($collection as $checklistorder) {
if($checklistorder['order_id'])
{
$order=explode(',', $checklistorder['order_id']);
array_push($alreadyassingorder,$order);
}
}
the output is
Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5
[5] => 6 [6] => 7 ) [1] => Array ( [0] => 8 [1] => 9 [2] => 10 [3] =>
11 [4] => 12 ) )
the output i want
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5
[5] => 6 [6] => 7 ) [7] => 8 [8] => 9 [9] => 10 [10] =>
11 [11] => 12 ) )
Try like this:
$alreadyassingorder=array();
foreach ($collection as $checklistorder) {
if($checklistorder['order_id'])
{
$order=explode(',', $checklistorder['order_id']);
foreach($order as $index=>$key):
array_push($alreadyassingorder,$key);
endforeach;
}
}

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

Categories