I need to make my array better.
I am getting data from database and i have milestones and milestone_parts. i want two-dimensional array. I need data of milestones in the first dimension and milestone_parts in the second dimension.
With this code:
$query = "
SELECT
a.id AS `milestone_id`,
a.titel AS `milestone_titel`,
a.client AS `client`,
a.verkocht_id AS `milestone_verkocht_id`,
b.id AS `milestonefase_id`,
b.titel AS `milestonefase_titel`,
b.milestone_id AS `milestonefase_milestone_id`,
b.omschrijving AS `milestonefase_omschrijving`
FROM `milestones` a
INNER JOIN `milestone_parts` b ON a.id=b.milestone_id
WHERE a.verkocht_id = '99'
";
$result= $db->query($dbh, $query);
while ($row = $db->fetchassoc($result))
{
$stone = array($row['milestone_verkocht_id'], $row['milestone_id'], $row['milestone_titel'], $row['client']);
$fase = array($row['milestonefase_milestone_id'],$row['milestonefase_id'],$row['milestonefase_titel']);
$stone[] = $fase;
echo '<pre>'; print_r($stone); echo '</pre>';
}
I get this as result
Array
(
[0] => 99
[1] => 6
[2] => string
[3] => string
[4] => Array
(
[0] => 6
[1] => 10
[2] => string
)
)
Array
(
[0] => 99
[1] => 6
[2] => string
[3] => string
[4] => Array
(
[0] => 6
[1] => 11
[2] => string
)
)
but I need (with names) this:
Array
(
[milestone_verkocht_id] => 99 // This is project id
[milestone_id] => 6
[milestone_title] => string
[client] => string
[10] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 10
[milestone_title] => string
)
[11] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 11
[milestone_title] => string
)
[12] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 12
[milestone_title] => string
)
)
Can you help me or do you have a solution? Help me please!
you can cycle each field returned by the query, checking the field name and making new arrays
$stones = array();
while ($row = $db->fetchassoc($result)) {
$fase = array();
$stone = array('milestones' => array());
foreach ($row as $k => $v) {
if (strpos($k, 'milestonefase_') === 0) {
$fase[$k] = $v;
} else {
$stone[$k] = $v;
}
}
if(!isset($stones[$stone['milestone_id']])) {
$stones[$stone['milestone_id']] = $stone;
}
$stones[$stone['milestone_id']]['milestones'][$fase['milestonefase_id']] = $fase;
}
echo '<pre>'.print_r($stones, true).'</pre>';
Edit
made some changes in order to match the request. Now we use $stones to store the information we already have on a milestone, adding to it the different "$fase" returned from the query
Probably a more clean way is to retrieve all the information with two different queries one for milestones and the other for the fases
Edit2
Added a sub-array for the milestone fases
Related
Outputting data to an array
public function GetProfitByMonth($d1, $d2){
$sql=odbc_exec($this->connections, "select con.ID, con.ShortName as NameAK,
ROUND(ISNULL(SUM(tik.TotalSub)-SUM(tik.TotalAG), 0), 2) as Profit,
MONTH(tik.DEALDATE) as month_num
from Counteragent as con
left join Tickets as tik on tik.AgentID=con.ID
where con.ValueType in (2,3) and con.Active='1' and (DEALDATE between '".$d1."' and '".$d2."')
group by con.ID, con.ShortName, MONTH(tik.DEALDATE)
order by con.ShortName, MONTH(tik.DEALDATE) ;");
$tblResult=array();
while ($row = odbc_fetch_array($sql)) {
$tblResult[]=$row;
}
odbc_free_result($sql);
Array
(
[0] => Array
(
[Profit] => 11218.30
[month_num] => 8
)
[1] => Array
(
[Profit] => 1152.15
[month_num] => 8
)
....
[4] => Array
(
[Profit] => 119837.81
[month_num] => 8
)
)
I need to display only month_num => Profi from the array
did this way
$tblResult = array();
while ($row = odbc_fetch_array($sql)) {
$tblResult[$row['mon_num']] = $row['Profit'];
}
odbc_free_result($sql);
return $tblResult;
but as a result in the array shows only the last value:
Array
(
[8] => 119837.81
)
How to make an array show all the data?
This is because month_num = 8 for all entries in your original array.
Your while loops first two iterations effectively evaluate to:
$tblResult[8] = 11218.30
$tblResult[8] = 1152.15
To get the result in the form you want, you need month_num to be unique.
I have a question about my array. How can I get only data of milestone in my milestone array?
I have a query which is a prefixed array. I get data of milestones and milestonefases (this is milestone parts).
This is my code:
$stones_fases = array();
while ($row = $db->fetchassoc($result)){
$milestonefase = array();
$milestone = array();
foreach ($row as $mkey => $mvalue){
$milestone[$mkey] = $mvalue;
foreach ($row as $fkey => $fvalue){
$milestonefase[$fkey] = $fvalue;
}
}
if (!isset($stones_fases[$milestone['milestone_id']])){
$stones_fases[$milestone['milestone_id']] = $milestone; //['client']['milestone_verkocht_id']
}
$stones_fases[$milestone['milestone_id']][$milestonefase['milestonefase_id']] = $milestonefase['milestonefase_titel'];
}
I get this:
Array
(
[int] => Array
(
[milestone_id] => int
[milestone_titel] => string
[client] => string
[milestone_verkocht_id] => 99
[milestonefase_id] => 10
[milestonefase_titel] => string
[milestonefase_milestone_id] => 6
[10] => string
[11] => string
)
)
But I want this:
Array
(
[int] => Array
(
[milestone_titel] => string
[client] => string
[milestone_verkocht_id] => int
[10] => string
[11] => string
)
)
My query is this:
$project = $_COOKIE['project'];
$query = " SELECT
a.id AS `milestone_id`,
a.titel AS `milestone_titel`,
a.client AS `client`,
a.verkocht_id AS `milestone_verkocht_id`,
b.id AS `milestonefase_id`,
b.titel AS `milestonefase_titel`,
b.milestone_id AS `milestonefase_milestone_id`
FROM `milestones` a
INNER JOIN `milestone_parts` b ON a.id=b.milestone_id
WHERE a.verkocht_id = '{$project}' ";
$result= $db->query($dbh, $query);
I solved my own problem!
If i do this:
while ($row = $db->fetchassoc($result)){
$stones_fases[$row['milestone_id']]['milestone_titel'] = $row['milestone_titel'];
$stones_fases[$row['milestone_id']]['milestone_client'] = $row['client'];
$stones_fases[$row['milestone_id']]['milestonesfases'][$row['milestonefase_id']] = $row['milestonefase_titel'];
}
I get what i expected:
Array
(
[2] => Array
(
[milestone_titel] => Beheer opleveren
[milestone_client] => stackoverflow
[milestonesfases] => Array
(
[1] => Menu bouwen
[2] => Pagina beheer CMS
[3] => Projecten CMS
[4] => Portfolio
[5] => Footer inbouwen
)
)
)
Look at that bunch of code what I wrote a day ago.
Do you see that nonsens of code what I wrote?
Take a look at my code which I wrote 1 minute ago!
I was struggling this for 3 days and now I solved my own problem!
I have an array coming from my database and simply it consists of questions and answers. I am trying to merge 2 arrays and create multidimensional array if values are more than one.
Array
(
[0] => Array
(
[question_id] => 1
[option_id] => 1
)
[1] => Array
(
[question_id] => 2
[option_id] => 3
)
[2] => Array
(
[question_id] => 3
[option_id] => 5
)
[3] => Array
(
[question_id] => 3
[option_id] => 6
)
)
I've tried to separate answers and questions to 2 different arrays but couldn't figure how to merge them again.
$user_questions = array_column($answers, 'question_id');
$user_answers = array_column($answers, 'option_id');
What I need is (question 3 has 2 answers) :
Array
(
[1] => 1
[2] => 3
[3] => Array (5, 6)
)
You can group your data like this as you fetch the results from your query instead of processing it after the fact. To get the array you have now, you're currently doing something like this:
while ($row = $stmt->someFetchMethod()) {
$result[] = $row;
}
Instead, use the question id as the key in your result array, and append the option id to an array at that key.
while ($row = $stmt->someFetchMethod()) {
$result[$row['question_id']][] = $row['option_id'];
}
Below code will create a new array by looping the existing array.
// Considering your existing array to be like this
$array = array(
'0' => array('question_id' => 1,'option_id' => 1),
'1' => array('question_id' => 2, 'option_id' => 3 ),
'2' => array('question_id' => 3,'option_id' => 5),
'3' => array('question_id' => 3,'option_id' => 6)
);
//define new array
$new_array = array();
// loop the array
foreach($array as $key=>$value){
// if the option/answer is already set to to question key
if(isset($new_array[$value['question_id']])){
// if question key is an array, push new option to that array
if(is_array($new_array[$value['question_id']])){
array_push($new_array[$value['question_id']], $value['option_id']);
}else{
// convert question key to array with the old value and new option value
$new_array[$value['question_id']] = array($new_array[$value['question_id']],$value['option_id']);
}
}
else{
// assing option as value to question key
$new_array[$value['question_id']] = $value['option_id'];
}
}
print_r($new_array);
Out put:
Array
(
[1] => 1
[2] => 3
[3] => Array
(
[0] => 5
[1] => 6
)
)
I have the following array data:
[0] => stdClass Object
(
[schoolBin] => 110140014570
[schoolName] => школа-лицей № 66
[users] => 30
[tb0306_tb0301_id] => 514725
[tb0306_tb3002_id] => 17
[tb0306_countOfCorrectAnswers] => 14
[point] => 4
)
[1] => stdClass Object
(
[schoolBin] => 110140014570
[schoolName] => школа-лицей № 66
[users] => 30
[tb0306_tb0301_id] => 514725
[tb0306_tb3002_id] => 18
[tb0306_countOfCorrectAnswers] => 11
[point] => 4
)
So, i have many tb0306_tb0301_id from one schoolBin, and tb0306_tb0301_id has many tb0306_countOfCorrectAnswers of tb0306_tb3002_id. So i need to sum all tb0306_countOfCorrectAnswers of for all tb0306_tb0301_id of one schoolBin and i have many schoolBin, so i need to do the process for all schoolBin.
Tried the code:
$results = array();
foreach ($schoolResults as $schoolResult) {
$schoolBin = $schoolResult->schoolBin;
if (isset($results[$schoolBin])) {
$results[$schoolBin][] = $schoolResult;
} else {
$results[$schoolBin] = array($schoolResult);
}
}
But could not sum tb0306_countOfCorrectAnswers for one tb0306_tb0301_id.
Any helps guys!
here is the code to sum the tb0306_countOfCorrectAnswers for tb0306_tb0301_id of schoolBin. I use # to ingnore the worning for uninitial value.
$results = array();
foreach ($schoolResults as $schoolResult) {
#$result[$schoolResult->schoolBin][$schoolResult->tb0306_tb0301_id] += $schoolResult->tb0306_countOfCorrectAnswers
}
i have table (tag)..
i want to select all tags in this table and return it as single array where tag_id = index
and tag_name = value
like this array
array(
3 => 'Hazel Grouse',
4 => 'Common Quail',
5 => 'Common Pheasant',
6 => 'Northern Shoveler',
7 => 'Greylag Goose',
8 => 'Barnacle Goose',
9 => 'Lesser Spotted Woodpecker',
10 => 'Eurasian Pygmy-Owl',
11 => 'Dunlin',
13 => 'Black Scoter',
)
tags return like this multi D array
Array
(
[0] => Array
(
[tag_id] => 3
[tag_name] => Hazel Grouse
)
[1] => Array
(
[tag_id] => 4
[tag_name] => Common Quail
)
this is query code
function get_alltags() {
$this->db->select('tag_id');
$this->db->select('tag_name');
$result = $this->db->get('d_tag');
return $result->result_array();
}
$alltags = $this->tag->get_alltags();
echo "<pre>";
print_r($alltags);
echo "</pre>";
exit;
you can do something like this
$result=array();
$alltags = $this->tag->get_alltags();
foreach($alltags as $k => $v){
$result[$v['tag_id']]=$v['tag_name'];}
echo "<pre>";
print_r($result);