how to convert array 1 dimensional to be 2 dimensional in php - php

I have one array as :
Array
(
[0] => 11
[1] => 4
)
I want convert it to be array like this :
Array
(
[0] => array
(
[0] => 11
)
[1] => array
(
[0] => 4
)
)
Thanks you very much for your answer

Assuming your array is in the variable $array, you could simply use array_map:
$array = array_map( function( $val ) {
return array( $val );
}, $array );

You can cast each element as an array like this:
Code:
$array=[11,4];
$array=array_map(function($v){return (array)$v;},$array);
var_export($array);
Output:
array (
0 =>
array (
0 => 11,
),
1 =>
array (
0 => 4,
),
)
Or write a foreach loop and make the elements modifiable by reference:
foreach($array as &$v){
$v=(array)$v;
}

Related

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);

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

Multidimentional Associative array with array values

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);

PHP: merge two multi-dimensional arrays without merging depth

I have two arrays:
print_r($array_one):
Array
(
[a] => Array (
[param1] => 1
[param2] => 2
)
[b] => Array (
[param1] => 3
[param2] => 4
)
)
print_r($array_two):
Array
(
[param3] => 5
)
I want to form an array that looks like:
Array
(
Array
(
[a] => Array (
[param1] => 1
[param2] => 2
)
[b] => Array (
[param1] => 3
[param2] => 4
)
)
Array
(
[param3] => 5
)
)
But both array_merge() and array_merge_recursive() will create:
Array
(
[a] => Array (
[param1] => 1
[param2] => 2
)
[b] => Array (
[param1] => 3
[param2] => 4
)
[param3] => 5
)
The original array was intended for a json encode to make a dimensional json file, but the result of merge of arrays is not what I want.
Would someone please point out how to make it work?
Couldn't you just do something like:
$new_array = array();
$new_array[] = $array_one;
$new_array[] = $array_two;
Something like $newArray = array($array1, $array2); ?
If I am reading this right, this is what you are looking for:
$output = array($array_one, $array_two);

Categories