php - set values as index in array - php

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

Related

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

how to convert array 1 dimensional to be 2 dimensional in 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;
}

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;

how to count the array values with key

I have array like this i need to count by array values
Array ( [Cop] => Array ( [0] => Dozen [1] => Dozen [2] => Akls [3] => Akls ) [MSN] => Array ( [0] => Dozen ) [NeK] => Array ( [0] => Suhan [1] => Ebao ) [NetSE] => Array ( [0] => SuZhan [1] => Guhang ) )
For example
Array ( [Cop] => Array ( [0] => Dozen [1] => Dozen [2] => Akls [3] => Akls ))
In the Cop key i have two different values for cop so i need cop should be 2
Cop - 2
MSn - 1
NeK - 2
NetSE - 2
I need the count like above how can i do this ?
Try simply using array_map,count,& array_unique like as
array_map(function($v) {
return count(array_unique($v));
}, $arr);
Use array_unique() and then count.
count(array_unique($array['Cop']));// output 2
If you want to print for every key do following:
$array = array('Cop'=>array('Dozen','Dozen','Akls','Akls'), 'MSN'=> array('Dozen'), 'NeK'=> array('Suhan','Ebao'));
foreach($array as $key => &$value) {
$value = count(array_unique($array[$key]));
}
print_r($array);
Output:
Cop = 2
MSN = 1
NeK = 2
You should use array_count_values() for that, here's an example:
$data = array('cop' => array(0 => 'test', 1 => 'test', 2 => 'test2'));
foreach($data as $item){
$result = array_count_values($item);
print_r($result);
}
Outputs:
Array
(
[test] => 2
[test2] => 1
)

Creating a multidimensional array

I have 2 arrays, that I want to put into 1 multidimensional array
$array_result = array();
Array1 = a,b,c,d
Array2 = 1,2,3,4
The result that I want to get is
$array_result = [0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
) etc...
I can't work out how to do this. Then length of Array1 and Array2 varies as it is dynamic data.
Can someone point me in the right direction?
Try this
$arr1 = array(1,2,3,4);
$arr2 = array('a','b','c','d');
$arr3 = array();
for($i = 0;$i< count($arr1);$i++) {
$arr = array();
$arr[] = $arr2[$i];
$arr[] = $arr1[$i];
array_push($arr3,$arr);
}
Output
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
)
[2] => Array
(
[0] => c
[1] => 3
)
[3] => Array
(
[0] => d
[1] => 4
)
)
Codepad Demo
Use array_merge() function. It should do what you want to do.
$array_result=array_merge($array1, $array2, ...);

Categories