I have a multi dimensional array with the structure:
Name
Address
Area
Area Type 1
Area Type 2
Area Type 2
I have a loop that grabs the Name and Address for each business and echos them out. Now what I want to be able to do is now grab the area and chuck that out for each business. My loop thus far is as follows:
foreach ($data AS $key => $value) {
echo '<ul>';
echo '<li>';
echo $value['Name'];
echo '</li>';
echo '<li>';
echo $value['Address'];
echo '</li>';
echo '<li>';
foreach ($data as $row) {
echo $data['Area'];
}
echo '</li>';
echo '</ul>';
}
I can output all the Areas in one go with:
foreach($data as $row)
{
foreach($row['Area'] as $areaout)
{
echo $areaout;
}
}
But I need it to echo out with it's respective name and address
Shouldn't you simply replace
foreach ($foo as $row) {
echo $foo['Area'];
}
by
echo '<ul>';
foreach ($value['Area'] as $v) {
echo '<li>' . $v . '</li>';
}
echo '</ul>'
?
Related
I'm Trying to format the output of an array using php but I can't seem to get the keys and values on the same line. I've listed the code that I'm using to display the keys and values but this code outputs the keys and values on different lines
function olLiTree($tree)
{
echo '<pre>';
foreach($tree as $key => $item) {
if (is_array($item)) {
echo '<pre>', $key ;
olLiTree($item);
echo '</pre>';
} else {
echo '<pre>', $item, '</pre>';
}
}
echo '</ul>';
}
print(olLiTree($results));
Use <ul> <li> .... </li></ul>. Also remove comma(,) because PHP use dot(.) for concat string.
function olLiTree($tree)
{
echo '<ul>';
foreach($tree as $key => $item) {
if (is_array($item)) {
echo '<li>'. $key ;
olLiTree($item);
echo '</li>';
} else {
echo '<li>' .$item. '</li>';
}
}
echo '</ul>';
}
print(olLiTree($results));
You use comma , instead of dot .
for string concatenation php use dot
function olLiTree($tree)
{
echo '<pre>';
foreach($tree as $key => $item) {
if (is_array($item)) {
echo '<pre>'. $key ;
olLiTree($item);
echo '</pre>';
} else {
echo '<pre>' . $item. '</pre>';
}
}
echo '</ul>';
}
print(olLiTree($results));
I want generate a link using two array: the first one contains addresses; the second one contains text.
I want have:
- text3
- text3
- text3
to do so I tried like this but I can't generate texts.
<ul>
<?php
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$vocimenu=array("address1","address2","address3");
$nomimenu=array("text1","text2","text3");
$nome=array_values($nomimenu);
foreach($vocimenu as $voce) {
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$voce.'">';
echo $nome;
if($page!=$voce) echo "</a>";
echo "</li>";
}
?>
</ul>
You can use one array
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$links=array("address1"=>"text1","address2"=>"text2","address3"=>"text3");
foreach($links as $href=>$text){
if($page!=$voce){
echo ''.$text.'';
}else{
echo $text;
}
}
This should work:
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$vocimenu=array("address1","address2","address3");
$nomimenu=array("text1","text2","text3");
//since you're using two arrays, foreach is not the way to go
//you need a counter so you can get elements from each array
for ($i=0;$i<count($vocimenu);$i++) {
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$vocimenu[$i].'">';
echo $nomimenu[$i];
if($page!=$voce) echo "</a>";
echo "</li>";
}
An alternate option is to do it like this, but that could make some of your other code less flexible:
$array = array("address1"=>"value1","address2"=>"value2",...);
foreach($array as $address=>$value){
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$address.'">';
echo $value;
if($page!=$voce) echo "</a>";
echo "</li>";
}
It would be much easier if you create an associative array:
$menu = array(
"fmp_trama" => "Full Metal Panic!",
"fumoffu_trama" => "Full Metal Panic? Fumoffu",
"fmp_tsr" => "Full Metal Panic! TSR"
);
echo '<ul>';
foreach ($menu as $key => $value) {
echo "<li>";
if($page != $key) {
echo sprintf('%s', $key, $value);
}
else {
echo sprintf('<span>%s</span>', $value);
}
echo "</li>";
}
echo '</ul>';
You can build the array like this: (if you are bound to the 2 array structure)
$menu = array_combine($vocimenu, $nomimenu);
Needed Navigation Html
Home
Pages
About
Services
Products
Contact
FAQs
Sitemap
Privacy Policy
Column Layouts
1 Column
2 Column (Left Sidebar)
2 Column (Right Sidebar)
3 Column
4 Column
I want to use php arrays and foreach loops to output the needed html.
The php code I have thus far is:
<?php
$data = array("navigation");
$data['navigation']['Home'] = base_url();
$data['navigation']['Pages'] = base_url('pages');
$data['navigation']['Pages']['About'] = base_url('pages/about');
echo '<ul>';
foreach($data as $nav) {
foreach($nav as $subNavKey => $subNavHref) {
echo "<li><a href='$subNavHref'>$subNavKey</a>";
}
}
echo '</ul>';
?>
I was thinking I would need three foreach loops nested but php warnings/errors are generated when the third loop is reached on lines such as:
$data['navigation']['Home'] = base_url();
$data['navigation']['Pages'] = base_url('pages');
I'm not quite sure how to test for 3rd level depths such as:
$data['navigation']['Pages']['About'] = base_url('pages/about');
Also, outputting the needed li and ul tags in the proper positions has given me trouble aswell.
Use recursion
$data['navigation']['Home'] = base_url();
$data['navigation']['Pages'] = base_url('pages');
$data['navigation']['Pages']['About'] = base_url('pages/about');
$data['navigation']['Pages']['About']['Team'] = base_url('pages/team');
$data['navigation']['Pages']['About']['Team']['Nate'] = base_url('pages/nate');
echo "<ul>"
print_list($data);
echo "</ul>"
function print_list($menu) {
foreach($menu as $key=>$item) {
echo "<li>";
if(is_array($item)) {
echo "<ul>";
print_list($item);
echo "</ul>";
} else {
echo "<a href='{$val}'>$key</a>";
}
echo "</li>";
}
}
<?php
function nav($data) {
$html = '<ul>';
foreach ($data as $k => $v) {
if (is_array($v)) {
$html .= "<li>$k" . nav($v) . "</li>";
}
else {
$html .= "<li><a href='$k'>$v</a>";
}
}
$html .= '</ul>';
return $html;
}
echo nav($data);
A recursive function can get the job done:
$items = array(
"Home",
"Pages" => array(
"About",
"Services",
"Products",
"Contact",
"FAQs",
"Sitemap",
"Privacy Policy",
"Column Layouts" => array(
"1 Column",
"2 Column (Left Sidebar)",
"2 Column (Right Sidebar)",
"3 Column",
"4 Column"
)
)
);
function getMenu($array) {
foreach($array as $key => $value) {
if(is_array($value)) {
echo "<li>" . $key . "</li>";
echo "<ul>";
getMenu($value);
echo "</ul>";
} else {
echo "<li>" . $value . "</li>";
}
}
}
echo "<ul>";
getMenu($items);
echo "</ul>";
Output:
You should use a recursive function, for example (Working Demo):
function makeMenu($array)
{
$menu = '';
foreach($array as $key => $value) {
if(is_array($value)) {
$menu .= '<li>' . $key . '<ul>' . makeMenu($value) . '</ul></li>';
}
else {
$menu .= "<li><a href='". $value ."'>" . $value ."</a></li>";
}
}
return $menu;
}
Then call it like:
$data = array(
"Home",
"Pages" => array("About", "Services"),
"Column Layouts" => array("1 Column", "2 Column (Left Sidebar)")
);
echo '<ul>' . makeMenu($data) . '</ul>';
Basically I'm trying to group my arrays like this:
Shopping
Amazon
Social
Amoeblo
American express
By using below PHP code:
<?php
echo '<ul id="list"><h2 class="searchresults"></h2>';
foreach($records as $catval) {
$sitechar = $catval->site_category;
echo '<h3 id="disappear">'. strtoupper($sitechar) .'</h3>';
echo '<li class="siteli"><a href="#" class="add">';
echo '<p id="text-site">'.$catval->site_name. '</p></a>';
echo '</li>';
}
echo '</ul>';
?>
But I'm getting values only like below.
Shopping
Amazon
Social
Amoeblo
Social
American express
I'm not getting the exact PHP sorting to use for this.
I would create a new array with categories as keys for arrays with the sites.
<?php
$arr = array();
// First create multidimensional array with categories as keys for site arrays
foreach($records as $catval) {
$sitechar = $catval->site_category;
if (!array_key_exists($sitechar, $arr)) {
// Set new array for a category if it does not exist
$arr[$sitechar] = array();
}
// Add site to category
$arr[$sitechar][] = array(
"name"=>$catval->site_name,
"image"=>$catval->site_img
);
}
// Then iterate the new array of categories
echo ("<ul>");
foreach($arr as $category => $sites) {
echo("<h3>" . $site_category "</h3>");
// Iterate array of sites
foreach($sites as $site) {
echo("<li>" . $site["name"] . "-" , $site["image"] . "</li>");
}
}
echo("</ul>");
?>
You can do it using a foreach and ksort
Let $your_array be the array you mentioned above
$res_array = array();
foreach($your_array as $val){
$res_array[$val->site_category][] = $val->site_name;
}
ksort($res_array);
print_r($res_array);
OR search for multisort in php which will solve your problem :)
$tmp = null;
echo '<ul id="list"><h2 class="searchresults"></h2>';
foreach($records as $catval) {
$myHtml = makeHtml($catval,$tmp);
echo $myHtml;
$tmp = $catval->site_category;
}
echo '</ul>';
function makeHtml($catval,$tmp){
if($tmp != $catval->site_category){ $html .= '<h3 id="disappear">'. strtoupper($catval->site_category) .'</h3>';}
$html .='<li class="siteli"><p id="text-site">'.$catval->site_name. '</p></li>';
return $html;
}
I'm getting an error while looping over an element children:
$placeById=simplexml_load_file("http://www.43places.com/service/get_place_by_id?api_key=1234&id=".$placeId);
$children=$placeById->children;
echo '<ul>';
foreach ($children as $child)
{
echo '<li>'.$child->child.'</li>';
}
echo '</ul>';
My Output is only this:
Alabama
Although there are several children, see the XML here
Your only referencing the first child, try this:
$placeById=simplexml_load_file("http://www.43places.com/service/get_place_by_id?api_key=1234&id=".$placeId);
$children=$placeById->children;
echo '<ul>';
foreach ($children->child as $child) {
echo '<li>'.$child.' - '. $child['id'].'</li>';
}
echo '</ul>';