I can usually do pretty decent with basic arrays, but this one has put my head vs the wall.
I am trying to pass some information (for a menu) through a function, and return it in a formatted fashion.
My desired end result is to send some information like this. I need to be able to repeat the array until it is empty in the event that I have a number of fields
$Sort = array('imgup.jpg','imagedn.jpg','Name','imgx.jpg','imagy.jpg','Name4');
NewSortBox($Sort);
and have an end result that would return like
<div>Name <img src='imgup.jpg'><img src='imgdn.jpg'></div>
<div>Name4 <img src='imgx.jpg'><img src='imgy.jpg'></div>
I have figured out that I have to use the Array_Chunk function to break the array, but I am not able to figure out how to make it properly use the foreach or loop functions.
function NewSortBox(&$array){
$newArray = array_chunk($array, 3, false);
$i = 0;
foreach ($newArray as $inner_array) {
$i++;
echo "<div>";
while (list($key, $value) = each($inner_array)) {
echo "$key: $value";
// Here is where I am totally lost, I want to acheive something like ??
// echo "$value[1] <img src='$value[2]'><img src='$value[3]'>";
}
echo "</div>";
}
Something like this may help to get the desired result:
$newArray = array_chunk($Sort, 3, false);
foreach ($newArray as $inner_array) {
echo "<div>";
list($a, $b, $c) = $inner_array;
echo $c.":".$b.":".$a; //arrange the variables as required
echo "</div>";
}
Related
I've been stuck on this for like 2 days, and I know it's much more simpler than I think it is..
I have a foreach loop that goes like this.
foreach($appointments as $appointment){
$list = $appointment['techs']
}
The $appointment['techs']; comes out of the database like this.
a:2:{i:0;s:1:"1";i:1;s:2:"12";}
My question is, how to I get loop through appointments and then show the users that are assigned to each appointment...
The desired output should look like this,
{ resource : 1, event : 1},{ resource : 12, event : 1}
I've literally tried everything! Any help would be greatly appreciated.
$array = [];
$string = 'a:2:{i:0;s:1:"1";i:1;s:2:"12";}';
$arr = (unserialize($string));
foreach ($arr as $item){
array_push($array, json_encode(['resource'=> $item, 'event'=>1]));
}
$i = 0;
$numItems = count($array);
foreach ($array as $item) {
if (++$i === $numItems) {
echo $item;
}
else{
echo $item.',';
}
}
// Output: {"resource":"1","event":1},{"resource":"12","event":1}
The PHP command unserialize it's what you are looking for.
foreach($appointments as $appointment){
$list = unserialize($appointment['techs']);
}
Using below array, i'm trying to use foreach loop to iterate through each item. Then i need to apply if condition to check if the given number is even and odd. I also need to create two arrays one for even and one for odd and push each number in their respective category.
So i have done this so far:
These are the two arrays i created to push through the values to.
}
$numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];
$array_odd = [];
$array_even = [];
foreach ($numbers as $value)
{
if (value %2 == 0)
{
$array_even = $value;
echo $array_even;
}
else
{
$array_odd = $value;
echo $array_odd;
}
I'd like to know if i'm using the correct solution or are there major errors im committing?
It will surely work like charms.
$numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];
$array_odd = [];
$array_even = [];
foreach ($numbers as $value)
{
if ($value %2 == 0)
{
$array_even[] = $value;
}
else
{
$array_odd[] = $value;
}
}
echo "<pre>";
print_r($array_odd);
echo "<pre>";
print_r($array_even);
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>";
}
I want to change the value of the $key because I have array_splice inside the loop which change the position of my values so - it mess up the value I need in a specific place.
I tried $key-- but it doesn't work.
for example when I print the $key after I do echo $key it's fine but when I echo $key just after the foreach loop I get the worng value.
Any ideas?
foreach ($cut as $key => $value) {
echo "foreach key:".$key."<br>";
if(in_array($value,$operators))
{
if($value == '||')
{
echo "found || in position:".$key."<br>";
if(($key+1<sizeof($cut)))
{
$multi = new multi;
echo "<br>"."key-1: ";
print_r($cut[$key-1]);
echo"<br>";
echo "<br>"."key+1: ";
print_r($cut[$key+1]);
echo"<br>";
$res = $multi->orex($cut[$key-1],$cut[$key+1],$numString);
$cut[$key-1]= $res;
array_splice($cut,$key,1);
array_splice($cut,$key,1);
$key--; //here trying to change the key
echo "new string:";
print_r($cut);
echo "<br>";
echo "key:".$key."<br>";
}
}
}
}
Updated
I don't think it is a good idea to change the array itself inside the foreach loop. So please crete another array and fill data into it, which will be your result array. This method works well when your array data is not big, in other words, most situations.
Origin
I don't know what do you mean. Let me give it a guess...
You want:
foreach($arr as $key=>$val){
$newkey = /* what new key do you want? */
$arr[$newkey] = $arr[$key];
unset($arr[$key]);
}
below is example how mine arrays do look, i want them combine so i can output both title, votes and ratings in one line.
foreach ($items->items as $item) {
echo $item->title;
foreach ($results->resx as $res) {
echo $res->votes;
echo $res->ratings;
}
I'd like to have this, but i know this isn't right.
foreach ($items as $item) ($results as $res) {
echo $res->votes;
echo $res->ratings;
echo $item->title;
}
You can use array_merge() for that, like so:
foreach (array_merge($results, $items) as $item) {
echo isset($item->title) ? $item->title : $item->votes .'<br>'. $item->ratings;
}
UPDATE:
Changed how to print values as the objects from merged array can only have one of the two groups of properties.
UPDATE 2:
After some OP's notes that made more clear what his scenario is, and now given the assumption that both $results and $items arrays have the same number of elements, an update solution is as follows:
while ((list(, $it) = each($items)) && (list(, $rs) = each($results))) {
echo $it->title;
echo $rs->votes;
echo $rs->ratings . '<br>';
}