I have a function common to sites based in different locations. The two-dimensional array looks something like this:
$sites = array(
'UK' => array(
"sitecode" => "AUTest",
"domain" => ".com.au",
"label" => "Australia",
),
'DE' => array(
"sitecode" => "DETest",
"domain" => ".de",
"label" => "Germany"
),
'CA' => array(
"sitecode" => "CATest",
"domain" => ".ca",
"label" => "Canada"
)
);
I'd like to display a list of all countries on each site, but always display the current country at the top of the list, with the others listed in their natural order, e.g.
In Australia, I want to see:
<li class="foo">Australia</li>
<li>Germany</li>
<li>Canada</li>
In Germany:
<li class="foo">Germany</li>
<li>Australia</li>
<li>Canada</li>
In Canada:
<li class="foo">Canada</li>
<li>Australia</li>
<li>Germany</li>
If I have something like this:
foreach ($sites as $key => $list) {
if (My_External_Variable == $list['sitecode']) {
echo '<li><a class="foo" href="http://www.example' . $list['domain'] .'/">' . $list['label'] . '</a>';
}
else {
echo '<li>' . $list['label'] . '</li>';
}
}
I can find the current country site I'm viewing and, for instance, apply a css class, but as it is, it's always going to display in it's index order.
I've looked at a lot of sort man pages, but have to confess I'm not a clever man. I think what I need to do is dynamically change the index based on the country site I'm currently viewing so that I can place it first, then iterate through the remaining. I'm sure it can be done, but am stuck trying to make it so.
I'm hoping someone can point me in the right direction.
you can save the output to variable and the selected to variable and after the loop chain them together so the first one will be the selected
$list = ''; $selected = '';
foreach ($sites as $key => $list) {
if (My_External_Variable == $list['sitecode']) {
$selected = '<li><a class="foo" href="http://www.example' . $list['domain'] .'/">' . $list['label'] . '</a>';
}
else {
$list .= '<li>' . $list['label'] . '</li>';
}
}
echo $selected . $list;
Why dont use another array to describe the order?
<?php
$sites = array(
'UK' => array(
"sitecode" => "AUTest",
"domain" => ".com.au",
"label" => "Australia",
),
'DE' => array(
"sitecode" => "DETest",
"domain" => ".de",
"label" => "Germany"
),
'CA' => array(
"sitecode" => "CATest",
"domain" => ".ca",
"label" => "Canada"
)
);
$sites_order = array(
"UK" => array("UK", "DE", "CA"),
"DE" => array("DE", "UK", "CA"),
"CA" => array("CA", "UK", "DE")
);
$selected_language = "DE";
foreach($sites_order[$selected_language] as $language) {
printf("<pre>%s</pre>",$sites[$language]["label"]);
}
?>
Related
I am trying to implement a decision tree based on this jQuery plugin.
It expects data in this format:
$tree = array(
"href" => "/",
"label" => "Title",
"nodes" => array(
array(
"href" => "/q1",
"label" => "Question 1?",
"nodes" => array(
array(
"href" => "/q1/a1",
"label" => "Answer 1",
"nodes" => array(
"href" => "/q1/a1/q11",
"label" => "Question 1.1?",
"nodes" => array(
array(
"href" => "/q1/a1/q11/a11",
"label" => "Answer 1.1",
"nodes" => null
),
array(
"href" => "/q1/a1/q11/a12",
"label" => "Answer 1.1",
"nodes" => null
)
)
)
),
array(
"href" => "/q1/a2",
"label" => "Answer 2",
"nodes" => null
)
)
)
)
);
I fetch the results as a flat list from a database and then am able to generate the tree structure using a recursive function:
function buildTree(array $elements, $rootId = 0, $current_path = '', $new_path = '') {
$ctr = 0;
$branch = array();
foreach($elements as $element) {
if ($element['root_id'] == $rootId) {
$array = array(
'root_id' => $element['root_id'],
'label' => $element['label'],
'text' => $element['text'],
);
if(!empty($current_path)){
$path = $current_path . ++$ctr;
$array['href'] = $path;
}
$new_path = $new_path == '/q' ? '/a' : '/q';
$children = buildTree($elements, $element['id'], $path . $new_path, $new_path);
if ($children) {
$array['nodes'] = $children;
}
$branch[] = $array;
}
}
return $branch;
}
$tree = buildTree($results);
which yields:
$tree = array(
"href" => "/",
"label" => "Title",
"nodes" => array(
array(
"href" => "/q1",
"label" => "Question 1?",
"nodes" => array(
array(
"href" => "/q1/a1",
"label" => "Answer 1",
"nodes" => array(
"href" => "/q1/a1/q1",
"label" => "Question 1.1?",
"nodes" => array(
array(
"href" => "/q1/a1/q1/a1",
"label" => "Answer 1.1",
"nodes" => null
),
array(
"href" => "/q1/a1/q1/a1",
"label" => "Answer 1.1",
"nodes" => null
)
)
)
),
array(
"href" => "/q1/a2",
"label" => "Answer 2",
"nodes" => null
)
)
)
)
);
which gives the correct 'href's up to a depth of 2 but then deviates from the expected format.
I can't figure out how to get the 'href's correct.
How can i generate the correct 'href' using my recursive function as expected by the jQuery plugin?
$path = $current_path . ++$ctr;
You're counting yet this value is localized for that particular function even on recursion. It will never and seems to never move above 1 because after it iterates once, it calls buildTree again.
Is q11 not stored in the database? You need a way of incrementing/parsing/gathering (i am not sure exactly what the value should be) that value to what it should be as it recurses through. Should this value just remove the . from 1.1 ?
Try dropping in this function in place of yours and see if it changes anything:
function buildTree(array $elements, $rootId = 0, $current_path = '', $new_path = '', $parent_id = 0, $ctr = 0) {
$branch = array();
foreach($elements as $element) {
if ($element['root_id'] == $rootId) {
$array = array(
'root_id' => $element['root_id'],
'label' => $element['label'],
'text' => $element['text'],
);
if(!empty($current_path)){
if($parent_id > 0 ) {
$path = $current_path . $parent_id . $ctr;
}else{
$path = $current_path . ++$ctr;
}
$array['href'] = $path;
}
$new_path = $new_path == '/q' ? '/a' : '/q';
$parent_id++;
$children = buildTree($elements, $element['id'], $path . $new_path, $new_path, $parent_id, $ctr);
$parent_id--;
if ($children) {
$array['nodes'] = $children;
}
$branch[] = $array;
}
}
return $branch;
}
I have this array:
$lk = array(
'About_US' => array(
'en' => 'about-us',
'es' => 'sobre-nosotros',
),
'Shared_Hosting' => array(
'en' => 'website-shared-hosting',
'es' => 'alojamiento-de-sitios',
),
And I need to get the name "Shared_Hosting" by this type of search
$key = array_search('website-shared-hosting', array_column($lk, 'en'));
echo $key;
The value of $key for me shows "1" instead of "Shared_Hosting", its because we have "0" as "About_US" and "1" as "Shared_Hosting" (i believe).
If I try to echo $key this will result in "1"
I tried for all day to get $key = "Shared_Hosting"
Tried so many ways and functions!
Maybe another function instead of array_column?
Thank you everyone for read it and help!
Here's my easiest solution I could find, I don't think there would be a one-liner:
<?php
$lk = array(
'About_US' => array(
'en' => 'about-us',
'es' => 'sobre-nosotros',
),
'Shared_Hosting' => array(
'en' => 'website-shared-hosting',
'es' => 'alojamiento-de-sitios',
)
);
$foundIn = [];
forEach($lk as $key => $sub) {
if(array_search('website-shared-hosting', $sub)) {
$foundIn[] = $key;
}
}
var_dump($foundIn);
// Output: array(1) { [0]=> string(14) "Shared_Hosting" }
I was trying to get the same result in two places with this code by including it with include(""); but after the first include goes right the second one gives me the last element of the array I made, the array is this one:
<?php
$aMenu = array(
array(
"title" => "Home",
"page" => "home",
),
array(
"title" => "Over mijzelf",
"page" => "mijzelf",
),
array(
"title" => "PC Games",
"page" => "games",
),
array(
"title" => "Video's maken",
"page" => "videos",
),
array(
"title" => "Basketball",
"page" => "basketball",
),
array(
"title" => "Fitness",
"page" => "fitness"
),
array(
"title" => "Toekomst",
"page" => "toekomst"
),
);
I call it here
foreach($aMenu as $aMenu) {
$sClass = '';
if ($aMenu["page"] == $_GET['page']) {
$sClass = 'class = "active" ';
}
/*echo $aMenu["title"];
echo $aMenu["page"];*/
echo '
<ul class=" nav nav-pills nav-stacked">
<li class="'.$sClass.'" role="presentation" >'.$aMenu["title"].'</li>
</ul>';
}
You overwrite your array while iterating:
foreach($aMenu as $aMenu)
Write something like this
foreach($aMenu as $entry)
so your iteration variable won't overwrite the array itself.
I have an array structured like so:
<?php
$orderdata = array(
"service_type_web" => "on",
"custid" => "A12345",
"domain_web" => "foo.com",
"cust_email_web" => "foo#bar.com",
"plantype" => "dynamic",
"platform" => "unix",
"service_type_ssl" => "on",
"custid" => "A23456",
"common_name" => "foo.bar.com",
"cust_email_ssl" => "bar#foo.com"
);
?>
What I need to do is find the service_type_* part of the array, then echo all of the key value pairs until it reaches the next service_type_* where the loop then breaks.
Now this would be easy enough to do with a static data set but the array can change in size and each form can change depending on different variables, which is why the service_type_* key value entry is there.
Any ideas would be appreciated.
EDIT for clarification:
I basically need so that if I need, for example, the form data for web hosting, it would find the service_type_web key and return the following:
"service_type_web" => "on",
"custid" => "A12345",
"domain_web" => "foo.com",
"cust_email_web" => "foo#bar.com",
"plantype" => "dynamic",
"platform" => "unix",
...And leaves out:
"service_type_ssl" => "on",
"custid" => "A23456",
"common_name" => "foo.bar.com",
"cust_email_ssl" => "bar#foo.com"
SECOND EDIT:
OK, I'm nearly there. I've come up with the following:
$delimiter = 0;
$array_counter = 0;
foreach($orderdata as $orderkey => $ordervalue) {
if($orderkey == "service_type_web") {
$new_array = array_slice($orderdata, $array_counter);
$array_counter ++;
$delimiter = 1;
} elseif(preg_match('/service_type_[a-z\_]+/', $orderkey)) {
if($delimiter == 1) {
echo $orderkey . " => " . $ordervalue . "<br />\n";
break;
}
} else {
$array_counter ++;
}
}
However the break in the if statement isn't working, even though when I get it to echo the key/value pair for the current array pointer, it echos
"service_type_ssl" => "on"
Anyone know why? Kinda stumped.
You can check "key" before you loop into it, by using array_key_exists
$orderdata = array(
"service_type_web" => "on",
"custid" => "A12345",
"domain_web" => "foo.com",
"cust_email_web" => "foo#bar.com",
"plantype" => "dynamic",
"platform" => "unix",
"service_type_ssl" => "on",
"custid" => "A23456",
"common_name" => "foo.bar.com",
"cust_email_ssl" => "bar#foo.com"
);
if (array_key_exists('service_type_web', $orderdata )) {
echo "array found";
}
A possible solution is. This answer may guide you in right direction but still you need to do some minor changes.
$input = array(
"service_type_web" => "on",
"custid" => "A12345",
"domain_web" => "foo.com",
"cust_email_web" => "foo#bar.com",
"plantype" => "dynamic",
"platform" => "unix",
"service_type_ssl" => "on",
"custid" => "A23456",
"common_name" => "foo.bar.com",
"cust_email_ssl" => "bar#foo.com"
);
function preg_grep_keys($pattern, $input, $flags = 0) {
return array_intersect_key($input, array_flip(preg_grep($pattern, array_keys($input), $flags)));
}
$a = preg_grep_keys("/service_type_[a-z]*/", $input);
print_r($a);
expected output will be
Array ( [service_type_web] => on [service_type_ssl] => on )
taken from http://php.net/manual/en/function.preg-grep.php
I have a drop down menu on my site where I want all US states listed. I have an array of states but I can't seem to get ALL of them to load into my <option> tag. Below is the first 15 states as an array and my <select> code. Eventually the drop down will link to separate pages for each state with an image of that state (that is why I have an "img" within each state array). The only states I get to load in the drop down are: Alaska, Arkansas, Colorado, Delaware, Florida, Hawaii, and Illinois.
<?php
$states = array();
$states['AL'] = array (
"name" => "Alabama",
"img" => "http://getwidgetsfree.com/images/state_png/al.png"
);
$states['AK'] = array (
"name" => "Alaska",
"img" => "http://getwidgetsfree.com/images/state_png/ak.png"
);
$states['AZ'] = array (
"name" => "Arizona",
"img" => "http://getwidgetsfree.com/images/state_png/az.png"
);
$states['AR'] = array (
"name" => "Arkansas",
"img" => "http://getwidgetsfree.com/images/state_png/ar.png"
);
$states['CA'] = array (
"name" => "California",
"img" => "http://getwidgetsfree.com/images/state_png/ca.png"
);
$states['CO'] = array (
"name" => "Colorado",
"img" => "http://getwidgetsfree.com/images/state_png/co.png"
);
$states['CT'] = array (
"name" => "Connecticut",
"img" => "http://getwidgetsfree.com/images/state_png/ct.png"
);
$states['DE'] = array (
"name" => "Delaware",
"img" => "http://getwidgetsfree.com/images/state_png/de.png"
);
$states['DC'] = array (
"name" => "District of Columbia",
"img" => "http://pharmacoding.com/dendreon/provenge/images/dc.png"
);
$states['FL'] = array (
"name" => "Florida",
"img" => "http://getwidgetsfree.com/images/state_png/fl.png"
);
$states['GA'] = array (
"name" => "Georgia",
"img" => "http://getwidgetsfree.com/images/state_png/ga.png"
);
$states['HI'] = array (
"name" => "Hawaii",
"img" => "http://getwidgetsfree.com/images/state_png/hi.png"
);
$states['ID'] = array (
"name" => "Idaho",
"img" => "http://getwidgetsfree.com/images/state_png/id.png"
);
$states['IL'] = array (
"name" => "Illinois",
"img" => "http://getwidgetsfree.com/images/state_png/il.png"
);
$states['IN'] = array (
"name" => "Indiana",
"img" => "http://getwidgetsfree.com/images/state_png/in.png"
);
?>
<select>
<?php
foreach($states as $state) {
echo "<option value='state.php?id=" . $state["id"] . ">" . $state["name"] . "</option>";
};
?>
</select>
You are missing a closing ``' quote, next to the option tag’s closing bracket right after $state["id"].
<?php
foreach($states as $state) {
echo "<option value='state.php?id=" . $state["id"] . "'>" . $state["name"] . "</option>";
};
?>
To answer the most immediate question, you are missing a closing quote when you set <option>. Then you are setting $state["id"] when that key doesn’t exist in the structure you have. You should be using the actual key in that array.
So with that in mind, here is my re-factored version of the code. Note you can use string substitution with double quotes (") so no need for concatenation. Which makes this easier to read and debug as well.
echo '<select>';
foreach($states as $state_key => $state_value) {
echo "<option value='state.php?id=$state_key'>"
. $state_value["name"]
. "</option>"
;
};
echo '</select>';
That said, you can simplify your code by using an array like this:
$states = array();
$states['al'] = array('name' => 'Alabama');
$states['ak'] = array('name' => 'Alaska');
$states['az'] = array('name' => 'Arizona');
$states['ar'] = array('name' => 'Arkansas');
$states['ca'] = array('name' => 'California');
$states['co'] = array('name' => 'Colorado');
$states['ct'] = array('name' => 'Connecticut');
$states['de'] = array('name' => 'Delaware');
$states['de'] = array('name' => 'Delaware');
$states['dc'] = array('name' => 'District of Columbia');
$states['fl'] = array('name' => 'Florida');
$states['ga'] = array('name' => 'Georgia');
$states['hi'] = array('name' => 'Hawaii');
$states['id'] = array('name' => 'Idaho');
$states['il'] = array('name' => 'Illinois');
$states['in'] = array('name' => 'Indiana');
echo '<select>';
foreach($states as $state_key => $state_value) {
$states[$state_key] = array_merge($states[$state_key], array('img' => "http://getwidgetsfree.com/images/state_png/$state_key.png"));
$state_key_uc = strtoupper($state_key);
echo "<option value='state.php?id=$state_key_uc'>"
. $state_value['name']
. "</option>"
;
};
echo '</select>';
The goal is you are starting out with a basic array that has the lowercase state code for the array key. Then set the name as you did before. But when you do the foreach loop, then you just set the img value & merge that using array_merge into the existing array.