I have problem with building multi-dimensional array by pulling data from database.
I have a list of Sales Representative (sr) in my database. I run query and selected all sr.
Now I have to create array like this:
array (size=3)
'Manoj' =>
array (size=3)
'mc_count' => 0
'auto_count' => 0
'in_count' => 0
'Bharat' =>
array (size=3)
'mc_count' => 0
'auto_count' => 0
'in_count' => 0
'Pradeep' =>
array (size=3)
'mc_count' => 0
'auto_count' => 0
'in_count' => 0
To create this I have written following code:
<?php
$sr_array=array();
$sr_sql= "select DISTINCT sr from sales_invoice";
$sr_query = mysqli_query($connection, $sr_sql);
while($sr_result = mysqli_fetch_assoc($sr_query)){
$sr_array []= array($sr_result["sr"]=>array(“mc_count”,”auto_count”,”in_coun”);
}
var_dump($sr_array);
?>
I get this OUTPUT
array (size=9)
0 =>
array (size=1)
'Manoj' =>
array (size=3)
'count_in_battery' => int 10
'count_auto_battety' => int 0
'count_indu_battery' => int 0
1 =>
array (size=1)
'Bharat' =>
array (size=3)
'count_in_battery' => int 10
'count_auto_battety' => int 0
'count_indu_battery' => int 0
2 =>
array (size=1)
'Pradeep =>
array (size=3)
'count_in_battery' => int 10
'count_auto_battety' => int 0
'count_indu_battery' => int 0
If you see the output my array has additional index as
0 => //Unwanted Index
array (size=1)
'Manoj' =>
Which is creating problem in programming. If you can please help me.
Try this:
$sr_array = array();
$sr_sql = "select DISTINCT sr from sales_invoice";
$sr_query = mysqli_query($connection, $sr_sql);
while($sr_result = mysqli_fetch_assoc($sr_query)){
$sr_array[$sr_result["sr"]] = array(
"mc_count" => 0,
"auto_count" => 0,
"in_coun" => 0
);
}
Related
How to construct data into a binary tree sort to output a one-dimensional array?
Now that I have constructed the data into a binary tree, how can I recursively solve the binary tree as a one-dimensional array with the following code and data:
Data
$nodes = array(8,3,10,1,6,14,4,7,13);
Construct a binary tree code
function insertNode($node,$newNode){
//var_dump($node);
//var_dump($newNode);
//exit;
if ($node['key'] < $newNode['key']){
if (empty($node['right'])){
$node['right'] = $newNode;
}else{
$node['right'] = insertNode($node['right'],$newNode);
}
}elseif ($node['key'] > $newNode['key']){
if (empty($node['left'])){
$node['left'] = $newNode;
}else{
$node['left'] = insertNode($node['left'],$newNode);
}
}
return $node;
}
function tree($nodes)
{
$node = [
'key' => '',
'left' => '',
'right' => ''
];
$newNode = [
'key' => '',
'left' => '',
'right'=> ''
];
foreach ($nodes as $key => $value){
//insert($value,$key);
if($key == 0)
{
$node['key'] = $value;
continue;
}
$newNode['key'] = $value;
//Constructing a binary tree
$node = insertNode($node,$newNode);
}
//Recursive solution
$node = midSortNode($node);
return $node;
}
var_dump(tree($nodes));
The following is my constructed binary tree
array (size=3)
'key' => int 8
'left' =>
array (size=3)
'key' => int 3
'left' =>
array (size=3)
'key' => int 1
'left' => string '' (length=0)
'right' => string '' (length=0)
'right' =>
array (size=3)
'key' => int 6
'left' =>
array (size=3)
...
'right' =>
array (size=3)
...
'right' =>
array (size=3)
'key' => int 10
'left' => string '' (length=0)
'right' =>
array (size=3)
'key' => int 14
'left' =>
array (size=3)
...
'right' => string '' (length=0)
I need to recursively classify the binary tree into a well-ordered one-dimensional array.
My code is as follows
function midSortNode($node){
$sortArr = [];
if (!empty($node)){
$sortArr[] = midSortNode($node['left']);
//$sortArr['left'] = midSortNode($node['left']);
array_push($sortArr,$node['key']);
$sortArr[] = midSortNode($node['right']);
//$sortArr['right'] = midSortNode($node['right']);
}
return $sortArr;
}
var_dump(midSortNode($node));
Here is the result, but not what I want
0 =>
array (size=3)
0 =>
array (size=3)
0 =>
array (size=0)
...
1 => int 1
2 =>
array (size=0)
...
1 => int 3
2 =>
array (size=3)
0 =>
array (size=3)
...
1 => int 6
2 =>
array (size=3)
...
1 => int 8
2 =>
array (size=3)
0 =>
array (size=0)
empty
1 => int 10
2 =>
array (size=3)
0 =>
array (size=3)
...
1 => int 14
2 =>
array (size=0)
...
How to solve the binary tree as follows
array (size=9)
0 => int 1
1 => int 3
2 => int 4
3 => int 6
4 => int 7
5 => int 8
6 => int 10
7 => int 13
8 => int 14
I'm assuming that your happy with the steps so far, so the main code as it is isn't changed. All I think you need to do is to extract the data from the final tree into a 1 dimensional array. As the items are all leaf nodes and in order, you can just use array_walk_recursive() to go over all of the nodes and add them to a new array...
$tree = tree($nodes);
array_walk_recursive( $tree,
function ($data) use (&$output) { $output[] = $data;} );
print_r($output);
gives...
Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 6
[4] => 7
[5] => 8
[6] => 10
[7] => 13
[8] => 14
)
Edit:
To update the existing code to do this, you can change the midSortNode() to pass around the list of outputs and only add in the current node...
function midSortNode($node, $sortArr = []){
if (!empty($node)){
$sortArr = midSortNode($node['left'], $sortArr);
$sortArr[] = $node['key'];
$sortArr = midSortNode($node['right'], $sortArr);
}
return $sortArr;
}
I've been trying to use PHP Variables to get results from a mongoDB database.
This works:
//create Mongo connection
$m = new MongoClient();
//Select db
$db = $m->warehouse;
//create collection
$fact = $db->fact;
$res = $fact->aggregate([
array(
'$match'=>array(
'day'=>array('$gte'=>3,'$lte'=>28),
'month'=>array('$gte'=>3,'$lte'=>3),
'year'=>array('$gte'=>2017,'$lte'=>2017)
)
),
array(
'$group'=>array(
'_id'=>array(
'month'=>'$month',
'day'=>'$day',
'year'=>'$year'
),
'totalSales'=>array('$sum'=>'$sale'),
'totalCost'=>array('$sum'=>'$total_cost')
)
),
array(
'$project'=>array(
'_id'=>0,
'totalSales'=>1,
'totalCost'=>1,
'totalProfit'=>array(
'$subtract'=>['$totalSales','$totalCost']
)
)
)
]);
Output of var_dump($res):
array (size=3)
'waitedMS' => int 0
'result' =>
array (size=1)
0 =>
array (size=3)
'totalSales' => float 440
'totalCost' => float 350
'totalProfit' => float 90
'ok' => float 1
But When I try to replace the numbers with php variables like:
$res = $fact->aggregate([
array(
'$match'=>array(
'day'=>array('$gte'=>$dayfrom,'$lte'=>$dayto),
'month'=>array('$gte'=>$monthfrom,'$lte'=>$monthto),
'year'=>array('$gte'=>$yearfrom,'$lte'=>$yearto)
)
),
array(
'$group'=>array(
'_id'=>array(
'month'=>'$month',
'day'=>'$day',
'year'=>'$year'
),
'totalSales'=>array('$sum'=>'$sale'),
'totalCost'=>array('$sum'=>'$total_cost')
)
),
array(
'$project'=>array(
'_id'=>0,
'totalSales'=>1,
'totalCost'=>1,
'totalProfit'=>array(
'$subtract'=>['$totalSales','$totalCost']
)
)
)
]);
I don't get back any results:
array (size=3)
'waitedMS' => int 0
'result' =>
array (size=0)
empty
'ok' => float 1
What am I doing wrong here? I have tried several things like putting the PHP variables under single quotes or trying to put parts of the query into strings and using those strings to re-build the query but nothing seems to work excep for the hard-coded numbers into the aggregation. Any idea what's going on here?
EDIT: (This did the trick)
Tried initializing the variables in a PHP class constructor that explicitly specifies that the value is coming from a PHP variable:
class DateSet {
public $df;
public $dt;
public $mf;
public $mt;
public $yf;
public $yt;
function __construct( $df, $dt, $mf, $mt, $yf, $yt) {
$this->df = $df;
$this->dt = $dt;
$this->mf = $mf;
$this->mt = $mt;
$this->yf = $yf;
$this->yt = $yt;
}
};
$dateobj = new DateSet($dayfrom,$dayto,$monthfrom,$monthto,$yearfrom,$yearto);
$res = $fact->aggregate([
array(
'$match'=>array(
'day'=>array('$gte'=>$dateobj->df,'$lte'=>$dateobj->dt),
'month'=>array('$gte'=>$dateobj->mf,'$lte'=>$dateobj->mt),
'year'=>array('$gte'=>$dateobj->yf,'$lte'=>$dateobj->yt)
)
),
array(
'$group'=>array(
'_id'=>array(
'month'=>'$month',
'day'=>'$day',
'year'=>'$year'
),
'totalSales'=>array('$sum'=>'$sale'),
'totalCost'=>array('$sum'=>'$total_cost')
)
),
array(
'$project'=>array(
'_id'=>0,
'totalSales'=>1,
'totalCost'=>1,
'totalProfit'=>array(
'$subtract'=>['$totalSales','$totalCost']
)
)
)
]);
OUTPUT:
array (size=3)
'waitedMS' => int 0
'result' =>
array (size=7)
0 =>
array (size=3)
'totalSales' => float 640
'totalCost' => float 520
'totalProfit' => float 120
1 =>
array (size=3)
'totalSales' => float 8000
'totalCost' => float 6000
'totalProfit' => float 2000
2 =>
array (size=3)
'totalSales' => float 700
'totalCost' => float 600
'totalProfit' => float 100
3 =>
array (size=3)
'totalSales' => float 18000
'totalCost' => float 15000
'totalProfit' => float 3000
4 =>
array (size=3)
'totalSales' => float 6300
'totalCost' => float 6100
'totalProfit' => float 200
5 =>
array (size=3)
'totalSales' => float 68503
'totalCost' => float 55720
'totalProfit' => float 12783
6 =>
array (size=3)
'totalSales' => float 440
'totalCost' => float 350
'totalProfit' => float 90
'ok' => float 1
I would like to add values from a $secondArray to $firstArray:
$firstArray = [
0 => [
'prodID' => 101,
'enabled' => 1,
],
1 => [
'prodID' => 105,
'enabled' => 0,
],
];
The $secondArray will always have the same amount of array items and will be in the same order as the $firstArray:
$secondArray = [34, 99];
This is what I have tried but I keep getting the wrong stockQT values after the exercise:
foreach ($secondArray as $value) {
foreach ($firstArray as &$firstArray) {
$firstArray['stockQT'] = $value;
}
}
Incorrect Result for a var_dump($firstArray):
array (size=2)
0 =>
array (size=3)
'prodID' => int 101
'subscribed' => int 1
'stockQT' => int 99
1 =>
array (size=3)
'prodID' => int 105
'subscribed' => int 0
'stockQT' => int 99
I have looked at similar posts but cant seem to get the correct vales after using different suggestions like while() loops. Below is what I need the $firstArray to look like:
array (size=2)
0 =>
array (size=3)
'prodID' => int 101
'subscribed' => int 1
'stockQT' => int 34
1 =>
array (size=3)
'prodID' => int 105
'subscribed' => int 0
'stockQT' => int 99
You just need one foreach() using the key since $secondArray will always have the same amount of array items and will be in the same order as the $firstArray. Notice the & to modify the actual value in the array:
foreach($firstArray as $key => &$value) {
$value['stockQT'] = $secondArray[$key];
}
Or alternately loop $secondArray and use the key to modify $firstArray:
foreach($secondArray as $key => $value) {
$firstArray[$key]['stockQT'] = $value;
}
I have two arrays.
$array1 = ['id_e' =>[91707, 91708]];
$array2 = ['id_s' => [18, 57]];
If I want to insert or delete into the database, I want to make one-to-many mappings on these two arrays. And the result I expect it to be a new array as shown below.
Final Array:
array (size=4)
0 =>
array (size=2)
'id_e' => int 91707
'id_s' => int 18
1 =>
array (size=2)
'id_e' => int 91707
'id_s' => int 57
array (size=2)
2 =>
array (size=2)
'id_e' => int 91708
'id_s' => int 18
3 =>
array (size=2)
'id_e' => int 91708
'id_s' => int 57
I'm stuck after returning array1 and array2. I'm a beginner in php.
How do I do this?
Easiest way is:
$res = array();
foreach ($array1['id_e'] as $ide)
foreach ($array2['id_s'] as $ids)
$res[] = array('id_e' => $ide, 'id_s' => $ids);
I dont know what to ask so i straight away went to show an example. Hope it helps though!
Say i have an array
array (size=3)
0 =>
array (size=3)
0 => int 1
1 => int 2
2 => int 3
2 =>
array (size=3)
0 => int 2
1 => int 3
2 => int 4
5 =>
array (size=3)
0 => int 5
1 => int 6
2 => int 7
Now i want to arrange it according to KEY so that it looks like
array (size=3)
0 =>
array (size=3)
0 => int 1
1 => int 2
2 => int 3
1 =>
array (size=3)
0 => int 2
1 => int 3
2 => int 4
2 =>
array (size=3)
0 => int 5
1 => int 6
2 => int 7
Does anyone has a solution ?
One possible approach:
$new_arr = array_values($old_arr);
You can use foreach and assign the values to new array
$newArr = array();
foreach($array as $k=>$v){
$newArr[] = $v;
}
The most direct way to achieve that is probably to run array_values on your array :
$array = array_values($array);
It will basically reset the keys