Multidimentional Associative array with array values - php

i wanted to covert array values into a multidimensional associative array.
Array size can be dynamic.
Array to be converted:
Array
(
[0] => abc
[1] => 0
[2] => xyz
[3] => 0
)
Expected Output:
Array
(
[abc] => Array
(
[0] => Array
(
[xyz] => Array
(
[0] =>
)
)
)
)
Tried with popping first key, but that has no luck...

Try this..
$tmpArr = array('Abc', '0', 'ABC','0');
$array = array();
foreach (array_reverse($tmpArr) as $arr)
$array = array($arr => $array);

Related

multidimensional array values matched with another index array values then remove values from multidimensional in PHP 7

I am having below two arrays
$firstArray = Array
(
[1] => Array
(
[MemberList] => Array
(
[0] => 100
[1] => 5d6
[2] => 5d7
)
)
[3] => Array
(
[MemberList] => Array
(
[0] => 5d8
[1] => 200
)
)
)
$secondArray = Array
(
[0] => 100
[1] => 200
)
my question is if suppose $secondArray array values matched with $firstArray array then i have to remove the values from $firstArray
my expected output
$firstArray = Array
(
[1] => Array
(
[MemberList] => Array
(
[0] => 5d6
[1] => 5d7
)
)
[3] => Array
(
[MemberList] => Array
(
[0] => 5d8
)
)
)
$newArray = array_map(
function ($v) use ($secondArray) {
return ['MemberList' => array_diff($v['MemberList'], $secondArray)];
},
$firstArray
);
Fiddle here.

php - set values as index in array

This is my array:
Array
(
[0] => Array
(
[0] => Q1
[1] => 100
)
[1] => Array
(
[0] => Q2
[1] => 200
)
[2] => Array
(
[0] => Q3
[1] => 300
)
)
I want to have an array like this:
Array
(
[Q1] => 100
[Q2] => 200
[Q3] => 300
)
So basically I want to split all arrays in one, and 0 key of all multi arrays will be key in the new array and 1 value in multi-array will be value in the new array. I tried with array_combine, but that does not work for me, any ideas?
There's a function for that:
$result = array_column($array, 1, 0);
Failing that just loop:
foreach($array as $v) { $result[$v[0]] = $v[1]; }
Use this straight-forward solution :
$arr = [
['Q1',100],
['Q2',200],
['Q3',300]
];
$res = array_combine(
array_column($arr, 0),
array_column($arr, 1)
);
print_r($res);

Merge a simple array and nested array according to same keys

I have array 1 like this
Array
(
[0] => 1
[1] => 2
)
Second array would be
Array
(
[0] => Array
(
[FullName] => Bhupat Chippa
)
[1] => Array
(
[FullName] => Dvs Patel
)
)
I want to merge it the way values would be added to second array with same keys. Desired Output will look like this or some way around so that I can use the Array 1's value with Second Array Only:
Array
(
[0] => Array
(
[FullName] => Bhupat Chippa
[0] => 1
)
[1] => Array
(
[FullName] => Dvs Patel
[1] => 2
)
)
You can apply simple foreach() to do that
$final = [];
foreach($array2 as $key =>$arr2 ){
$final[$key]['FullName'] = $arr2['FullName'];
$final[$key][$key] = $array1[$key];
}
print_r($final);
Output:- https://eval.in/1010437
If both arrays are of the same length, you might use array_map passing the array_keys as the second parameter:
$array1 = ["1", "2"];
$array2 = [
["FullName" => "Bhupat Chippa"],
["FullName" => "Dvs Patel"]
];
$result = array_map(function($x, $y) use ($array1){
$x[$y] = $array1[$y];
return $x;
}, $array2, array_keys($array1));
print_r($result);
Demo
That will give you:
Array
(
[0] => Array
(
[FullName] => Bhupat Chippa
[0] => 1
)
[1] => Array
(
[FullName] => Dvs Patel
[1] => 2
)
)

Removing subarrays that share a key-value pair with another multidimensional array

I have 2 arrays as below. I want to remove data from array2 if array1 has the stu_id. final array should be like result_array.
$array1 = Array
(
[0] => Array
(
[stu_id] => 1
[name] => mr.a
)
[1] => Array
(
[stu_id] => 3
[name] => mr.b
)
)
$array2 = Array
(
[0] => Array
(
[id] => 1
[stu_id] => 1
[data] => abc
)
[1] => Array
(
[id] => 2
[stu_id] => 2
[data] => xyz
)
[3] => Array
(
[id] => 3
[stu_id] => 3
[data] => aaa
)
)
$result_array = Array
(
[0] => Array
(
[id] => 2
[stu_id] => 2
[data] => xyz
)
)
I tried array_diff, $result_array = array_diff($array2, $array1); but it's not working.
Please help me to do this.
Temporarily assign keys using array_column() with stud_id (NULL retains the full subarray data), then use array_diff_key() to filter, and array_values() to reset the keys:
Code: (Demo)
$array1=[
['stu_id'=>1,'name'=>'mr.a'],
['stu_id'=>3,'name'=>'mr.b']
];
$array2=[
['id'=>1,'stu_id'=>1,'data'=>'abc'],
['id'=>2,'stu_id'=>2,'data'=>'xyz'],
['id'=>3,'stu_id'=>3,'data'=>'aaa']
];
//var_export(array_column($array1,NULL,'stu_id'));
//var_export(array_column($array2,NULL,'stu_id'));
var_export(array_values(array_diff_key(array_column($array2,NULL,'stu_id'),array_column($array1,NULL,'stu_id'))));
Output:
array (
0 =>
array (
'id' => 2,
'stu_id' => 2,
'data' => 'xyz',
),
)
If you'd like to use a foreach loop structure, generate a filtering array of stu_id's from $array1 and write a conditional check on each iteration of $array2. This method doesn't not modify the original arrays, so they can be reused "down script".
Code:
$stu_ids=array_column($array1,'stu_id');
foreach($array2 as $row){
if(!in_array($row['stu_id'],$stu_ids)){
$result[]=$row; // auto-index the qualifying subarrays
}
}
var_export($result);
// same result as above method
foreach($array1 as $data1){
foreach($array2 as $k => $data2){
if($data2["stu_id"] == $data1["stu_id"]){
unset($array2[$k]);
break;
}
}
}
$result_array = $array2;

PHP - Merge two arrays on same key

I'd like to merge two arrays on same key.
Here's the 1st array :
Array
(
[2052] => Array
(
[495] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 79755
)
[1] => Array
(
[ID_RI_BELANJA] => 79755
)
)
)
[4566] => Array
(
[488] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 231610
)
[1] => Array
(
[ID_RI_BELANJA] => 231610
)
)
)
)
And this is the 2nd array
Array
(
[2052] => Array
(
[495] => Array
(
[TOTAL_RI] => 1000000
[TOTAL_ANGGARAN] => 500000
)
)
[4566] => Array
(
[488] => Array
(
[TOTAL_RI] => 2000000
[TOTAL_ANGGARAN] => 1000000
)
)
)
And i'd like merge that two arrays to be like this :
Array
(
[2052] => Array
(
[495] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 79755
)
[1] => Array
(
[ID_RI_BELANJA] => 79755
)
[TOTAL_RI] => 1000000
[TOTAL_ANGGARAN] => 500000
)
)
[4566] => Array
(
[488] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 231610
)
[1] => Array
(
[ID_RI_BELANJA] => 231610
)
[TOTAL_RI] => 2000000
[TOTAL_ANGGARAN] => 1000000
)
)
)
This is my first project and I don't know what to do.
Can anyone tell me how to do that?
Pls
If your arrays have same Key then:
$array1 = array(); //put your value in this array
$array2 = array(); //put your value in this array
$array3 = array();
$array3[] = $array1;
$array3[] = $array2;
Assuming your two arrays are $array1 and $array 2 respectively, try this:
foreach($array1 as $k1 => $v1) {
foreach($v1 as $k2 => $v2) {
foreach($v2 as $k3 => $v3) {
$new[$k1][$k2][$k3] = $array1[$k1][$k2][$k3];
$new[$k1][$k2] = array_merge($new[$k1][$k2], $array2[$k1][$k2]);
}
}
}
=> store the all value in this variable
$Arr1 //put your value in this array
$Arr2 //put your value in this array
=> And merge it
$ResponseDetails = array_merge( (array)$Arr1, (array)$Arr2);
array_replace_recursive should do the job:
// $arr1 is the 1st array, $arr2 - is your 2nd array
$result = array_replace_recursive($arr1, $arr2); // now the $result variable contains the expected merged result
http://php.net/manual/en/function.array-replace-recursive.php

Categories