how to get get_theme_mod value into array in wordpress? - php

I want to make an array by theme_mod value in wp theme customizer.
for example in this code:
$arr = array(com, net, org);
foreach ($arr as &$value) {
echo "<div id='domain-$value'></div>";
}
I have an array like above with some tlds and want to set it dynamically by wp mcomstomizer.
I had a try by put "get_theme_mod( 'tlds' );" (like below) in array but no success and it put all the value in just one array key.
$arr = array(get_theme_mod( 'tlds' ););
foreach ($arr as &$value) {
echo "<div id='domain-$value'></div>";
}
How to put values into array and separate every array by comma,?

get_them_mods() should return an array that you can loop over.
https://codex.wordpress.org/Function_Reference/get_theme_mods
You can use array_values()
Example:
$arr = get_them_mods();
$temp = array_values($arr);
foreach ($temp as $value) {
echo "<div id='domain-$value'></div>";
}

I found the soloution by explode. Thank You
$tlds = get_theme_mod( 'tlds' );
$arr = explode(', ', $tlds);
foreach ($arr as &$value) {
echo "<div id='domain-$value' class='col-md-4 col-xs-6 col-xxs-12 otherdomains'></div>";
}

Related

Getting formatted result with nested arrays in PHP

I am trying to generate sql statement dynamically with loops but i am not getting any idea on how to do it with html name array.
assuming $name and $message are post arrays ... and assuming length of both will be equal,
following is a method i tried;
<?php
$name = array('name1','name2' );
$mess= array('message1','message2' );
$values = array($name,$mess);
foreach ($values as $key){
foreach ($key as $value){
echo $value.",";
}
}
?>
output is =
name1,name2,message1,message2,
but i want output as =
(name1,message1),(name2,message2),
Edit : I have acess to $values only and I will not be able to determine how many values are going to join in $values ..
like it can be
$values=array($name,$message,$phone);
and the result i want will be
(name1,message1,phone1),(name2,message2,phone2)
My solutions is
Step 1: Loop your $values this will gonna loop every index of your array like $name
foreach($name as $index => $value) {
// do something
}
Step 2: Inside your loop values start with ( which mean wrap your detail in (
echo "(";
Step 3: loop parent array
foreach($values as $key => $arr) {
// do something
}
Step 4: Inside the loop of your parent array display each data according to your index
echo $values[$key][$index];
$key represents the index of your parent array while $index represents the index of your child array like $name
this loop will create data like
(name1,message1,phone1)
and I just add this
if ($key < sizeof(array_keys($values))-1) {
echo ",";
}
to avoid adding , after the last loop
This code will dynamically display array you put inside $values
So your code would be like this
$name = array('name1','name2','name3','name4' );
$mess= array('message1','message2','message3','message4' );
$phone= array('phone1','phone2','phone3','phone4' );
$married= array('yes','no','yes','yes' );
$values = array($name,$mess,$phone);
foreach($name as $index => $value) {
echo "(";
foreach($values as $key => $arr) {
echo $values[$key][$index];
if ($key < sizeof(array_keys($values))-1) {
echo ",";
}
}
echo "),";
}
Demo
or this
$name = array('name1','name2','name3','name4' );
$mess= array('message1','message2','message3','message4' );
$phone= array('phone1','phone2','phone3','phone4' );
$values = array($name,$mess,$phone);
foreach($name as $index => $value) {
$join = array();
foreach($values as $key => $arr) {
$join[] = $values[$key][$index];
}
echo "(".implode(",",$join)."),";
}
Demo
$name = array('name1','name2' );
$mess = array('message1','message2' );
foreach ($names as $k => $v){
echo "(".$v.",".$mess[$k]."),";
}
Try this, you not need two foreach loop, only use foreach loop and pass key to other array and get value
$name = array('name1','name2' );
$mess= array('message1','message2' );
$values = array($name,$mess);
foreach ($name as $keys => $vals)
{
echo "(".$vals.",".$mess[$keys]."),";
}
DEMO
You can use arrap_map() function in order to join two array. Here is reference http://php.net/manual/en/function.array-map.php
<?php
$name = array('name1','name2' );
$mess= array('message1','message2' );
$value = array_map(null, $name, $mess);
print_r($value);
?>

Loop through a two-dimensional array

I have an array that looks like this:
$array = array(
array(
"http://google.com",
"Google"
),
array(
"http://yahoo.com",
"Yahoo"
)
);
What is the simplest way to loop through it. Something like:
foreach ($array as $arr) {
// help
}
EDIT: How do I target keys, for example, I want to do:
foreach ($array as $arr) {
echo '<a href" $key1 ">';
echo ' $key2 </a>';
}
In order to echo out the bits you have to select their index in each array -
foreach($array as $arr){
echo ''.$arr[1].'';
}
Here is an example.
Use nested foreach() because it is 2D array. Example here
foreach($array as $key=>$val){
// Here $val is also array like ["Hello World 1 A","Hello World 1 B"], and so on
// And $key is index of $array array (ie,. 0, 1, ....)
foreach($val as $k=>$v){
// $v is string. "Hello World 1 A", "Hello World 1 B", ......
// And $k is $val array index (0, 1, ....)
echo $v . '<br />';
}
}
In first foreach() $val is also an array. So a nested foreach() is used. In second foreach() $v is string.
Updated according to your demand
foreach($array as $val){
echo ''.$val[1].'';
}
The easiest way to loop through it is:
foreach ($array as $arr) {
foreach ($arr as $index=>$value) {
echo $value;
}
}
EDIT:
If you know that your array will have always only two indexes then you can try this:
foreach ($array as $arr) {
echo "<a href='$arr[0]'>$arr[1]</a>";
}
First modify your variable like this:
$array = array(
array("url"=>"http://google.com",
"name"=>"Google"
),
array("url"=>"http://yahoo.com",
"name"=>"Yahoo"
));
then you can loop like this:
foreach ($array as $value)
{
echo '<a href='.$value["url"].'>'.$value["name"].'</a>'
}
The way to loop through is,
foreach($array as $arr)
foreach($arr as $string) {
//perform any action using $string
}
Use the first foreach loop without { } for the simplest use.
That can be the most simple method to use a nested array as per your request.
For your edited question.
Wrong declaration of array for using key.
$array = array(
"http://google.com" => "Google",
"http://yahoo.com" => "Yahoo" );
And then, use the following.
foreach ($array as $key => $value)
echo "<a href='{$key}'>{$value}</a>";
This doesn't slow down your server's performance.
In modern versions of php, you can assign (destructure) subarray values to variables of your choosing.
A good tutorial: https://stitcher.io/blog/array-destructuring-with-list-in-php
Code: (Demo)
$array = [
["http://example1.com", "Example 1"],
["http://example2.com", "Example 2"]
];
foreach ($array as [$key1, $key2]) {
echo "$key2\n";
}
Output:
Example 1
Example 2
To be honest, I'd probably name the variables $url and $text respectively.

How to echo out the values of this array?

How to echo out the values individually of this array?
Array ( [0] => 20120514 [1] => My Event 3 )
so
echo $value[0]; etc
I have this so far:
foreach (json_decode($json_data_string, true) as $item) {
$eventDate = trim($item['date']);
// positive limit
$myarray = (explode(',', $eventDate, 2));
foreach ($myarray as $value) {
echo $value;
}
This echo's out the whole string no as an array. and if i do this?
echo $value[0};
Then I only get 2 characters of it??
The print_r :
Array ( [0] => 20120430 [1] => My Event 1 )
foreach ($array as $key => $val) {
echo $val;
}
Here is a simple routine for an array of primitive elements:
for ($i = 0; $i < count($mySimpleArray); $i++)
{
echo $mySimpleArray[$i] . "\n";
}
you need the set key and value in foreach loop for that:
foreach($item AS $key -> $value) {
echo $value;
}
this should do the trick :)
The problem here is in your explode statement
//$item['date'] presumably = 20120514. Do a print of this
$eventDate = trim($item['date']);
//This explodes on , but there is no , in $eventDate
//You also have a limit of 2 set in the below explode statement
$myarray = (explode(',', $eventDate, 2));
//$myarray is currently = to '20'
foreach ($myarray as $value) {
//Now you are iterating through a string
echo $value;
}
Try changing your initial $item['date'] to be 2012,04,30 if that's what you're trying to do. Otherwise I'm not entirely sure what you're trying to print.
var_dump($value)
it solved my problem, hope yours too.

Basic implode foreach

I have the following code of which I want to echo array elements separated by commas. The code outputs the disered list, but without commas. What am I missing?
<?php
$array = get_field('casts');
$elements = $array;
foreach($array as $key => $value) {
echo implode(', ', $value)};
?>
EDIT 1: where $elements are nested arrays.
EDIT 2: Working snippet:
<?php
$array = get_field('casts');
$new_array = array();
foreach($array as $sub_array) {
foreach($sub_array as $value) {
array_push($new_array, $value);
}
}
echo implode(", ", $new_array);
?>
Why are you assigning $elements = $array; and then never using $elements?
Also you don't need to loop (foreach) to implode an array.
Try this:
<?php
$array = get_field('casts');
$new_array = array();
foreach($array as $sub_array) {
foreach($sub_array as $value) {
// this array_push() function adds $value to the end of $new_array.
array_push($new_array, $value);
}
}
echo implode(", ", $new_array);
?>
Here is the documentation on implode()
You can play around and test the above code here.
Also next time, add the tag php, otherwise our codes won't get color syntax.

Copy values into another array

I have the following code:
foreach ($row as $item) {
foreach($item as $key) {
echo "<pre>";
print_r($key);
echo "</pre>";
}
}
I am trying to copy the keys ($key) into another array for further processing. How can i do this?
define some variable as array $array = array(); and just push the keys in with array_push($array, $key);
$array = array();
foreach ($row as $item) {
foreach($item as $key) {
array_push($array, $key);
}
}
$aNew = array();
foreach($row as $item) {
foreach($item as $key) {
$aNew[] = $key;
}
}
But; why would you do this? You can also just perform your commands / processing inside the second foreach().
If you want to get all keys of an array you may use
array_keys()
instead. Also, if each of your rows has the same keys in your second foreach loop, you may break both of the loops after getting all the keys from the first row.
Just use array_keys()
$a = array();
$array_of_keys = array_keys( $a );

Categories