Change structure multidimensional array - php

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
)

Related

How to map multidimensional array

I have an array of arrays that I need to consolidate into another array.
I have tried mapping over it, matching object_id, and gathering all account_ids for said object_id, but all my attempts are not even close as I am trying to learn PHP
This is the original array
[0] => Array
(
[rank] => 0
[id] => 6
[object_id] => 3
[account_id] => 13
)
[1] => Array
(
[rank] => 1
[id] => 7
[object_id] => 3
[account_id] => 565
)
[2] => Array
(
[rank] => 2
[id] => 1823
[object_id] => 825
[account_id] => 563
)
[3] => Array
(
[rank] => 3
[id] => 1824
[object_id] => 825
[account_id] => 564
)
[4] => Array
(
[rank] => 4
[id] => 1825
[object_id] => 825
[account_id] => 565
)
[5] => Array
(
[rank] => 5
[id] => 7187
[object_id] => 3113
[account_id] => 564
)
[6] => Array
(
[rank] => 6
[id] => 7188
[object_id] => 3113
[account_id] => 565
)
This is the desired result
[3] => [13, 565],
[825] => [563, 564, 565],
[3113] => [564, 565],
You need to create a new array by using object_id index.
Example:
<?
$array = array(
array('rank'=>0,'id'=>6,'object_id'=>3,'account_id'=>13),
array('rank'=>1,'id'=>7,'object_id'=>3,'account_id'=>565),
array('rank'=>2,'id'=>1823,'object_id'=>825,'account_id'=>563),
array('rank'=>3,'id'=>1824,'object_id'=>825,'account_id'=>564),
array('rank'=>4,'id'=>1825,'object_id'=>825,'account_id'=>565),
array('rank'=>5,'id'=>7187,'object_id'=>3113,'account_id'=>564),
array('rank'=>6,'id'=>7188,'object_id'=>3113,'account_id'=>565),
);
$newArray = array(); // initiliaze array
foreach ($array as $key => $value) {
$newArray[$value['object_id']][] = $value['account_id']; // save it in group
}
echo "<pre>";
print_r($newArray); // result
?>
Running Example

PHP trimensional array

I was wondering if someone could help me with this looping array in PHP as I am still learning the language.
I need to loop into the 3-dimensional (3D) array below in order to:
Check the [room_type_id] values and if its true to a specific value, I would return [check_in_times] array value for this same parent array.
For example:
if [room_type_ids] = 261
I would need to return [check_in_times] = 1
if [room_type_ids] = 36
I would need to return [check_in_times] = 2
My array is:
$myarray = Array (
[0] => Array (
[check_in_times] => Array (
[0] => 1
)
[room_type_ids] => Array (
[0] => 261
[1] => 281
[2] => 283
[3] => 292
[4] => 296
[5] => 365
[6] => 381
[7] => 387
[8] => 389
[9] => 730
)
[season_ids] => Array (
[0] => 0
)
)
[1] => Array (
[check_in_times] => Array (
[0] => 2
)
[room_type_ids] => Array (
[0] => 36
[1] => 38
[2] => 300
[3] => 318
[4] => 336
[5] => 889
[6] => 897
[7] => 728
[8] => 745
[9] => 747
)
[season_ids] => Array (
[0] => 0
)
)
)
Thank you!
Something like:
$room = 261;
$checks = 0;
foreach ($myarray as $subset)
{
if (in_array($room, $subset['room_type_ids']))
{
$checks = $subset['check_in_times'][0];
break;
}
}

How to separate two array by id

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.

How to get a sub-vale from a multi-dimensional array with only 1 sub-value

$gpph_array=
Array (
[0] => Array ( [product_id] => 83 [GPPH] => 18.80 )
[1] => Array ( [product_id] => 93 [GPPH] => 20.55 )
[2] => Array ( [product_id] => 94 [GPPH] => 30.75 )
[3] => Array ( [product_id] => 109 [GPPH] => 5.60 )
)
Lets say I have a product_id of 109 how do I extract only the associated GPPH value of 5.60 with php?
<?php
echo '[GPPH] where [product_id)=>109';
?>
I've read many post dealing with multidimensional arrays but only seen people loop through results. Any help is very much appreciated.
Thank you Vulcan that is awesome foresight for what I was trying to accomplish.
I was using this method to create my array:
$gpph_array=array();
while($grow=mysql_fetch_assoc($gquery)) {
$gpph_array[] = $grow;
}
which was creating the multi-dimensial array,
like
Array (
[0] => Array ( [product_id] => 83 [GPPH] => 18.80 )
[1] => Array ( [product_id] => 93 [GPPH] => 20.55 )
[2] => Array ( [product_id] => 94 [GPPH] => 30.75 )
[3] => Array ( [product_id] => 109 [GPPH] => 5.60 )
)
by changing to
$gpph_array=array();
while($grow=mysql_fetch_assoc($gquery)) {
$gpph_array[$grow['product_id']]=$grow['GPPH'];
}
I got a single array which is much easier to pluck values from since I will know the product id.
Array ( [83] => 18.80 [93] => 20.55 [94] => 30.75 [109] => 5.60)
Great thinking Vulcan

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