How to separate two array by id - php

I have an array ,i want to seperate array into single array by there TNXID.by any php function seperate array by TXNID into one single array variable .Because i want to save array with parent TXNID and child TXNID save..My demo code is
Array
(
[XYX] => Array
(
[0] => Array
(
[TXNObjectType] => 102
[CustomerAccountID] => 951
[ParentTXNLineNumber] => 1
[TXNID] => 11
)
[1] => Array
(
[TXNObjectType] => 84
[CustomerAccountID] => 951
[ParentTXNLineNumber] => 1
[TXNID] => 12
)
)
[XYZ] => Array
(
[0] => Array
(
[TXNID] => 11
[LineNo] => 7
[BranchID] => 164
)
[1] => Array
(
[TXNID] => 11
[LineNo] => 8
[BranchID] => 164
)
[2] => Array
(
[TXNID] => 12
[LineNo] => 9
[BranchID] => 164
)
[3] => Array
(
[TXNID] => 12
[LineNo] => 10
[BranchID] => 164
)
)
)
Expected output:
Array(
Array
(
[XYX] => Array
(
[0] => Array
(
[TXNObjectType] => 102
[CustomerAccountID] => 951
[ParentTXNLineNumber] => 1
[TXNID] => 11
)
)
)
Array(
[XYZ] => Array
(
[0] => Array
(
[TXNID] => 11
[LineNo] => 7
[BranchID] => 164
)
[1] => Array
(
[TXNID] => 11
[LineNo] => 8
[BranchID] => 164
)
)
)
Array(
Array
(
[XYX] => Array
(
[0] => Array
(
[TXNObjectType] => 102
[CustomerAccountID] => 951
[ParentTXNLineNumber] => 1
[TXNID] => 12
)
)
)
Array(
[XYZ] => Array
(
[0] => Array
(
[TXNID] => 12
[LineNo] => 7
[BranchID] => 164
)
[1] => Array
(
[TXNID] => 12
[LineNo] => 8
[BranchID] => 164
)
)
)
so please suggest mi appropriate solution for this .

I will try to give you an approach on how I would go about it. Here you need to take care of two keys while rearranging the nested arrays. First is the outer-most keys labelled XYX,XYZ and so on. Second is the key for the child array i.e. TXNID.
So, initially you could traverse the array and transform(flatten) it into this form:
Array
( [0] => Array
(
[TXNObjectType] => 102
[CustomerAccountID] => 951
[ParentTXNLineNumber] => 1
[TXNID] => 11
[Parent] => XYX //Store the parent at the same level
)
[1] => Array
(
[TXNObjectType] => 84
[CustomerAccountID] => 951
[ParentTXNLineNumber] => 1
[TXNID] => 12
[Parent] => XYX //Store the parent at the same level
)
...
Once you have this, you could simply sort this array of arrays based on the key TXNID. For that you might want to use uasort. You will have to define a custom comparator for this, should be a fun exercise.
Once you have sorted the array of arrays. You will just have to traverse this array one last time and sort of uncompress it, by checking the Parent key and pushing in array elements with that as the key and the sorted arrays as the values to get back a nested output like you desire.
There several other array functions in PHP that might come in handy. This might not even be the most efficient solutions but I hope it gets you started in the right direction.

Related

Change structure multidimensional array

Good day. I am new at php and I have a small problem. I have an array.. I want to change stucture of it .. object_type sometimes the same.. I want to have multidimensional array with object_type a key and value all arrays with this object_type.
[0] => Array
(
[initiator_id] => 259
[object_type] => 1
[object_id] => 905
[date] => 2021-11-16 06:24:16
)
[1] => Array
(
[initiator_id] => 259
[object_type] => 1
[object_id] => 905
[date] => 2021-11-16 04:54:54
)
[2] => Array
(
[initiator_id] => 259
[object_type] => 1
[object_id] => 905
[date] => 2021-11-16 04:53:58
)
[3] => Array
(
[initiator_id] => 219
[object_type] => 2
[object_id] => 915
[date] => 2021-11-16 04:53:58
)
Here you can find how to create Multidimensional Arrays. Click here PHP Multidimensional Arrays
And to learn PHP language www.w3schools.com is a very good site. You can find almost every example briefly explained here.
I hope this example may solve your issue.
<?php
$obj = array("initiator_id"=>"35", "object_type"=>"37", "object_id"=>"43");
$obj_2 = array("initiator_id"=>"135", "object_type"=>"237", "object_id"=>"343");
$array_list = array (
array("Test 1",22,18),
array("Test 2",5,2),
array("Test 3",17,15)
);
$array_list[1]["new"] = $obj_2;
$result = array_merge($array_list, $obj);
echo "<Pre>";
print_r($result);
echo "</pre>";
?>
Output:
Array
(
[0] => Array
(
[0] => Test 1
[1] => 22
[2] => 18
)
[1] => Array
(
[0] => Test 2
[1] => 5
[2] => 2
[new] => Array
(
[initiator_id] => 135
[object_type] => 237
[object_id] => 343
)
)
[2] => Array
(
[0] => Test 3
[1] => 17
[2] => 15
)
[initiator_id] => 35
[object_type] => 37
[object_id] => 43
)

Sort a multidimensional an array with numeric keys but keep the keys same just change the order [duplicate]

This question already has an answer here:
PHP sort associative array by numeric key in asc order [duplicate]
(1 answer)
Closed 10 months ago.
So I have 3 dimensional array. I want that array to be reordered based on the keys but the value of the keys should remain as it is. Like for an example if the array keys are 5,2,4,1,3 then it should become 1,2,3,4,5. Below I'm providing the array I have and excepted array and the solutions I have tried.
This is the array I have :-
[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)
)
)
[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)
[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
Following are the solutions I have tried :-
$result = ksort($result);
$result = array_values($result);
$result = array_splice($result, 0, 0);
$result = sort($result);
$result = array_splice($result, 0, count($result));
This is the expected array :-
Array
(
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)
[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)
)
)
)
Nothing is working any help will be appreciated. thanks in advance.
You are using ksort as $result = ksort($result);, ksort return TRUE/FALSE. That means you are assigning that to $results.
Read here PHP ksort
Your code should be:-
ksort($results);
instead of
$result = ksort($result);
You can use ksort for the keys sorting, here is an example
$arr = [
5 => [1,3],
3 => [2,3],
2 => [0,7]
];
ksort($arr);
echo '<pre>';
print_r($arr);
Output
Array
(
[2] => Array
(
[0] => 0
[1] => 7
)
[3] => Array
(
[0] => 2
[1] => 3
)
[5] => Array
(
[0] => 1
[1] => 3
)
)

How to sort a multi dimension PHP array with a key DESC wise without rearranging its index (can remove those elements)

I am solving a formula for MLM website for distributing their members a commission.
I have an multi dimension array in PHP $directors, which is as bellow
Array
(
[0] => Array
(
[user_id] => 10
[directors_count] => 6
)
[1] => Array
(
[user_id] => 11
[directors_count] => 2
)
[2] => Array
(
[user_id] => 12
[directors_count] => 5
)
[3] => Array
(
[user_id] => 13
[directors_count] => 1
)
[4] => Array
(
[user_id] => 14
[directors_count] => 1
)
[5] => Array
(
[user_id] => 15
[directors_count] => 2
)
[6] => Array
(
[user_id] => 16
[directors_count] => 1
)
[7] => Array
(
[user_id] => 17
[directors_count] => 0
)
)
$directors array will be used to generate an new array called $final_array which would extract data from $directors array $directors[''directors_count] key wise (DESC wise) but in straight manner.
I want $directors array to be extracted like
highest directors_count >> next highest directors_count >> next highest directors_count
without going back with array's index, unwanted indexes would get deleted.
as mentioned in bellow image, all the indexes without highlighted (with red) would get deleted
so the $final_array would look like something bellow
Array
(
[0] => Array
(
[user_id] => 10
[directors_count] => 6
)
[1] => Array
(
[user_id] => 12
[directors_count] => 5
)
[2] => Array
(
[user_id] => 15
[directors_count] => 2
)
[3] => Array
(
[user_id] => 16
[directors_count] => 1
)
[4] => Array
(
[user_id] => 17
[directors_count] => 0
)
)
This will work for you for sure. Check out the below methods.
array_column
array_unique
array_multisort
array_intersect_key
array_multisort(array_column($array, 'directors_count'), SORT_DESC,
array_column($array, 'user_id'), SORT_DESC,
$array);
$tempArr = array_unique(array_column($array, 'directors_count'));
$final_array = array_intersect_key($array, $tempArr);
print_r($final_array);
Output:
Array (
[0] => Array ( [user_id] => 10 [directors_count] => 6 )
[1] => Array ( [user_id] => 12 [directors_count] => 5 )
[2] => Array ( [user_id] => 15 [directors_count] => 2 )
[4] => Array ( [user_id] => 16 [directors_count] => 1 )
[7] => Array ( [user_id] => 17 [directors_count] => 0 )
)
DEMO: https://3v4l.org/JV0Kn

Remove Line in a WP array

I've an Array (wordpress) and would like to remove or add some parts. I don't know if some of my needs are possible and how to do it.
These are my needs: Remove [LineRoot] but keep all [line] (just move to parent in order to have all [Line] at the same level than [LineRoot] )
Array
(
[0] => Array
(
[Order] => 679
[LivraisonPrenom] => Joe
[LineRoot] => Array
(
[Line] => Array
(
[0] => Array
(
[ll] => hh
[Id] => 20
[SKU] => A104
[Quantity] => 1
)
)
[Line] => Array
(
[0] => Array
(
[ll] => hh
[Id] => 22
[SKU] => A105
[Quantity] => 1
)
)
)
[Meta-LangueBL] =>
[CheckoutAddOns] => Array
(
[CheckoutAddOn] => Array
(
[0] => Array
(
[ID] => 2
[Name] => Livraison
[Cost] => -48.33
)
)
)
)
)
Here is a list of PHP functions that can help you
unset
array_unshift
array_merge
array_splice
Good luck :)

How to efficiently extract a sub array based upon a set of keys

I have an array of arrays $data.
With
print_r($data);
returning
Array (
[1401] => Array ( [0] => 94 [1] => 2 [2] => 159 )
[1402] => Array ( [0] => 94 [1] => 2 [2] => 50 [3] => 23 [4] => 159 )
[1403] => Array ( [0] => 94 [1] => 2 [2] => 50 )
[1404] => Array ( [0] => 94 [1] => 90 [2] => 50 [3] => 23 )
[1405] => Array ( [0] => 94 [1] => 90 )
[1406] => Array ( [0] => 94 [1] => 90 [2] => 23 )
[1407] => Array ( [0] => 94 [1] => 90 [2] => 50 )
)
The keys are a set of numbers. And I need to extract from this array a sub array which has only the keys stored in another variable.
$toextract=array(1402,1406);
Apart building one by one the new array with a loop, is there a simpler way. I will need to run this command multiple times so it is quite important that is fast.
$result = array_intersect_key($data, array_flip(array(1402, 1406));
array_intersect_key()

Categories