I'm having two arrays like this
$whole_orders
Array
(
[2] => Array
(
[0] => Array
(
[id] => 3
[food_id] => 1
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[food_id] => 2
)
[1] => Array
(
[id] => 1
[food_id] => 1
)
)
)
And $array
Array
(
[2] => Array
(
[0] => Array
(
[count] => 1
[subtotal] => 103.42
[tax] => 18.42
)
)
[1] => Array
(
[0] => Array
(
[count] => 2
[subtotal] => 303.42
[tax] => 38.42
)
)
)
Here I'm having two arrays such as $whole_orders & $array from which I need to merge the $array values into the $whole_orders..
And the $whole_orders having nested values which are dynamic..
Finally My array should be like this..
Array
(
[2] => Array
(
[0] => Array
(
[id] => 3
[food_id] => 1
)
[1] => Array
(
[count] => 1
[subtotal] => 103.42
[tax] => 18.42
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[food_id] => 2
)
[1] => Array
(
[id] => 1
[food_id] => 1
)
[2] => Array
(
[count] => 2
[subtotal] => 303.42
[tax] => 38.42
)
)
)
It should append nested values of $whole_orders array's..
If you think that my title is not correct please change it..
Thanks in advance..
Use foreach and iterate your $array and assigned to $whole_orders
<?php
// if $array is always single dimension array
foreach($array as $array_key=>$array_val)
{
$whole_orders [$array_key][]=$val[0];
}
// or if $array is multi dimension array
foreach($array as $array_key=>$array_val)
{
foreach($array_val as $key=>$val)
{
$whole_orders [$array_key][]=$val;
}
}
?>
Just do this it will achieve you desire output ,but when if the count is same for both array
foreach($array as $arrayKey => $arrayValue){
foreach($arrayValue as $key => $value){
$whole_orders[$arrayKey][] = $value;
}
}
print_r($whole_orders);
You can try below approach..
$arr3 = array();
foreach($arr1 as $key => $value) :
$arr3[$key] = $value;
if(isset($arr2[$key])) :
foreach($arr2[$key] as $k=>$val) :
$arr3[$key][] = $val;
endforeach;
endif;
endforeach;
print_r($arr3);
Related
This is my array
Array
(
[2] => Array
(
[0] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
[1] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:17 AM
[status] => 0
)
)
)
From this I need sub array count i.e., the array having two indexes such as 2 & 1 from this 2 & 1 there are some nested arrays found such as 0 & 1 for each
Here,I need array count as follows
Array
(
[2] => Array
(
[count] = 2
)
[1] => Array
(
[count] = 1
)
)
How should I get this..
Someone help me out of this...
Thank you..
It is very easy foreach your array and use count or sizeof function.
$desiredArray = array();
foreach ($myarray as $key => $value) {
$desiredArray [$key] ['count'] = sizeof ($value);
}
print_r ($desiredArray);
The output will be as your desired output
Array
(
[2] => Array
(
[count] = 2
)
[1] => Array
(
[count] = 1
)
)
It's simple, and is better to create new array where you can save count of elements of main array items:
$counts = array();
foreach ($array as $k => $values) {
$counts[$k] = count($values);
}
print($counts); // gives desired result
Also you don't need to have extra array for the $counts array, what you get is:
array (
2 => 2,
1 => 1
)
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 generating dynamic textbox for save and add more functionality and i have to store that data in database i got the array but i dont know how to put that array in to loop so i can get my data.
Array looks like this based on this prepare loop so i can access every element of array:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a1
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a2
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
)
Thanks
try this
$array = //your array
foreach($array as $value){
foreach($value as $value2){
foreach($value2 as $value3){
foreach($value3 as $key3 => $value3){
//$key3 is rem_type, phase_nameā¦
//$value3 is required values
}
}
}
}
To get to the data you can use something like:
foreach ($array AS $row) {
$prem_type = $row[0][0]['prem_type'];
$phase_name = $row[0][1]['phase_name'];
$counter = $row[0][2]['counter'];
$block1 = $row[0][3]['block'];
$block2 = $row[0][4]['block'];
}
An alternative is to restructure the array into something more tidy:
$result = array();
foreach ($array AS $rowId => $row) {
$result[$rowId] = array(
'prem_type' => $row[0][0]['prem_type'],
'phase_name' => $row[0][1]['phase_name'],
'counter' => $row[0][2]['counter'],
'block1' => $row[0][3]['block'],
'block2' => $row[0][4]['block']
);
}
This results in:
Array
(
[0] => Array
(
[prem_type] => 1,
[phase_name] => a1,
[counter] => 2,
[block1] => A,
[block2] => B
)
...
)
Here is the answer:
for($i=0;$i<count($data1);$i++){
for($j=0;$j<count($data1[$i]);$j++){
$prem_type =$data1[$i][$j][0]['prem_type'];
$prem_name= $data1[$i][$j][1]['phase_name'];
$no_of_phase= $data1[$i][$j][2]['counter'];
echo $prem_type." ".$prem_name." ".$no_of_phase."<br>";
for($k=3;$k<count($data1[$i][$j]);$k++){
echo $data1[$i][$j][$k]['unitname']."<br>";
}
}
}
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;
}