Need some help formatting this array properly:
Here is the dump of the array:
array (size=5)
'Bank Deposit' =>
array (size=13)
'Payment Method' => string 'Bank Deposit' (length=12)
0 => string 'Bank Deposit' (length=12)
201704 => string '1200' (length=4)
201705 => string '0' (length=1)
201701 => string '0' (length=1)
201706 => string '0' (length=1)
201707 => string '0' (length=1)
201612 => string '0' (length=1)
201708 => string '0' (length=1)
201602 => string '0' (length=1)
201709 => string '0' (length=1)
201702 => string '0' (length=1)
201710 => string '0' (length=1)
'Cash' =>
array (size=13)
'Payment Method' => string 'Cash' (length=4)
0 => string 'Cash' (length=4)
201704 => string '300' (length=3)
201705 => string '120' (length=3)
201701 => string '800' (length=3)
201706 => string '800' (length=3)
201707 => string '120' (length=3)
201612 => string '800' (length=3)
201708 => string '800' (length=3)
201602 => string '12' (length=2)
201709 => string '12' (length=2)
201702 => string '0' (length=1)
201710 => string '0' (length=1)
'Cheque' =>
array (size=13)
'Payment Method' => string 'Cheque' (length=6)
0 => string 'Cheque' (length=6)
201704 => string '10' (length=2)
201705 => string '0' (length=1)
201701 => string '590' (length=3)
201706 => string '590' (length=3)
201707 => string '0' (length=1)
201612 => string '0' (length=1)
201708 => string '0' (length=1)
201602 => string '231' (length=3)
201709 => string '231' (length=3)
201702 => string '0' (length=1)
201710 => string '0' (length=1)
'Mobile Money' =>
array (size=13)
'Payment Method' => string 'Mobile Money' (length=12)
0 => string 'Mobile Money' (length=12)
201704 => string '0' (length=1)
201705 => string '0' (length=1)
201701 => string '0' (length=1)
201706 => string '0' (length=1)
201707 => string '0' (length=1)
201612 => string '0' (length=1)
201708 => string '0' (length=1)
201602 => string '0' (length=1)
201709 => string '0' (length=1)
201702 => string '150' (length=3)
201710 => string '150' (length=3)
'' =>
array (size=13)
'Payment Method' => null
0 => null
201704 => string '1510' (length=4)
201705 => string '120' (length=3)
201701 => string '1390' (length=4)
201706 => string '1390' (length=4)
201707 => string '120' (length=3)
201612 => string '800' (length=3)
201708 => string '800' (length=3)
201602 => string '243' (length=3)
201709 => string '243' (length=3)
201702 => string '150' (length=3)
201710 => string '150' (length=3)
QUESTION: How do I get rid of the 'Payment Method' and 0 keys in the array?
WHAT I'VE TRIED:
Table as it is: https://ibb.co/gW55g5
Splicing the value from offset 1: https://ibb.co/fogNTk
Splicing the value and key from offset 1: https://ibb.co/jgU98k
This removes the columns but then reindexes the keys!
<div class='box-body no-padding'>
<table class='table cell-border' id='summary'><thead>
<tr>
<?php
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
} foreach (array_splice($v,1) as $k => $v){
echo '<th>'.$k.'</th>';
}
var_dump($temp);
}
?>
</tr></thead>
<?php
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
foreach (array_splice($v , 1) as $k => $v){
echo '<td>'.$v.'</td>';
}
echo "</tr>";
}
?>
</table>
</div>
</div>
</div>
I made an earlier post but it seemed to not get to the point but if you need any extra help in getting the picture you can see a more detailed question here: Multidimensional Array Dynamic Columns Format. Thanks
<?php
$data = [
'foo' =>
[
'unwanted' => 'something',
0 => 'something else not wanted',
'201704' => '23',
],
'bar' =>
[
'unwanted' => 'something',
0 => 'something else not wanted',
'201703' => '47',
]
];
foreach($data as $k => $v) {
unset($data[$k]['unwanted']);
unset($data[$k][0]);
}
var_export($data);
Output:
array (
'foo' =>
array (
201704 => '23',
),
'bar' =>
array (
201703 => '47',
),
)
I am trying to push all the records from DB into one hash (associative array) on the basis of uid (as key) below.
Sample dump of Records from DB:
array (size=4)
'id' => string '18' (length=2)
'job_title' => string 'Rubyyyy' (length=4)
'job_skills' => string 'Rubyyyy' (length=4)
'uid' => string '1' (length=1)
array (size=4)
'id' => string '19' (length=2)
'job_title' => string 'Ruby on Rails' (length=13)
'job_skills' => string 'Ruby on Rails' (length=13)
'uid' => string '2' (length=1)
array (size=4)
'id' => string '20' (length=2)
'job_title' => string 'Ruby' (length=4)
'job_skills' => string 'Ruby' (length=4)
'uid' => string '1' (length=1)
array (size=4)
'id' => string '21' (length=2)
'job_title' => string 'Rubyon' (length=13)
'job_skills' => string 'Rubyon' (length=13)
'uid' => string '2' (length=1)
Expected Output:
array (size=1)
1 =>
array (size=4)
'id' => string '20' (length=2)
'job_title' => string 'Ruby' (length=4)
'job_skills' => string 'Ruby' (length=4)
'uid' => string '1' (length=1)
array (size=4)
'id' => string '18' (length=2)
'job_title' => string 'Rubyyyy' (length=4)
'job_skills' => string 'Rubyyyy' (length=4)
'uid' => string '1' (length=1)
2 =>
array (size=4)
'id' => string '21' (length=2)
'job_title' => string 'Rubyon' (length=13)
'job_skills' => string 'Rubyon' (length=13)
'uid' => string '2' (length=1)
array (size=4)
'id' => string '19' (length=2)
'job_title' => string 'Ruby on Rails' (length=13)
'job_skills' => string 'Ruby on Rails' (length=13)
'uid' => string '2' (length=1)
I have an array:
array (size=3)
0 =>
array (size=3)
'quantity' => string '20' (length=2)
'date' => string '2016-01-01' (length=10)
'eID' => string '2' (length=1)
1 =>
array (size=3)
'quantity' => string '10' (length=2)
'date' => string '2016-03-01' (length=10)
'eID' => string '2' (length=1)
2 =>
array (size=3)
'quantity' => string '15' (length=2)
'date' => string '2016-01-01' (length=10)
'eID' => string '1' (length=1)
how can i fill missing date for each 'eID'? so the output would be:
array (
0 =>
array (
'quantity' => string '20' (length=2)
'date' => string '2016-01-01' (length=10)
'eID' => string '2' (length=1)
1 =>
array (
'quantity' => null
'date' => string '2016-02-01' (length=10)
'eID' => string '2' (length=1)
2 =>
array (
'quantity' => string '10' (length=2)
'date' => string '2016-03-01' (length=10)
'eID' => string '2' (length=1)
...until 2016-12-01. 12 arrays for each 'eID' key with same value
I have a few records from database and I fetched all records. The array return as the code below
array (size=10)
0 =>
array (size=5)
'id' => string '1' (length=1)
'type' => string 'group' (length=5)
'members' => null
'parents' => string '0' (length=1)
'level' => string '1' (length=1)
1 =>
array (size=5)
'id' => string '2' (length=1)
'type' => string 'group' (length=5)
'members' => null
'parents' => string '0' (length=1)
'level' => string '1' (length=1)
2 =>
array (size=5)
'id' => string '3' (length=1)
'type' => string 'team' (length=4)
'members' => string '["3","5","6"]' (length=13)
'parents' => string '1' (length=1)
'level' => string '2' (length=1)
3 =>
array (size=5)
'id' => string '4' (length=1)
'type' => string 'team' (length=4)
'members' => string '["1"]' (length=5)
'parents' => string '1' (length=1)
'level' => string '2' (length=1)
4 =>
array (size=5)
'id' => string '5' (length=1)
'type' => string 'group' (length=5)
'members' => null
'parents' => string '1' (length=1)
'level' => string '2' (length=1)
5 =>
array (size=5)
'id' => string '8' (length=1)
'type' => string 'team' (length=4)
'members' => string '["4"]' (length=5)
'parents' => string '5' (length=1)
'level' => string '3' (length=1)
6 =>
array (size=5)
'id' => string '9' (length=1)
'type' => string 'team' (length=4)
'members' => string '["2"]' (length=5)
'parents' => string '5' (length=1)
'level' => string '3' (length=1)
7 =>
array (size=5)
'id' => string '10' (length=2)
'type' => string 'team' (length=4)
'members' => null
'parents' => string '5' (length=1)
'level' => string '3' (length=1)
8 =>
array (size=5)
'id' => string '11' (length=2)
'type' => string 'team' (length=4)
'members' => null
'parents' => string '5' (length=1)
'level' => string '3' (length=1)
9 =>
array (size=5)
'id' => string '12' (length=2)
'type' => string 'group' (length=5)
'members' => null
'parents' => string '1' (length=1)
'level' => string '2' (length=1)
the members field has been encoded as json string. I want to build a array with format as the code below
array('1'=>array(1,2,3,4,5,6),
'2'=>array(),
'3'=>array(3,5,6),
'4'=>array(1),
'5'=>array(2,4),
'8'=>array(4),
'9'=>array(2))
The record's id will be key of array and the members has decoded will become the value. I wrote a function handle the array return from the database but the result not as my intention. This is my code
$results = mysql_query('select id,type,members,parents,level from team');
$array = array();
recursiveDate($results,0,$array);
function recursiveData($sourceArr,$parents = 0, &$newMenu){
if(count($sourceArr)>0){
foreach ($sourceArr as $key => $value){
if($value['parents'] == $parents){
if(isset($newMenu[$value['id']])){
$newMenu[$value['id']] = array();
}
$newMenu[$value['id']] = $value['members']?json_decode($value['members']):array();
if(isset($newMenu[$parents])){
$newMenu[$parents] = array_merge($newMenu[$value['id']],$newMenu[$parents]);
}
$newParents = $value['id'];
unset($sourceArr[$key]);
$this->recursiveData($sourceArr,$newParents, $newMenu);
}
}
}
}
And this is the array after handled
array (size=10)
1 =>
array (size=4)
0 => string '1' (length=1)
1 => string '3' (length=1)
2 => string '5' (length=1)
3 => string '6' (length=1)
3 =>
array (size=3)
0 => string '3' (length=1)
1 => string '5' (length=1)
2 => string '6' (length=1)
4 =>
array (size=1)
0 => string '1' (length=1)
5 =>
array (size=2)
0 => string '2' (length=1)
1 => string '4' (length=1)
8 =>
array (size=1)
0 => string '4' (length=1)
9 =>
array (size=1)
0 => string '2' (length=1)
10 =>
array (size=0)
empty
11 =>
array (size=0)
empty
12 =>
array (size=0)
empty
2 =>
array (size=0)
empty
Please help me build that array
I quickly wrote this so not sure if it's exactly what you're looking for, looks like you over complicated it hugely though. I kept the original result structure incase you wanted that but you can easily overwrite it if you wish.
function modifyData($data = array()) {
foreach($data as &$entry) {
if(!empty($entry['members'])) {
$entry['members'] = json_decode($entry['members'], true);
} else {
$entry['members'] = array();
}
}
return $data;
}
I tested this with
$data = array(
array(
'id' =>'1',
'type' =>'group',
'members' => null,
'parents' =>'0',
'level' =>'1'
),
array(
'id' =>'1',
'type' =>'group',
'members' => '["3","5","6"]',
'parents' =>'0',
'level' =>'1'
),
array(
'id' =>'1',
'type' =>'group',
'members' => '["3"]',
'parents' =>'0',
'level' =>'1'
)
);
And the output is
array (size=3)
0 =>
array (size=5)
'id' => string '1' (length=1)
'type' => string 'group' (length=5)
'members' =>
array (size=0)
empty
'parents' => string '0' (length=1)
'level' => string '1' (length=1)
1 =>
array (size=5)
'id' => string '1' (length=1)
'type' => string 'group' (length=5)
'members' =>
array (size=3)
0 => string '3' (length=1)
1 => string '5' (length=1)
2 => string '6' (length=1)
'parents' => string '0' (length=1)
'level' => string '1' (length=1)
2 =>
array (size=5)
'id' => string '1' (length=1)
'type' => string 'group' (length=5)
'members' =>
array (size=1)
0 => string '3' (length=1)
'parents' => string '0' (length=1)
'level' => string '1' (length=1)
For a project i am trying to rebuild a array that the database returned. This data is needed to populate a graph.
array
0 =>
array
'payment_method_stats_id' => string '1' (length=1)
'payment_method_id' => string '92' (length=2)
'country_id' => string '1' (length=1)
'total' => string '2' (length=5)
'timestamp' => string '2014-01-17 13:39:41' (length=19)
'payment_methods_id' => string '92' (length=2)
'definition' => string 'iDeal' (length=5)
'month' => string 'January' (length=7)
1 =>
array
'payment_method_stats_id' => string '49' (length=2)
'payment_method_id' => string '92' (length=2)
'country_id' => string '1' (length=1)
'total' => string '2' (length=5)
'timestamp' => string '2014-02-17 13:39:41' (length=19)
'payment_methods_id' => string '92' (length=2)
'definition' => string 'iDeal' (length=5)
'month' => string 'February' (length=8)
2 =>
array
'payment_method_stats_id' => string '97' (length=2)
'payment_method_id' => string '92' (length=2)
'country_id' => string '1' (length=1)
'total' => string '2' (length=5)
'timestamp' => string '2014-03-17 13:39:41' (length=19)
'payment_methods_id' => string '92' (length=2)
'definition' => string 'iDeal' (length=5)
'month' => string 'March' (length=5)
3 =>
array
'payment_method_stats_id' => string '97' (length=2)
'payment_method_id' => string '92' (length=2)
'country_id' => string '1' (length=1)
'total' => string '2' (length=5)
'timestamp' => string '2014-03-17 13:39:41' (length=19)
'payment_methods_id' => string '92' (length=2)
'definition' => string 'Paypal' (length=5)
'month' => string 'January' (length=5)
4 =>
array
'payment_method_stats_id' => string '97' (length=2)
'payment_method_id' => string '92' (length=2)
'country_id' => string '1' (length=1)
'total' => string '2' (length=5)
'timestamp' => string '2014-03-17 13:39:41' (length=19)
'payment_methods_id' => string '92' (length=2)
'definition' => string 'Visa' (length=5)
'month' => string 'January' (length=5)
The database returns a array with payment methods and with the month. so ideal is returned 12 times only with a different month.
now i need this data for a graph and i want to rebuild this array into this
'Januari'
=>payment
=> values, values, values, values values,
=>payment
=> values, values, values, values, values
how could i accomplish this