foreach with two kind of values - php

<?php
foreach ($array['response']['data']['Offers'] as $arr) {
$gegevens = array(
$arr['Offer']['id'],
$arr['Offer']['name'],
$arr['Advertiser']['company'],
$arr['Offer']['advertiser_id'],
$arr['Offer']['offer_url'],
$arr['Offer']['default_payout'],
$arr['Offer']['expiration_date']
);
echo '<tr>';
foreach ($gegevens as $value) {
echo "<td>{$value}</td>";
}
echo "</tr>\n";
}
?>
This is the code I have.
How can I search for two kind of values inside the foreach($array['response']['data']?
It has to be foreach($array['response']['data'][**Offers**] and also foreach($array['response']['data'][**Advertisers**].
I need this so that I can echo out $arr['**Offer**']['name'], $arr['**Advertiser**'] ['company']
Can someone help me with this?

In its simplest:
foreach($array['response']['data']['Offers'] as $key => $offer_arr){
$advertiser_arr = $array['response']['data']['Advertisers'][$key];
}
You then have offer array and advertiser array of the same index

Doesn't sound like you need to put them all into an array. This should suffice:
<?php
foreach($array['response']['data']['Offers'] as $arr) {
echo '<tr>';
echo "<td>{$arr['Offer']['name']}</td>";
echo "<td>{$arr['Advertiser']['company']}</td>";
echo "</tr>\n";
}
?>

Related

PHP code to separate Table based on column values

I have a csv file that should be converted to HTML table based on column values , and I have to put the values into separate table based on first column of csv . And I think the problem is because of '\n' in first column.
So its like this:
Here in Result column, in one row I have three values separated using comma (W,M,P). In the code I wrote it is considered as separate table headers .
Can anyone please help me with this?
This is my code:
<?php
$csv="FinalResult.csv" ;
$csvcontents=file_get_contents($csv);
$csv_array = explode("\n", $csvcontents);
$tables = [];
foreach($csv_array as $key => $value) {
if ($key == 0) {
continue;
}
$line = explode(',', $value);
if (array_key_exists($line[0], $tables)) {
$tables[$line[0]][] = $line;
} else {
$tables[$line[0]] = [$line];
}
}
foreach ($tables as $key => $value) {
echo '<h1> ' .$key. ' </h1>'; // YOUR TITLE (Team)
echo "<table>";
echo '<tr>';
foreach (explode(',', $csv_array[0]) as $keyHeader => $valueHeader) {
if (in_array($keyHeader, [0, 1])) {
continue;
}
echo "<th>$valueHeader</th>";
}
echo '</tr>';
foreach ($value as $keyRow => $valueRow) {
echo '<tr>';
foreach ($valueRow as $keyValue => $valueValue) {
if (in_array($keyValue, [0, 1])) {
continue;
}
echo "<td>$valueValue</td>";
}
echo '</tr>';
}
echo '</table>';
}
?>
I refereed in stack overflow , but there they were giving only single values to a column and not providing multiple values .
But am getting output like this ,
so it takes the value of Result column after '-' as a new heading , i tried but am not able to solve this , can anyone really help me in this matter .
Here is how my output should look like:
I marked in yellow where all the data am getting in same column
This is my csv file :
Team,Date,Opponent,Result
MIN,May-03,UTA,a.b.c=d-e.f-g.h=log4j2-i.xml-j -k -a4j.k=tp_r-RR.xml -
MIN,May-04,SEA,"L,Q,J"
SAC,May-03,DAL,L
SAC,May-04,TOR,W
NYN,May-05,BAL,L
NYN,May-07,MIA,W
Here is the code
<table>
<?php
$csvValues= array_map('str_getcsv', file('FinalResult.csv'));
$counter = 0;
// Header
echo "<tr>";
foreach($csvValues[0] as $headers){
echo "<th>".$headers."</th>";
}
echo "</tr>";
// Content
foreach($csvValues as $values){
echo "<tr>";
if($counter >0){
foreach($values as $data){
echo "<td>".$data."</td>";
}
}
echo "</tr>";
$counter++;
}
?>
</table>

avoid $ sign on php for print array value from json

I have to Simple code retrieve data from json.i got problem with json properties that contains like department$_identifier. 'department$_identifier' properties contains the value.but php count it as a variable.how to escape this.
Here is my code :
foreach ($data as $key => $Attendance) {
if ($Attendance->department$_identifier == 'Administration') {
echo $Attendance->department$_identifier;
echo $Attendance->attendanceDate;
echo $Attendance->status;
echo $Attendance->_entityName;
}
}
How to solve this
use {} like as
echo $Attendance->{'department$_identifier'};
http://php.net/manual/en/function.json-decode.php
Try this
foreach ($data as $key => $Attendance) {
if ($Attendance["department$_identifier"] == 'Administration') {
echo $Attendance["department$_identifier"];
echo $Attendance["attendanceDate"];
echo $Attendance["status"];
echo $Attendance["_entityName"];
}
}

How to loop through json array to convert it into single values and list them?

i have fetched a json output for menus to be displayed in the webpage.
I want to convert it into menus and sub menus. I have tried looping through it but it doesnt works.Please help me with this, thanks in advance.
$output = json_encode(array($menu_main_row['menu_name'] => $sub_menu));
Json Output:
{"Home":"[{\"Create\":\"create.php\"},{\"Edit\":\"edit.php\"}]"}
{"Projects":"[{\"My Projects\":\"myprojects.php\"},{\"My reports\":\"reports.php\"}]"}
This is the foreach loop which i used.
foreach($value as $main_menu_name=>$arr_val_sub)
{
echo "<ul>";
echo $main_menu_name;
$val=json_decode($arr_val_sub);
foreach ($val as $menu_name=>$menu_url)
{
echo $menu_name;
echo "<br>";
echo $menu_url;
}
}
The main menu name is printing, but when i print the sub menu name and url it prints like 0 1 .
You missed another step to dig into the json. $menu_url is another json to parse. Below code fixes it
foreach($value as $main_menu_name=>$arr_val_sub)
{
echo "<ul>";
echo $main_menu_name;
$val=json_decode($arr_val_sub);
foreach ($val as $menu_name=>$menu_url)
{
$submenu=new RecursiveArrayIterator($menu_url);
foreach ($submenu as $submenu_name=>$submenu_url)
{
echo $submenu_name;
echo "<br>";
echo $submenu_url;
echo "<br>";
}
}
}

Multiple nested array from MySQL query

I'm using foreach loops to access records in a nested array:
while ($row = mysql_fetch_assoc($result)){
$test_groups[$row['group_name']][] = $row['lab_test'];
}
foreach($test_groups as $group_name => $tests){
echo "<tr><td><span class='test_group_name'>" . $group_name . "</span></td></tr>";
foreach($tests as $test){
echo "<tr><td>" . $test . "</td></tr>";
}
echo "<tr><td> </td></tr>";
}
echo '</table>';
This works OK. Now I need to add another level to the nested array, like:
$departments[$row['department_name']][$row['group_name']][] = $row['lab_test'];
Would this work and how should I adjust the for each loops above to cater for this?
Assign to array should be with [] at the end
$departments[$row['department_name']][$row['group_name']][] = $row['lab_test'];
Foreach loop will be:
foreach ($departments as $dep_name => $groups) {
echo $dep_name;
foreach ($groups as $group_name => $tests) {
echo $group_name;
foreach ($tests as $test) {
echo $test;
}
}
}
It's just with echoes, make there HTML as you need.

how to display the name of an array which is inside another array in php

I am new to php and i have a small question.
This is my array
$cars=array( "1234"=> array("Toyota","100","2","white"),
"2468"=> array("Mazda","1000","0","red"),
"4587"=> array("Mercedes","200","0","green")
);
$_SESSION['cars']=$cars;
the elements 1234, 2468 and 4587 are basically registration numbers of the cars and my task is to insert these registration number in a table.
if( isset($_SESSION['cars']))
{
foreach($_SESSION['cars'] as $key)
{?>
<tr><td><?php echo $key?></td></tr>
this is wht i did but it gives me an error saying Notice: Array to string conversion.
can anyone tell me how to do this ? I'll be grateful
This should work for you:
if( isset($_SESSION['cars'])) {
foreach($_SESSION['cars'] as $key => $v)
echo "<tr><td>" . $key . "</td></tr><br />";
}
EDIT:
It should work see this example:
<?php
session_start();
$cars = array(
"1234"=> array("Toyota","100","2","white"),
"2468"=> array("Mazda","1000","0","red"),
"4587"=> array("Mercedes","200","0","green")
);
$_SESSION['cars'] = $cars;
if( isset($_SESSION['cars'])) {
foreach($_SESSION['cars'] as $key => $v)
echo "<tr><td>" . $key . "</td></tr><br />";
}
?>
Output:
1234
2468
4587
<?php
$cars=array( "1234"=> array("Toyota","100","2","white"),
"2468"=> array("Mazda","1000","0","red"),
"4587"=> array("Mercedes","200","0","green")
);
$_SESSION['cars']=$cars;
//In separated file
if(!isset($_SESSION)) session_start();
if( isset($_SESSION['cars']))
{
foreach($_SESSION['cars'] as $key => $value)
{
echo "<tr><td> $key </td></tr>";
}
}
?>

Categories