I want to insert multiple array to XML in PHP Codeigniter - php

I got many values from html rows table and I want to submit with XML Array, but I don't know how to change to array multidimension in php array.
and this is my array
Array
(
[RSVRS_TRX_H_ID] => Array
(
[0] => 2
[1] => 2
)
[RSVRS_TRX_D_ID] => Array
(
[0] => 3
[1] => 4
)
[PROCESS_STAT] =>
[IMG_CODE] => Array
(
[0] => KTP_IMG
[1] => KWIT_IMG
)
[IMG_DATA] => Array
(
[0] => iniktpimg
[1] => inilkwit
)
[NEED_REVISION] => Array
(
[0] => 1
[1] => 0
)
[NOTES] => Array
(
[0] => ya
[1] => tidak
)
[USR_CRT] => 30305
)
and i want to multidimensional array like this or it's just to looping?
Array
(
Array(
[RSVRS_TRX_H_ID] => 2
[RSVRS_TRX_D_ID] => 3
[PROCESS_STAT] =>
[IMG_CODE] => KTP_IMG
[IMG_DATA] => iniktpimg
[NEED_REVISION] => 1
[NOTES] => ya
[USR_CRT] => 30305
),
Array(
[RSVRS_TRX_H_ID] => 2
[RSVRS_TRX_D_ID] => 4
[PROCESS_STAT] =>
[IMG_CODE] => KWIT_IMG
[IMG_DATA] => inilkwit
[NEED_REVISION] => 0
[NOTES] => tidak
[USR_CRT] => 30305
)
)

loop over all array get key and value of array. loop over each value of array, assign to each key its values at numbers index of result number index come from value of array. if value is not array assign value with key to each number index of result.
array_walk($array, function($value, $key)use (&$result){
if(is_array($value) && !is_null($value)){
foreach ($value as $k => $v) {
$result[$k][$key]=$v;
}
}else
{
foreach ($result as $rkey => $rvalue) {
$result[$rkey][$key] =$value;
}
}
});
print_r($result);
Output
Array (
[0] => Array (
[RSVRS_TRX_H_ID] => 2
[RSVRS_TRX_D_ID] => 3
[PROCESS_STAT] =>
[IMG_CODE] => KTP_IMG
[IMG_DATA] => iniktpimg
[NEED_REVISION] => 1
[NOTES] => ya
[USR_CRT] => 30305 )
[1] => Array (
[RSVRS_TRX_H_ID] => 2
[RSVRS_TRX_D_ID] => 4
[PROCESS_STAT] =>
[IMG_CODE] => KWIT_IMG
[IMG_DATA] => inilkwit
[NEED_REVISION] => 0
[NOTES] => tidak
[USR_CRT] => 30305 ) )

Related

Create array key from value. i have created array define to bellow.i have differentiate actual result and expected result

Actual Array
Array
(
[0] => Array
(
[sub_id] => 3
[sub_name] => tttt
[master_id] => 3
)
[1] => Array
(
[sub_id] => 4
[sub_name] => yyyy
[master_id] => 3
)
[2] => Array
(
[sub_id] => 5
[sub_name] => kkkk
[master_id] => 4
)
)
Expected Result
Array
(
[3] => Array(
[0] => Array
(
[sub_id] => 3
[sub_name] => tttt
[master_id] => 3
)
[1] => Array
(
[sub_id] => 4
[sub_name] => yyyy
[master_id] => 3
)
)
[4] => Array(
[0] => Array
(
[sub_id] => 5
[sub_name] => kkkk
[master_id] => 4
)
)
)
You can create a new array and set value of master id as the index and put the value in it.
$data = array();
foreach($array as $key=>$value){
$data[$value['master_id']][] = $value;
}
$actualArray = array(array('sub_id' => 3, 'sub_name' => 'tttt', 'master_id' => 3),array('sub_id' => 4, 'sub_name' => 'yyyy', 'master_id' => 3),array('sub_id' => 5, 'sub_name' => 'kkkk', 'master_id' => 4));
$tempArray = array_unique(array_column($actualArray, 'master_id'));
$uniqueArray = array_intersect_key($actualArray, $tempArray);
foreach ($uniqueArray as $key => $masters) {
$count = 0;
foreach ($actualArray as $key1 => $actuals) {
if($masters['master_id'] == $actuals['master_id']){
$expectedArray[$key][$count] = $actuals;
$count++;
}
}
}

Merge 2 array same key

I'd like to merge two arrays on same key. I've tried my best to do it but unable to get success. I've added both the Arrays below. Kindly check both the Arrays in detail and help me how to merge it using same key.
Here's the 1st array :
Array
(
[1] => Array
(
[costprice1] => 500
[margin1] => 20
)
[2] => Array
(
[costprice2] => 600
[margin2] => 15
)
[3] => Array
(
[costprice3] => 700
[margin3] => 25
)
)
Here's the 2 array :
Array
(
[1] => Array
(
[entityType1] => Products1
)
[2] => Array
(
[entityType2] => Products2
)
[3] => Array
(
[entityType3] => Products3
)
)
i want to need like that array please suggestion me
Array
(
[1] => Array
(
[entityType1] => Products1
[costprice1] => 500
[margin1] => 20
)
[2] => Array
(
[entityType2] => Products2
[costprice2] => 600
[margin2] => 15
)
[3] => Array
(
[entityType3] => Products3
[costprice3] => 700
[margin3] => 25
)
)
please help me how to merge two array
Try this :
foreach($array1 as $key => $value) {
$array1[$key]['entityType'.$key] = $array2[$key]['entityType'.$key];
}
print_r($array1);
<?php
$array1 = [
1 => [
'costprice1' => 500,
'margin1' => 20
],
2 => [
'costprice2' => 600,
'margin2' => 15
],
3 => [
'costprice2' => 700,
'margin2' => 25
],
];
$array2 = [
1 => ['entityType1' => 'Products1'],
2 => ['entityType2' => 'Products2'],
3 => ['entityType3' => 'Products3'],
];
array_walk($array2, function(&$v, $k)use($array1){
$v = array_merge($v, $array1[$k]);
});
print_r($array2);
Output:
Array
(
[1] => Array
(
[entityType1] => Products1
[costprice1] => 500
[margin1] => 20
)
[2] => Array
(
[entityType2] => Products2
[costprice2] => 600
[margin2] => 15
)
[3] => Array
(
[entityType3] => Products3
[costprice2] => 700
[margin2] => 25
)
)
https://eval.in/618561

Get particular data with multiple index value with/without loop in cakephp

I have an array in which i want only lineNo and Isdirty field in each array .
My demo code is
Array
(
[CodeConfiguration] => Array
(
[0] => Array
(
[ObjectType] => 12
[LineNo] => 1
[CompanyID] => 1
[BranchID] => 46
[ModifiedDate] => 2014-04-25 05:10:15
[RevisionNumber] => 6
[IsDirty] =>
)
)
[TaxConfiguration] => Array
(
[0] => Array
(
[LineNo] => 2
[IsDirty] => 1
[ItemGroupID] =>
[TaxID] =>
[CalculationType_080] => 430
[RevisionNumber] => 1
)
[1] => Array
(
[LineNo] => 1
[IsDirty] => 1
[ItemGroupID] =>
[TaxID] =>
[CalculationType_080] => 372
[RevisionNumber] => 1
)
)
)
Only LineNo And Isdirty field want in every index array .So please suggest me solution.
You can sue following;
$finalArr = array();
foreach ($arr as $key => $item) {
foreach ($item as $k => $v) {
$finalArr[$key][] = array(
"LineNo" => $v["LineNo"],
"IsDirty" => $v["IsDirty"]
);
}
}
Here is a working demo: Demo

Find a value & key in a multidimensional array [duplicate]

This question already has answers here:
Find value and key in multidimensional array
(8 answers)
Closed 9 years ago.
I have 2 arrays, I need to find if one of the values in array one matches one of the values in array two, a multi-dimensional array. I also need to check that the value from array one is in a specific key in array two, the "principal" key as the "authority" key may also hold this value.
here is array one:
Array
(
[0] => 17
[1] => 6
[2] => 3
[3] => 2
)
and array two [actually slightly truncated for readability]:
Array
(
[modAccessResourceGroup] => Array
(
[3] => Array
(
[0] => Array
(
[principal] => 0
[authority] => 9999
[policy] => Array
(
[load] => 1
)
)
[1] => Array
(
[principal] => 2
[authority] => 10
[policy] => Array
(
[add_children] => 1
[create] => 1
[copy] => 1
[delete] => 1
[list] => 1
[load] => 1
[move] => 1
[publish] => 1
[remove] => 1
[save] => 1
[steal_lock] => 1
[undelete] => 1
[unpublish] => 1
[view] => 1
)
)
.... truncated ....
[13] => Array
(
[principal] => 16
[authority] => 9999
[policy] => Array
(
[load] => 1
)
)
)
[8] => Array
(
[0] => Array
(
[principal] => 0
[authority] => 9999
[policy] => Array
(
[load] => 1
)
)
[1] => Array
(
[principal] => 1
[authority] => 9999
[policy] => Array
(
[add_children] => 1
[create] => 1
[copy] => 1
[delete] => 1
[list] => 1
[load] => 1
[move] => 1
[publish] => 1
[remove] => 1
[save] => 1
[steal_lock] => 1
[undelete] => 1
[unpublish] => 1
[view] => 1
)
)
[2] => Array
(
[principal] => 22
[authority] => 9999
[policy] => Array
(
[add_children] => 1
[create] => 1
[copy] => 1
[delete] => 1
[list] => 1
[load] => 1
[move] => 1
[publish] => 1
[remove] => 1
[save] => 1
[steal_lock] => 1
[undelete] => 1
[unpublish] => 1
[view] => 1
)
)
)
)
)
I was using a series of foreach(){foreach(){foreach(){}}} but it seemed very messy and inefficient. Having some trouble getting my head around this. Any ideas?
A recursive function should do the trick:
$values = array(17, 6, 3, 2, 5);
function find($array, &$values) {
foreach ($array as $key => $element) {
if (is_array($element)) {
find($element, $values);
}
elseif ($key == 'principal') {
foreach ($values as $value) {
if ($element == $value) {
echo 'Found' . PHP_EOL;
// Do stuff
}
}
}
}
}
find($array, $values);
Several things come to mind. First, in situations like this, I will usually create a separate array with just the principal values so that I can loop over the first array and just use a simple in_array() check. Secondly, if you don't want to do that, you could do something using the array_walk_recursive() function or some of the recursive examples in array_search() to go through your second array.

How to Add a new Key and Merge the array values in multidimemsional array PHP

here's the result of my first function:
Array
(
[0] => Array
(
[MaidID] => 13
[Stores] => Array
(
[0] => 14
[1] => 5
)
)
[1] => Array
(
[MaidID] => 3
[Stores] => Array
(
[0] => 4
)
)
[2] => Array
(
[MaidID] => 41
[Stores] => Array
(
[0] => 14
)
)
)
Then, here's the result of my second function:
Array
(
[1] => Array
(
[MaidID] => 14
[Cash] => 10000
[Debit] => 0
[Credit] => 0
)
)
and here's should be the result:
Array ([0] => Array (
[MaidID] => 14
[Cash] => 10000.00
[Debit] => 0
[Credit] => 0
[MaidsID] => Array(
[0] => 13
[1] => 41
)
)
)
Is it possible to make it?I need to a new key name MaidsID pointing to the list of MaidID owned by more than one Stores.Please help me, Please be patience in my question, im just a beginner.Thank you so much.
this code work ok. $a is your first array $b is the second, $c is result
$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);`
I tested it here and it was ok. (Just replace $a with your first array and $b with seccond ).
Are you sure that the structure of your arrays is exactly as you have written above?? Maybe there is any problem with that.
You have puted array inside another array? (its not needed i think)
Howerver: For this code:
`$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
print_r($a);
echo "<br><br>================================================<br><br>";
print_r($b);
echo "<br><br>================================================<br><br>";
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);
print_r($c);
echo "<br><br>================================================<br><br>";`
The output is:
Array ( [0] => Array ( [Maid] => 1 [Stores] => Array ( [0] => 1 [1] => 5 ) ) [1] => Array ( [Maid] => 3 [Stores] => Array ( [0] => 4 ) ) [2] => Array ( [Maid] => 4 [Stores] => Array ( [0] => 1 ) ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) [MaidsID] => Array ( [0] => 1 [1] => 3 [2] => 4 ) )
================================================
Isn't this how you want the result?
Have a look at this. Maybe this can help you.
$result = array_merge($array1, $array2);

Categories