I have to admit that understanding the tables are big challenge for me so please dont judge me so hard...
This is my array:
Array
(
[names] => Array
(
[0] => Name1
[1] => Name2
[2] => Name1
)
[ids] => Array
(
[0] => 1
[1] => 2
[2] => 1
)
[quantities] => Array
(
[0] => 255
[1] => 2
[2] => 467
)
)
And i wish to sum "quantities" where names or ids are the same.
Example output should be:
Array
(
[names] => Array
(
[0] => Name1
[1] => Name2
)
[ids] => Array
(
[0] => 1
[1] => 2
)
[quantities] => Array
(
[0] => 722
[1] => 2
)
)
I know there is a function like "array_reduce" but don't know how to use it.
Thanks for help!
try this
$result = [];
foreach($array['ids'] as $key=>$val ){
if(array_key_exists($val, $result)){
$result[$val]['sum_quantity'] += $array['quantities'][$key];
}
else{
$result[$val]['sum_quantity'] = $array['quantities'][$key];
$result[$val]['name'] = $array['names'][$key];
$result[$val]['id'] = $array['ids'][$key];
}
}
and output will be like this
Array
(
[1] => Array //array key = id
(
['name'] => Name1,
['sum_quantity'] => 722,
['id'] => 1
)
[2] => Array
(
['name'] => Name2,
['sum_quantity'] => 2,
['id'] => 2
)
)
You can do this way :
$testArray['names'][0]='name1';
$testArray['names'][1]='name2';
$testArray['names'][2]='name1';
$testArray['ids'][0]=1;
$testArray['ids'][1]=2;
$testArray['ids'][2]=1;
$testArray['quantities'][0]=255;
$testArray['quantities'][1]=2;
$testArray['quantities'][2]=467;
echo "<pre>";
print_r($testArray);
$unqArray['names']=array();
$unqArray['ids']=array();
$unqArray['quantities']=array();
foreach($testArray['ids'] as $key=>$value)
{
if(!in_array($value,$unqArray['ids']))
{
$unqArray['names'][]=$testArray['names'][$key];
$unqArray['ids'][]=$testArray['ids'][$key];
$quantity=0;
foreach($testArray['ids'] as $keyId=>$valueId)
{
if($valueId==$value)
{
$quantity+=$testArray['quantities'][$keyId];
}
}
$unqArray['quantities'][]=$quantity;
}
}
print_r($unqArray);
echo "</pre>";
Related
i want to filter this array and get all arrays where [1] => 1. So the php code must return Array [1], [2] and [4]. I have no idea how to do it, so I will be grateful for any help.
Array
(
[0] => Array
(
[0] => 1
[1] => 1
[2] => data1
)
[1] => Array
(
[0] => 2
[1] => 1
[2] => data2
)
[2] => Array
(
[0] => 3
[1] => 2
[2] => data3
)
[3] => Array
(
[0] => 4
[1] => 2
[2] => data4
)
[4] => Array
(
[0] => 5
[1] => 1
[2] => data5
)
[5] => Array
(
[0] => 6
[1] => 3
[2] => data6
)
)
Using a simple foreach loop and appending to a new array when the item matches as required will work:
$in = [
[1,1,'data1'],
[2,1,'data2'],
[3,2,'data3'],
[4,2,'data4'],
[5,1,'data5'],
[6,3,'data6'],
];
$out = [];
foreach ($in as $arr) {
if ($arr[1] == 1) {
$out[] = $arr;
}
}
var_dump($out);
Assuming your parent array was called $master:
foreach ($master as $m)
{
if ($m[1] != 1)
{
unset($m);
}
}
return $master;
I have two array two array. First is multidimensional and other is single dimensional. I want to find difference between them. How do I found.
$arrayresult
Array 1
Array (
[0] => Array ( [0] => ishani.lad [1] => 9033187384 )
[1] => Array ( [0] => rajkumar.prajapati [1] => 8460078459 )
[2] => Array ( [0] => lokesh.bhandari [1] => 9687060900 )
[3] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[4] => Array ( [0] => vishal.dake [1] => 9879815299 )
[5] => Array ( [0] => mohsin [1] => 8347163123 )
)
$useduser
Array 2
Array (
[0] => ishani.lad
[1] => rajkumar.prajapati
[2] => lokesh.bhandari
)
I need difference as result as below
Result
Array (
[0] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[1] => Array ( [0] => vishal.dake [1] => 9879815299 )
[2] => Array ( [0] => mohsin [1] => 8347163123 )
)
I have used solution as
$resultremainig = [];
foreach($arrayresult as $val2){
if(!in_array($val2[0], $useduser)){
echo $val2[0]."<br>";
$resultremainig[] = $val2;
}
}
But it show last record also. Result of above code is as below. It always show me last record in second array's also
Array (
[0] => Array ( [0] => lokesh.bhandari [1] => 9687060900 )
[1] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[2] => Array ( [0] => vishal.dake [1] => 9879815299 )
[3] => Array ( [0] => mohsin [1] => 8347163123 )
)
If you wanted you could try using nested loops like so:
<?php
$arrOne = $arrFinal = [
["ishani.lad", 9033187384],
["rajkumar.prajapati", 8460078459],
["lokesh.bhandari" , 9687060900],
["shishanshu.rai" , 8401915337],
["vishal.dake" , 9879815299],
["mohsin" , 8347163123],
];
$arrTwo = [
"ishani.lad",
"rajkumar.prajapati",
"lokesh.bhandari",
];
foreach($arrOne as $key=>$item){
foreach($arrTwo as $k=>$v){
if(in_array($v, $item)){
unset($arrFinal[$key]);
}
}
}
var_dump($arrFinal);
// PRODUCES:::
array (size=3)
3 =>
array (size=2)
0 => string 'shishanshu.rai' (length=14)
1 => int 8401915337
4 =>
array (size=2)
0 => string 'vishal.dake' (length=11)
1 => int 9879815299
5 =>
array (size=2)
0 => string 'mohsin' (length=6)
1 => int 8347163123
You could use array_filter():
$output = array_filter($arrayresult, function($a) use ($useduser) {
return !in_array($a[0], $useduser);
});
Hi You can also try this
$one = array(array('ishani.lad',9033187384),array('rajkumar.prajapati',8460078459),array('lokesh.bhandari',9687060900),array('shishanshu.rai',8401915337),array('vishal.dake',9879815299),array('mohsin',8347163123));
$two = array('ishani.lad','rajkumar.prajapati','lokesh.bhandari');
foreach($one as $array){
if(!in_array($array[0],$two)){
$final[] = $array;
}
}
echo "<pre>";print_r($final);
Output
Array
(
[0] => Array
(
[0] => shishanshu.rai
[1] => 8401915337
)
[1] => Array
(
[0] => vishal.dake
[1] => 9879815299
)
[2] => Array
(
[0] => mohsin
[1] => 8347163123
)
)
Trim the value before checking in $useduser array
$resultremainig = [];
foreach($arrayresult as $val2){
// this removes any extra spaces from the search string
if(!in_array(trim($val2[0]), $useduser)){
echo $val2[0]."<br>";
$resultremainig[] = $val2;
}
You need to use the array_diff function.
Store your 2 arrays in variables and compare them.
I have one array which I am getting from database query response.
Now I want to count same categories and print in option_name array.
I have tried it with different array functions but want get desire output.
I have one idea to take new array and push it with foreach loop but not much idea of how can i achieve using code. Please help me to solve it.
Array
(
[0] => Array
(
[CNC] => Array
(
[id] => 5
[category_id] => 68
)
[GVO] => Array
(
[option_name] => Actors
)
)
[1] => Array
(
[CNC] => Array
(
[id] => 11
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[2] => Array
(
[CNC] => Array
(
[id] => 3
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[3] => Array
(
[CNC] => Array
(
[id] => 4
[category_id] => 74
)
[GVO] => Array
(
[option_name] => Musician
)
)
[4] => Array
(
[CNC] => Array
(
[id] => 7
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
[5] => Array
(
[CNC] => Array
(
[id] => 6
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
)
Desire Output:
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)
Thanks in advance!
You simply need to loop through the array using foreach like as
$result = [];
foreach($arr as $k => $v){
if(isset($result[$v['GVO']['option_name']])){
$result[$v['GVO']['option_name']] += 1;
}else{
$result[$v['GVO']['option_name']] = 1;
}
}
print_R($result);
You can count the option_name values by incrementing a counter in an associative array where the key is the option_name:
$counts = [];
foreach($array as $v) {
if(!isset($counts[$v['GVO']['option_name']])) {
$counts[$v['GVO']['option_name']] = 0;
}
$counts[$v['GVO']['option_name']]++;
}
print_r($counts);
Try this:
$new_arr = array();
foreach(array_column($your_arr, 'option_name') as $value){
if(in_array($value, $new_array)){
$new_array[$value] = $new_array[$value]+1;
}else{
$new_array[$value] = 1;
}
}
Output
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)
I am having an array like this.
Array
(
[0] => Array
(
[0] => a~226
[1] => a~228
)
[1] => Array
(
[0] => b~123
[1] => b~209
)
[2] => Array
(
[0] => c~161
[1] => c~140
)
)
I want to explode this array using ~ symbol and i want value to be a key in php array.i want an array like this.Kindly help me write the code.
Array
(
[0] => Array
(
[a] => 226
[a] => 228
)
[1] => Array
(
[b] => 123
[b] => 209
)
[2] => Array
(
[c] => 161
[c] => 140
)
)
Thanks in advance...
You cannot have such an array.
The keys must be unique (Like Mark Baker say).
You can have something like this:
Array
(
[a] => Array
(
[0] => 226
[1] => 228
)
[b] => Array
(
[0] => 123
[1] => 209
)
[c] => Array
(
[0] => 161
[1] => 140
)
)
The code to do this:
$array = array(
array("a~226", "a~228"),
array("b~123", "b~209"),
array("c~161", "c~140")
);
$result = array();
foreach($array as $inner_array) {
foreach($inner_array as $value) {
$spitted = explode("~", $value);
$result[$spitted[0]][] = end($spitted);
}
}
An working example: http://codepad.viper-7.com/znhhqB
try this
$arr_new = array();
foreach($arr_main as $key=>$arr)
{
foreach($arr as $k=>$val)
{
$str = explode("~",$val);
$arr_new[$key][$str[0].$k]=$str[1];
}
}
will maintain the index as a0, a1
I have following arrays:
1) for total placed
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totalplaced] => 8
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totalplaced] => 1
)
)
)
2) for total working
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totalworking] => 4
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totalworking] => 1
)
)
)
3) for total trained
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totaltrained] => 8
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totaltrained] => 1
)
)
)
I wanted to merge these arrays so that the resultant array should look like this
[newarray] => Array(
[0] => Array (
[centers] => Array
(
[name] => delhi
[id] => 1
[totalplaced] => 8
[totalworking] => 4
[totaltrained] => 8
)
)
[1]=> Array(
[centers] => Array
(
[name] => mumbai
[id] => 2
[totalplaced] => 1
[totalworking] => 1
[totaltrained] => 1
)
)
)
This is the tabular representation of the above data which i want to display
centername totalplaced totalworking totaltrained
delhi 8 4 8
mumbai 1 1 1
Please help me on this.
Thanks
Pankaj Khurana
The difficulty here is that PHP's functions such as array_merge() and array_merge_recursive() will not merge data into numeric keys, but rather will re-key any duplicate numeric key. So for example given two arrays:
array(
'test' => 'abc',
0 => 'xyz'
);
array(
'test' => 'def',
0 => 'uvw'
);
Merging them together with array_merge() will produce an array like:
array(
'test' => 'def',
0 => 'xyz',
1 => 'uvw'
);
So, you need a custom function to be "additive" on any key, regardless of whether it is a string or numeric key. Try this:
function mixed_key_array_merge() {
$args = func_get_args();
$result = array();
foreach ($args as $arg) {
// discard non-array arguments; maybe this could be better handled
if (!is_array($arg)) {
continue;
}
foreach ($arg as $key => $value) {
if (!isset($result[$key])) {
$result[$key] = $value;
} else if (is_array($result[$key])) {
$result[$key] = call_user_func_array('mixed_key_array_merge',array($result[$key],$value));
}
}
}
return $result;
}