I have the following code:
query_posts( array(
"tax_query" => array(
array(
"taxonomy" => "country",
"field" => "slug",
"terms" => array( "usa", "canada" )
)
)
) );
I also have a loop above to identify all of the "terms" like this:
$term_list = get_the_terms($post->ID,$taxonomy);
foreach($term_list as $term_single) {
echo $term_single->slug; //do something here
}
I'd like to dynamically replace "usa" and "canada" with the results from my foreach loop. How can I do that?
You should do something like that
function getTermsForQuery($post) {
$result = [];
$term_list = get_the_terms($post->ID,$taxonomy);
foreach($term_list as $term_single) {
$result[] = $term_single->slug; //do something here
}
return $result;
}
query_posts( array(
"tax_query" => array(
array(
"taxonomy" => "country",
"field" => "slug",
"terms" => getTermsForQuery($post)
)
)
) );
Without foreach Loop
$term_list = get_the_terms($post->ID,$taxonomy);
query_posts( array(
"tax_query" => array(
array(
"taxonomy" => "country",
"field" => "slug",
"terms" => $term_list
)
)
) );
With foreach loop
$term_list = get_the_terms($post->ID,$taxonomy);
foreach($term_list as $term_single) {
$cat_terms[] = $term_single;
}
query_posts( array(
"tax_query" => array(
array(
"taxonomy" => "country",
"field" => "slug",
"terms" => $cat_terms
)
)
) );
I have create 3 arrays then fill each one and insert to another.
//for loop, store the values that you want
$subArray2 = array();
for($row = 0;$row < 3;$row++){
$subArray2[$row] = "Value_$row";
}
$subArray1 = array("taxonomy" => "country","field" => "slug","terms" => $subArray2);
$arr = array("tax_query" => $subArray1);
//var_dump($arr);
foreach($arr as $val){
foreach($val as $val1){
if(is_array($val1)){
foreach($val1 as $val2){
echo "$val2<br>";
}
}else{
echo $val1 . "<br>";
}
}
}
Related
I have this array in PHP:
array(
"name" => "Cherry",
"desc" => "I'm a cherry.",
"keys" => array(
"Africa",
"India",
"America"
),
"pict" => "image1.png",
)
And I want to loop into this and between keys to display to countries/continents.
This is what I have already tried:
foreach($details as $detail) {
foreach($keys as $key) {
echo $key;
}
}
In case if you want to get other values in array along with countries:
<?php
$details = array(
"name" => "Cherry",
"desc" => "I'm a cherry.",
"keys" => array(
"Africa",
"India",
"America"
),
"pict" => "image1.png",
);
foreach( $details as $key => $detail ) {
if( is_array( $detail ) && $key == 'keys' ) {
foreach( $detail as $country ) {
echo $country;
}
} else {
echo $detail;
}
}
?>
I am creating bar chart , i am unable to get output and generate chart.
here is my code
if ($sql) {
$arrData = array(
"chart" => array(
"caption" => "Status stastics",
"showValues" => "0",
"theme" => "zune"
)
);
$arrData["data"] = array();
foreach($sql as $row) {
array_push($arrData["data"], array(
"label" => $row["name"],
"value" => $row["value"]
)
var_dump($row["value"]);
);
}
}
I am not getting any output for ( var_dump($row["value"]);)
am i going in a right way?
Can any one help me in this.
Got my answer
if ($sql) {
$arrData = array(
"chart" => array(
"caption" => "Status stastics",
"showValues" => "0",
"theme" => "zune"
)
);
$arrData["data"] = array();
foreach($sql as $row)
{
$object_array =(array)$row;
array_push($arrData["data"], array(
"label" => $object_array["name"],
"value" => $object_array["value"]
)
);
}
}
?>
I wish to create a list of items from an array, grouped by a value within that array.
Take this array:
$people = array(
0 => array(
"Forename" => "Jim",
"Surname" => "Smith"
),
1 => array(
"Forename" => "Mike",
"Surname" => "Johnson"
),
2 => array(
"Forename" => "Kim",
"Surname" => "Smith"
),
3 => array(
"Forename" => "Paul",
"Surname" => "Jones"
)
);
Specifically I'd like to run a foreach on $people, grouping them by unique surname. i.e. the desired output would be:
<select>
<optgroup label="Smith">
<option>Jim</option>
<option>Kim</option>
</optgroup>
<optgroup label="Johnson">
<option>Mike</option>
</optgroup>
<optgroup label="Jones">
<option>Paul</option>
</optgroup>
</select>
I'm struggling to come up with anything vaguely efficient and the Google gods aren't watching over me today :( What's the best approach for such a use-case in PHP?
$surnames = array();
foreach($people as $person) {
$surnames[$person['surname']][] = $person;
}
This code stores all persons in an array grouped by their surnames.
The resulting array:
array(
'smith' => array(
0 => array(
"Forename" => "Jim",
"Surname" => "Smith"
),
1 => array(
"Forename" => "Kim",
"Surname" => "Smith"
)
),
'jones' => array(
0 => array(
"Forename" => "Paul",
"Surname" => "Jones"
)
)
)
I would do this way:
$grouped = array();
foreach ($people as $p){
if (!array_key_exist($p["Surname"], $grouped)){
$grouped[$p["Surname"]] = array();
}
$grouped[$p["Surname"]][] = $p;
}
I've added one more duplicate person:
....
4 => array
(
"Forename" => "Kim",
"Surname" => "Smith"
)
);
this is how you filter array :
$uniqueNames = array();
foreach($people as $person)
{
$uniqueNames[$person['Surname']][] = $person['Forename'];
}
if however you also need Forename to be unique, you can do it like this:
$uniqueNames = array_map
(
function($arrayItem)
{
if (is_array($arrayItem))
{
return array_unique($arrayItem);
}
}
, $uniqueNames
);
Also I've made some easy functions to generate html code:
function htmlSelect($name, $optionsData, $selectedItem = null)
{
$str = "\n<select name='$name' id='select-$name'>";
foreach ($optionsData as $k => $value_s)
{
if(is_array($value_s))
{
$str .= htmlOptgroup($k, $value_s);
}
else
{
$selected = ($selectedItem && $selectedItem == $k);
$str .= "\n\t".htmlOption($value_s, $k, $selected);
}
}
$str .= "\n</select>";
return $str;
}
function htmlOptgroup($label, $optionsData, $selectedItem = null)
{
$str = "\n\t<optgroup label='$label'>";
foreach ($optionsData as $k => $value)
{
$selected = ($selectedItem && $selectedItem == $k);
$str .= "\n\t\t".htmlOption($value, $k, $selected);
}
$str .= "\n\t</optgroup>";
return $str;
}
function htmlOption($display, $value, $selected = false)
{
$selectedStr = $selected ? " selected='selected'" : "" ;
return "<option$selectedStr value='$value'>$display</option>";
}
these functions can easily move to static class for html.
finally you call:
echo htmlSelect('unique-surnames', $uniqueNames);
I swear when I started there was no any answers :d
I have this following array
$question = array(
"ques_15" => array(
"name" => array(
"0" => "aaa"
)
),
"ques_16" => array(
"name" => array(
"0" => "bbb",
"1" => "ccc"
)
)
);
$i=0;
foreach($question as $k=>$v)
{
echo $question[$k]['name'][$i];
$i++;
}
But my output is only
aaaccc
I am missing the value bbb
You need to iterate the inner 'name' arrays - you could use a nested foreach loop:
$question = array(
"ques_15" => array(
"name" => array(
"0" => "aaa"
)
),
"ques_16" => array(
"name" => array(
"0" => "bbb",
"1" => "ccc"
)
)
);
foreach($question as $quest)
{
foreach($quest['name'] as $val)
{
echo $val;
}
}
you should loop though like so
foreach($question as $q)
{
foreach($q['name'] as $v)
{
echo $v;
}
}
in foreach you don't need a counter $i, it's for while() or for()
you array is two dimensional so you need 2 foreach
Check it out in a functional way.
The shorthand array declaration works only on PHP 5.4+ though, but it still works with your longhand array declaration.
$questions = [
'ques_15' => ['name' => ['aaa']],
'ques_16' => ['name' => ['bbb', 'ccc']]
];
array_map(function($a){
foreach ($a['name'] as $v) echo $v;
}, $questions);
How can I edit this foreach loop so that I will be able to use strpos to look if q is found in the label ?
The result array will contain those values.
$q may be anna or ann or reas john
<?php
$q = $_GET["q"];
if (!$q) return;
$data = Array(
Array(
'label' => 'anna c13',
'category' => 'Products'
),
Array(
'label' => 'anders andersson',
'category' => 'People'
),
Array(
'label' => 'andreas johnson',
'category' => 'People'
)
);
$result = array();
foreach ($data as $value) {
array_push($result, array(
"label" => $value["label"],
"category" => $value["category"]
));
}
$json = json_encode($result);
echo $json;
?>
This will output every array in $data where $q is somewhere in 'label'.
<?php
if( !isset( $_GET["q"] )) return;
$q = $_GET["q"];
$data = Array(
Array(
'label' => 'anna c13',
'category' => 'Products'
),
Array(
'label' => 'anders andersson',
'category' => 'People'
),
Array(
'label' => 'andreas johnson',
'category' => 'People'
)
);
$result = array();
foreach ($data as $value) {
if( strpos( $value['label'], $q ) !== false ) {
$result[] = $value;
}
}
$json = json_encode($result);
echo $json;
?>
You haven't defined keys for your $data array - so it automatically take the form of:
array(
0=>array(...),
1=>array(...),
2=>array(...)
)
This means that you're using strtolower on an int - so that's probably why it's failing.
foreach ($data as $value) {
if(strpos($value['label'], $q) !== false){
$result[] = $value;
}
}