I am trying to create a table from array retrieved from database.
array is multidimensional and dynamic.
my sample array is
Array
(
[0] => Array
(
[licensee] => AmazonUK
[totalCounts] => 2
[netRoyalty] => 0.01006506
[month] => 2017-05-01
)
[1] => Array
(
[licensee] => AppleMusicGB
[totalCounts] => 52
[netRoyalty] => 43.39295886
[month] => 2017-04-01
)
[2] => Array
(
[licensee] => AppleMusicHK
[totalCounts] => 2
[netRoyalty] => 6.71799264
[month] => 2017-05-01
)
[3] => Array
(
[licensee] => AppleMusicIN
[totalCounts] => 22
[netRoyalty] => 1.29255252
[month] => 2017-03-01
)
[4] => Array
(
[licensee] => AppleMusicUS
[totalCounts] => 192
[netRoyalty] => 328.95685302
[month] => 2017-03-01
)
[5] => Array
(
[licensee] => Deezer
[totalCounts] => 18
[netRoyalty] => 2.91837036
[month] => 2017-01-01
)
[6] => Array
(
[licensee] => GoogleLocker
[totalCounts] => 10
[netRoyalty] => 0.08685774
[month] => 2017-01-01
)
[7] => Array
(
[licensee] => GoogleSubscription
[totalCounts] => 30
[netRoyalty] => 57.13748172
[month] => 2017-03-01
)
[8] => Array
(
[licensee] => Spotify
[totalCounts] => 356
[netRoyalty] => 179.53991898
[month] => 2017-03-01
)
[9] => Array
(
[licensee] => iTunesAU
[totalCounts] => 14
[netRoyalty] => 3.14949396
[month] => 2017-02-01
)
[10] => Array
(
[licensee] => iTunesCA
[totalCounts] => 16
[netRoyalty] => 0.18564444
[month] => 2017-01-01
)
[11] => Array
(
[licensee] => iTunesEU
[totalCounts] => 4
[netRoyalty] => 1.00737582
[month] => 2017-02-01
)
[12] => Array
(
[licensee] => iTunesGB
[totalCounts] => 8
[netRoyalty] => 0.91430508
[month] => 2017-03-01
)
[13] => Array
(
[licensee] => iTunesUS
[totalCounts] => 2
[netRoyalty] => 99.69851988
[month] => 2017-06-01
)
)
I want to create a table like :
licensee as horizontal heading
month as vertical heading
totalCounts and netRoyalty as totalCounts/netRoyalty under their respective licensee
jsfiddle of sample table is included.
jsfiddle
EDIT :
what i tried is
<table id="table1" class="table table-striped table-hover table-fw-widget">
<thead>
<tr>
<?php
foreach($distributorReportAnalysis as $distreportkey => $distreportval){
?>
<th><?php echo $distreportval[licensee]; ?></th>
<?php
}
?>
</tr>
</thead>
<tbody>
<?php
foreach($distributorReportAnalysis as $distreportk => $distreportv){
?>
<tr class="odd gradeX">
<th><?php echo date('M Y',strtotime($distreportv[month])); ?></th>
<?php
foreach($distreportv as $reportk => $reportv){
?>
<td><?php echo $distreportv['totalCounts']."/".round($distreportv['netRoyalty']); ?></td>
<?php
}
?>
</tr>
<?php
}
?>
</tbody>
</table>
this is a quick example of how you could extract values with array_map. Actually a foreach works too, array_map is particularly useful if you want to modify the original array.
So the idea here is to organize the data before display, here getting the headers and value by month separately. For your display, you'll still have to place the good value to the good position in the array, but it is sorted by headers value now, so that will be easy.
<?php
$arr = [
0 => [
'licensee' => 'AmazonUK',
'totalCounts' => '2',
'netRoyalty' => '0.01006506',
'month' => '2017-05-01',
],
1 => [
'licensee' => 'AppleMusicGB',
'totalCounts' => '52',
'netRoyalty' => '43.39295886',
'month' => '2017-04-01',
],
];
$headers = [];
$rows = [];
function customExtract($element){
global $headers, $rows;
//an array for your headers
if(!in_array($element['licensee'], $headers)){
$headers[] = $element['licensee'];
}
//another for the rows by month
if(!isset($rows[$element['month']])){
$rows[$element['month']] = [];
}
$rows[$element['month']][$element['licensee']] = $element['totalCounts'] . '/' . $element['netRoyalty'];
//you can also transform the original array element or not (if not you just return it)
return $element;
}
$arr = array_map('customExtract', $arr);
var_dump($headers);
echo'<br />';
var_dump($rows);
?>
the result:
array(2) { [0]=> string(8) "AmazonUK" [1]=> string(12) "AppleMusicGB" }
array(2) { ["2017-05-01"]=> array(1) { ["AmazonUK"]=> string(12) "2/0.01006506" } ["2017-04-01"]=> array(1) { ["AppleMusicGB"]=> string(14) "52/43.39295886" } }
Related
I'm trying to display data that shows the count for each month for locations in Jan-December.
I wrote the query to get the necessary data from the server but when I display it in the view in my table the layout for the data, doesn't look the way I want it to look like in my table. I believe I have to reconstruct the array that I wrote in the query for the right format first and then I can display it in my table?
Right now, the data that comes back from the server looks like so
<table id="registeredTable" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Locations</th>
<th>January</th>
<th>Feburary</th>
<th>March</th>
<th>April</th>
<th>May</th>
<th>June</th>
<th>July</th>
<th>August</th>
<th>September</th>
<th>October</th>
<th>November</th>
<th>December</th>
</tr>
</thead>
<tbody>
<?php
foreach($selectAllmonthlylocations as $item) {
echo '
<tr>
<td>'.$item["locations"].'</td>
<td>'.$item["Total"].'</td>
</tr>
';
}
?>
</tbody>
</table>
So my question is how can I format my array properly to display it in my table how I want?
Array
(
[0] => Array
(
[usergroup] => Austin_Domestic
[DateAdded] => 2018-01-03
[Total] => 15
)
[1] => Array
(
[usergroup] => Austin_International
[DateAdded] => 2018-01-25
[Total] => 2
)
[2] => Array
(
[usergroup] => BayArea_Domestic
[DateAdded] => 2018-01-16
[Total] => 192
)
[3] => Array
(
[usergroup] => BayArea_International
[DateAdded] => 2018-01-05
[Total] => 28
)
[4] => Array
(
[usergroup] => Bengaluru_Domestic
[DateAdded] => 2018-01-10
[Total] => 2
)
[5] => Array
(
[usergroup] => Bengaluru_International
[DateAdded] => 2018-01-05
[Total] => 1
)
[6] => Array
(
[usergroup] => Cork
[DateAdded] => 2018-01-02
[Total] => 31
)
[7] => Array
(
[usergroup] => CulverCity
[DateAdded] => 2018-01-10
[Total] => 3
)
[8] => Array
(
[usergroup] => Denver
[DateAdded] => 2018-01-10
[Total] => 1
)
[9] => Array
(
[usergroup] => Hyderabad
[DateAdded] => 2018-01-05
[Total] => 3
)
[10] => Array
(
[usergroup] => London
[DateAdded] => 2018-01-02
[Total] => 10
)
[11] => Array
(
[usergroup] => Macau
[DateAdded] => 2018-01-17
[Total] => 1
)
[12] => Array
(
[usergroup] => Munich
[DateAdded] => 2018-01-02
[Total] => 6
)
[13] => Array
(
[usergroup] => Sacramento_Domestic
[DateAdded] => 2018-01-04
[Total] => 1
)
[14] => Array
(
[usergroup] => Shanghai
[DateAdded] => 2018-01-12
[Total] => 2
)
[15] => Array
(
[usergroup] => Singapore
[DateAdded] => 2018-01-03
[Total] => 8
)
[16] => Array
(
[usergroup] => Sydney
[DateAdded] => 2018-01-21
[Total] => 1
)
[17] => Array
(
[usergroup] => Tokyo
[DateAdded] => 2018-01-04
[Total] => 3
)
[18] => Array
(
[usergroup] => Austin_Domestic
[DateAdded] => 2018-02-01
[Total] => 31
)
[19] => Array
(
[usergroup] => Austin_International
[DateAdded] => 2018-02-19
[Total] => 2
)
[20] => Array
(
[usergroup] => Bangkok
[DateAdded] => 2018-02-07
[Total] => 1
)
[21] => Array
(
[usergroup] => BayArea_Domestic
[DateAdded] => 2018-02-08
[Total] => 165
)
[22] => Array
(
[usergroup] => BayArea_International
[DateAdded] => 2018-02-12
[Total] => 29
)
Here is my code:
$selectallmonthlysql = 'SELECT locations, DateAdded, COUNT(*) as Total
FROM testserverdataSignup
WHERE DateAdded > "2017-12-31"
GROUP BY month(DateAdded), locations';
$selectAllmonthlystmt = $dbo->prepare($selectallmonthlysql);
$selectAllmonthlystmt->execute();
$selectAllmonthlylocations = $selectAllmonthlystmt->fetchAll(PDO::FETCH_ASSOC);
Screenshot of what the table currently looks like: I want the individual count for each location to correlate to the month and go across instead of down
First change your query
$selectallmonthlysql = '
SELECT
locations,
MONTHNAME(DateAdded) AS month,
COUNT(*) AS total
FROM
testserverdataSignup
WHERE
DateAdded > "2017-12-31"
GROUP BY month(DateAdded),locations';
I lowercased Total to total, its something that was irritating me.
With PDO then you can do
$results = $selectAllmonthlystmt->fetchAll(PDO::FETCH_GROUP);
foreach($results AS $location => $row ){
FETCH_GROUP is poorly documented but this will organize your data with the first column as the Key of the nested array, in this case the location, which is exactly what we want. One of the reasons I use PDO instead of that other DB library I won't mention by name but it starts with MySql and ends with i.
MONTHNAME (click for documentation) will return the full name of the month like "February" which although is not necessary (just a number would do) its much easier to read when outputting the array for debugging.
You'll wind up with something like this
//canned example data, notice one month is out of order and the big gap
// between February to October, this is to show it properly orders and
//fills those in. Always best to test this situations.
$result = array(
"Austin_Domestic" => array(
0 => Array(
"month" => "February",
"total" => 5
),
1 => Array(
"month" => "January",
"total" => 15
),
2 => Array(
"month" => "October",
"total" => 8
),
),
);
//$results = $selectAllmonthlystmt->fetchAll(PDO::FETCH_GROUP);
//then putting it all together
//we'll need the months in an array anyway so we can saves some space with them in the HTML too.
$months = array(
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
)
?>
<table id="registeredTable" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Locations</th>
<?php foreach($months as $month){
//Output the names of the months
echo "<th>{$month}</th>\n";
} ?>
</tr>
</thead>
<tbody>
<?php
foreach( $result as $location => $rows ){
echo "<tr>\n";
echo "<td>{$location}</td>\n";
//simplifies lookup by month (indexes are correlated)
//it returns column 'month' from $rows as a flat array.
//we can save ourselves an external loop by doing this "trick"
$m = array_column($rows, 'month');
//for example, in the case: [0=>February,1=>January,2=>October]
//because this is created from $rows, with a natural number index
//we can simply do array_search on $m to get the index we need in $rows (see below)
foreach($months as $month){
$index = array_search($month, $m);
if(false === $index){
echo "<td>0</td>\n";
}else{
echo "<td>{$rows[$index]['total']}</td>\n";
}
}
echo "</tr>\n";
}
?>
</tbody>
</table>
Output (I did format it to look a bit nicer):
<table id="registeredTable" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Locations</th>
<th>January</th>
<th>February</th>
<th>March</th>
<th>April</th>
<th>May</th>
<th>June</th>
<th>July</th>
<th>August</th>
<th>September</th>
<th>October</th>
<th>November</th>
<th>December</th>
</tr>
</thead>
<tbody>
<tr>
<td>Austin_Domestic</td>
<td>15</td>
<td>5</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>8</td>
<td>0</td>
<td>0</td>
</tr>
</tbody>
</table>
Sandbox
One note and benefit of using an array for the months is you spelled Feburary wrong it should be February something I've been guilty of many many times.
The only real downside to this, is the month name has to match the month name returned from the DB, which shouldn't be an issue if they are spelled correctly (which is how I found the mistake). Otherwise this can applied to the month as a number just as easily, same idea.
Cheers!
First, you should change the query to return MONTH(DateAdded) instead of the whole date, since that's how your output table is organized:
$selectallmonthlysql = 'SELECT locations, MONTH(DateAdded) AS month, COUNT(*) as Total
FROM testserverdataSignup
WHERE DateAdded > "2017-12-31"
GROUP BY month, locations';
Then you should reorganize the array into nested arrays:
array(location1 => array(month1 => total1, month2 => total2, ...),
location2 => array(month1 => total1, month2 => total2, ...),
...)
This code will do that:
$groupedData = array();
while ($selectAllmonthlystmt->fetch(PDO::FETCH_ASSOC) {
$groupedData[$row['locations']][$row['month']] = $row['Total'];
}
Then you can use nested loops when creating the table. The outer loop creates the rows, the inner loop is for the cells in the row.
foreach ($groupedData as $location => $locationData) {
echo "<tr><td>$location</td>";
for ($month = 1; $month <= 12; $month++) {
echo "<td>";
echo isset($locationData[$month]) ? $locaitonData[$month] : 0;
echo "</td>";
}
echo "</tr>";
}
I've been using codeigniter for years but there's a really big gap in between so i always find myself in situations where i forgot how to do things and its almost midnight here so my brain isn't working fast. Can someone show me how to echo the array i have and explain to me how they are processed in the foreach loops?
I have this code in my model to take the rows in 2 tables.
public function tag_genre(){
$result['tag'] = $this->db->get('tags')->result_array();
$result['genre'] = $this->db->get('genre')->result_array();
return $result;
}
And I have this in my controller
public function view_publish_story(){
$data = array('tag_genre' => $this->story_model->tag_genre(), 'title' => "New Story");
$this->load->view('template/header',$data);
$this->load->view('template/navbar');
$this->load->view('pages/storypublish',$data);
$this->load->view('template/footer');
}
I used print_r in my view and this is the result. Seeing this just confuses me more. It's been atleast 2 years since i even dealt with foreach loops.
Array ( [tag] => Array ( [0] => Array ( [tag_id] => 1 [tag_name] =>
LitRPG ) [1] => Array ( [tag_id] => 2 [tag_name] => Virtual Reality )
[2] => Array ( [tag_id] => 3 [tag_name] => Cyberpunk ) [3] => Array (
[tag_id] => 4 [tag_name] => Reincarnation ) [4] => Array ( [tag_id] =>
5 [tag_name] => Summoned Hero ) [5] => Array ( [tag_id] => 6
[tag_name] => Martial Arts ) [6] => Array ( [tag_id] => 7 [tag_name]
=> Slice of Life ) [7] => Array ( [tag_id] => 8 [tag_name] => Overpowered ) [8] => Array ( [tag_id] => 9 [tag_name] => Non-Human )
[9] => Array ( [tag_id] => 10 [tag_name] => Anti-hero ) ) [genre] =>
Array ( [0] => Array ( [genre_id] => 1 [genre_name] => action ) [1] =>
Array ( [genre_id] => 2 [genre_name] => adventure ) [2] => Array (
[genre_id] => 3 [genre_name] => comedy ) [3] => Array ( [genre_id] =>
4 [genre_name] => Drama ) [4] => Array ( [genre_id] => 5 [genre_name]
=> Fantasy ) [5] => Array ( [genre_id] => 6 [genre_name] => Historical ) [6] => Array ( [genre_id] => 7 [genre_name] => Horror ) [7] => Array
( [genre_id] => 8 [genre_name] => Psychological ) [8] => Array (
[genre_id] => 9 [genre_name] => Romance ) [9] => Array ( [genre_id] =>
10 [genre_name] => Sci-fi ) [10] => Array ( [genre_id] => 11
[genre_name] => Mystery ) [11] => Array ( [genre_id] => 12
[genre_name] => Tragedy ) [12] => Array ( [genre_id] => 13
[genre_name] => Short Story ) [13] => Array ( [genre_id] => 14
[genre_name] => Satire ) ) )
I've been looking at various questions regarding arrays from assoc to multidimensional and other stuff. Then i finally visited php manual for foreach loop and i finally was able to echo the array. The problem now is that although it echoes my array it also has an error for each, i can probably fix the error part by declaring it as a defined variable. My question is, is there any other better way? or any improvement on how i made my array to make it cleaner or easier to print?
foreach($tag_genre as $key => $value){
foreach($value as $values){
echo $values['tag_name'];
}
}
UPDATE: i changed the way i made the array.
This is now my model:
public function get_tags(){
$query = $this->db->get('tags')->result_array();
foreach($query as $row){
$result[$row['tag_id']] = $row['tag_name'];}
return $result;
}
public function get_genre(){
$query = $this->db->get('genre')->result_array();
foreach($query as $row){
$result[$row['genre_id']] = $row['genre_name'];}
return $result;
}
and my controller:
public function view_publish_story(){
$data = array('tag' => $this->story_model->get_tags(), 'genre' => $this->story_model->get_genre(), 'title' => "New Story");
$this->load->view('template/header',$data);
$this->load->view('template/navbar');
$this->load->view('pages/storypublish',$data);
$this->load->view('template/footer');
}
and in my view:
<tr>
<div class="form-group">
<td><label for="genre">Genre</label></td>
<?php
foreach($genre as $genre_id => $genre_name){
echo "<td><label class='checkbox-inline'><input type='checkbox' value=''>".$genre_name."</label><td>";
}
?>
</div>
</tr>
My problem now is that, how do i fit all these into my table? I just ruined my table right now since i echoed them all in a single line. How do i do it so that it is printed in 3 columns?
foreach($tag_genre as $key => $value){
foreach($value as $values){
echo '<pre>';
echo $values['tag_name'];
}
}
this will format your array in a way you can read it and understand it.
I have an array that has the following values
[0] => array(4) {
["sku"] => string(12) "WMS-M-VN-MRN"
["name"] => string(62) "Maroon V-neck Jumper"
["qty_ordered"] => string(6) "1.0000"
["product_options"] => string(533) "a:2:{s:15:"info_buyRequest";a:6:{s:4:"uenc";s:64:"aHR0cDovL2VtYmxlbWF0aWMuY28udWsvd21zLW0tdm4tbXJuLz9fX19TSUQ9VQ,,";s:7:"product";s:3:"780";s:8:"form_key";s:16:"gDXvCEtQOlRWihqc";s:15:"related_product";s:0:"";s:7:"options";a:1:{i:1970;s:5:"17201";}s:3:"qty";s:1:"1";}s:7:"options";a:1:{i:0;a:7:{s:5:"label";s:27:"PLEASE SELECT SIZE REQUIRED";s:5:"value";s:12:"36 inch (13)";s:11:"print_value";s:12:"36 inch (13)";s:9:"option_id";s:4:"1970";s:11:"option_type";s:9:"drop_down";s:12:"option_value";s:5:"17201";s:11:"custom_view";b:0;}}}"
}
Now is there a way to unserialise the "Product_options" field in-line, without having to break the array apart and rebuild it again?
This is the way. You just have to reassign the result of unserialize to the same array item.
<?php
$array = [
[
"sku" => "WMS-M-VN-MRN",
"name" => "Maroon V-neck Jumper",
"qty_ordered" => "1.0000",
"product_options" => 'a:2:{s:15:"info_buyRequest";a:6:{s:4:"uenc";s:64:"aHR0cDovL2VtYmxlbWF0aWMuY28udWsvd21zLW0tdm4tbXJuLz9fX19TSUQ9VQ,,";s:7:"product";s:3:"780";s:8:"form_key";s:16:"gDXvCEtQOlRWihqc";s:15:"related_product";s:0:"";s:7:"options";a:1:{i:1970;s:5:"17201";}s:3:"qty";s:1:"1";}s:7:"options";a:1:{i:0;a:7:{s:5:"label";s:27:"PLEASE SELECT SIZE REQUIRED";s:5:"value";s:12:"36 inch (13)";s:11:"print_value";s:12:"36 inch (13)";s:9:"option_id";s:4:"1970";s:11:"option_type";s:9:"drop_down";s:12:"option_value";s:5:"17201";s:11:"custom_view";b:0;}}}'
]
];
$array[0]["product_options"] = unserialize($array[0]["product_options"]);
echo "<pre>";
print_r($array);
echo "</pre>";
Output:
Array
(
[0] => Array
(
[sku] => WMS-M-VN-MRN
[name] => Maroon V-neck Jumper
[qty_ordered] => 1.0000
[product_options] => Array
(
[info_buyRequest] => Array
(
[uenc] => aHR0cDovL2VtYmxlbWF0aWMuY28udWsvd21zLW0tdm4tbXJuLz9fX19TSUQ9VQ,,
[product] => 780
[form_key] => gDXvCEtQOlRWihqc
[related_product] =>
[options] => Array
(
[1970] => 17201
)
[qty] => 1
)
[options] => Array
(
[0] => Array
(
[label] => PLEASE SELECT SIZE REQUIRED
[value] => 36 inch (13)
[print_value] => 36 inch (13)
[option_id] => 1970
[option_type] => drop_down
[option_value] => 17201
[custom_view] =>
)
)
)
)
)
I have json return string like given below. I want to extract cancellation Policy list of objects like cutoff Time and refund In Percentage. I tried using for-loop but it didn't help me. Can you please help me on extracting this.
Array (
[apiStatus] => Array ( [success] => 1 [message] => SUCCESS ) <br>
[apiAvailableBuses] => Array ( <br>
[0] => Array ( [droppingPoints] => [availableSeats] => 41 <br>[partialCancellationAllowed] => [arrivalTime] => 08:00 AM <br>
[cancellationPolicy] => [<br>
{"cutoffTime":"1","refundInPercentage":"10"},<br>
{"cutoffTime":"2","refundInPercentage":"50"},<br>
{"cutoffTime":"4","refundInPercentage":"90"}<br>
] <br>
[boardingPoints] => Array ( [0] => Array ( [time] => 09:00PM [location] => Ameerpet,|Jeans Corner 9687452130 [id] => 6 ) [1] => Array ( [time] => 09:15PM [location] => S.R Nagar,S.R Nagar [id] => 2224 ) [2] => Array ( [time] => 09:10PM [location] => Kondapur,Toyota Show room [id] => 2244 ) ) [operatorName] => Deepak Travels [departureTime] => 9:00 PM [mTicketAllowed] => [idProofRequired] => [serviceId] => 6622 [fare] => 800 [busType] => 2+1 Hi-Tech Non A/c [routeScheduleId] => 6622 [commPCT] => 0 [operatorId] => 213 [inventoryType] => 0 ) <br>
[1] => Array ( [droppingPoints] => [availableSeats] => 41 [partialCancellationAllowed] => [arrivalTime] => 07:00 AM <br>
[cancellationPolicy] => [<br>
{"cutoffTime":"1","refundInPercentage":"10"},<br>
{"cutoffTime":"2","refundInPercentage":"50"},<br>
{"cutoffTime":"4","refundInPercentage":"90"}<br>
] <br>
[boardingPoints] => Array ( [0] => Array ( [time] => 09:10PM [location] => Ameerpet,|Jeans Corner [id] => 6 ) [1] => Array ( [time] => 09:00PM [location] => S.R Nagar,S.R Nagar [id] => 2224 ) [2] => Array ( [time] => 08:30PM [location] => KUKATPALLY,JNTU [id] => 2230 ) ) [operatorName] => Dhanunjayabus [departureTime] => 9:00 PM [mTicketAllowed] => [idProofRequired] => [serviceId] => 6743 [fare] => 900 [busType] => VOLVO [routeScheduleId] => 6743 [commPCT] => 0 [operatorId] => 233 [inventoryType] => 0 )
)
)
Use a foreach() for it like so:
foreach ($your_response['apiAvailableBuses'] as $el) {
$cancellationPolicy[] = $el['cancellationPolicy'];
}
Try this:
foreach($data['apiStatus']['apiAvailableBuses'] as $item) {
foreach($item['cancellationPolicy'] as $key => $json) {
$jsonDecoded = json_decode($json, true);
// And you will have access to the data like this
// $jsonDecoded['cutoffTime'];
// $jsonDecoded['refundInPercentage'];
}
}
$response = json_decode($apiResponse);
$cancellationPolicies = [];
foreach ($response->apiAvailableBuses as $availableBus) {
$cancellationPolicies[] = $availableBus['cancellationPolicy'];
// if you want to display something you can simply do it like this;
echo $availableBus['cancellationPolicy']->cutoffTime;
}
I'm so close to this I could just scream.
Here's what I'm after. I have two arrays. The first one is a follows:
array("id", "txtLname", "txtFname");
The second is as follows:
Array
(
[0] => Array
(
[id] => 220
[RecordGUID] => 1233C9-1F7A15-E8A447-C56CB2-227C20-2829E0
[txtEmplid] => 5469857
[txtLname] => Jones
[txtFname] => Richard
[txtMname] =>
[txtEmail] => email address
[Reg_Pass] => umbra1234
[Reg_User] => user
[txtSecEmail] => user#gmail.com
[dtDOB] => 1979-02-28
[drpStatus] => STUDENT
[lstWaive] => 1
[ENTERED] => 2013-09-15 18:03:18
[Status] => 0
[Approvalcode] => 17dc8e7336f0e9fd3411a4d9617efe865c5744ac
[Approvaldate] => 2013-09-15 18:03:47
[Approved] => 1
)
[1] => Array
(
[id] => 221
[RecordGUID] => DD1E72-368879-68CFE2-E03010-ECE1B1-0974E9
[txtEmplid] => 4454688
[txtLname] => Mathews
[txtFname] => Richard
[txtMname] =>
[txtEmail] => user2#gmail.com
[Reg_Pass] => umbra1234
[Reg_User] => user
[txtSecEmail] => user3#gmail.com
[dtDOB] => 1979-02-28
[drpStatus] => STUDENT
[lstWaive] => 1
[ENTERED] => 2013-09-16 12:28:08
[Status] => 0
[Approvalcode] => 7182769e45dc38a3a747c4bdb128e2f0a8c658e4
[Approvaldate] =>
[Approved] => 0
)
[2] => Array
(
[id] => 222
[RecordGUID] => 40D8E7-04C600-30A829-8E26CC-498BBE-9D3DF6
[txtEmplid] =>
[txtLname] =>
[txtFname] =>
[txtMname] =>
[txtEmail] =>
[Reg_Pass] =>
[Reg_User] =>
[txtSecEmail] =>
[dtDOB] => 1979-02-28
[drpStatus] => STUDENT
[lstWaive] => 1
[ENTERED] => 2013-09-16 12:28:24
[Status] => 0
[Approvalcode] => 8d2c33f7b7d6ef620811dbc4358e8103346bfb59
[Approvaldate] =>
[Approved] => 0
)
)
All I need is to loop through the second array and remove the items that do not match the first. The result would be as follows:
Array
(
[0] => Array
(
[id] => 220
[txtLname] => Method
[txtFname] => Richard
)
[1] => Array
(
[id] => 221
[txtLname] => Method
[txtFname] => Richard
)
[2] => Array
(
[id] => 222
[txtLname] => Method
[txtFname] => Richard
)
)
Here's what I've done so far.
foreach ($r as $reg) {
foreach($reg as $k => $v) {
if ($k == !in_array($k, $f)) {
echo $reg[$k];
}
}
}
The echo response I get is a listing of all of the data from the first array, minus the fields that are in the first array. So, it is removing items that
I want removed, but I can't seem to get it into the proper format.
Thanks in advance for your help.
$arr1 = array("id"=>1, "txtLname"=>1, "txtFname"=>1)
$result = array();
// $arr2 = array 2 (check your code)
foreach ($arr2 as $el) {
$result[] = array_intersect_key($el, $arr1);
}
var_dump($result);
replace echo $reg[$k]; with unset($reg[$k]);