How to get a value separated with comma. I used explode for it. This is what I've tried.
$get_item_desc = get_post_meta ($post->ID, 'item_description', true);
$arr = explode(",", $get_item_desc);
$item_string = '';
foreach($arr as $val){
echo $get_items = $item_string.trim($val).' ';
}
Sample Input:
Item 1, Item 2, Item 3
Sample Output:
DESC-1: Item 1
DESC-2: Item 2
DESC-3: Item 3
Based on your sample input/output, you can use:
$get_item_desc = get_post_meta($post->ID, 'item_description', true);
$arr = explode(',', $get_item_desc);
$i = 1;
foreach($arr as $val){
echo 'DESC-' . $i . ': ' . trim($val) . ' ';
$i++;
}
Related
Could somebody explaind me ***why array count is not 2
$value = '"305", "112", ';
//remove last comma and space
echo $value = rtrim($value, ', ');
echo '<br>';
$value = array($value);
echo $array_length = count($value); //***
You should use explode function to get array like the following code :
$value = '"305", "112"';
$value = rtrim($value, ', ');
echo '<br>';
$value = explode(',',$value);
echo $array_length = count($value);
you can use explode() to get it.
$value = '"305", "112", ';
//remove last comma and space
echo $value = rtrim($value, ', ');
echo '<br>';
$value = explode(',',$value);
echo $array_length = count($value);
you can also do this if you want to create an array from that these type of strings and do array count.
$arr = array_filter(array_map('trim', str_getcsv($value)));
print_r($arr);
echo count($arr);
when you are creating an array with array($value), the entire value of $value counted as a single element of an array. Thats why you are getting count($value) not equal to 2.
can I ask for your help? I have values in the array and I want to combine them to become one value. I want a result that 'gendesc' 'dmdnost' 'stredesc' 'formdesc' 'rtedesc' will be in one column. Thanks.
foreach($data as $item){
$tmp = array();
$tmp_item = (array) $item;
$tmp['description'] = $tmp_item['gendesc'];
$tmp['m1'] = $tmp_item['dmdnost'];
$tmp['m2'] = $tmp_item['stredesc'];
$tmp['m3'] = $tmp_item['formdesc'];
$tmp['m4'] = $tmp_item['rtedesc'];
$final_data [] = $tmp;
}
print_r($final_data);
Can you try
foreach($data as $item){
$tmp_item = (array) $item;
$tmp = $tmp_item['gendesc']
. ' ' . $tmp_item['dmdnost']
. ' ' . $tmp_item['stredesc']
. ' ' . $tmp_item['formdesc']
. ' ' . $tmp_item['rtedesc'];
$final_data[] = array('description' => $tmp);
}
print_r($final_data);
You want this?
foreach($data as $item){
$tmp = array();
// ver.1
$tmp['description'] = $item['gendesc']
. $item['dmdnost']
. $item['stredesc']
. $item['formdesc']
. $item['rtedesc'];
// ver.2 also you can use one-line shortcut with implode instead of ver.1
$tmp['description'] = trim(implode(" , ", $item));
$final_data[] = $tmp;
}
print_r($final_data);
Try this,
$i =0;
foreach($data as $item)
{
$tmp = [];
$tmp[$i]['description'] = $item['gendesc'];
$tmp[$i]['m1'] = $item['dmdnost'];
$tmp[$i]['m2'] = $item['stredesc'];
$tmp[$i]['m3'] = $item['formdesc'];
$tmp[$i]['m4'] = $item['rtedesc'];
$i++;
}
print_r($tmp);
My tags column is like this:
first row: sky - earth - sea
second row: iron - silver - gold
third row: apple - fruit - food
...and so on
Want to create a div from each item, like this:
<div class='tagdown'>sky</div>
<div class='tagdown'>earth</div>
$st = $db->query("select tags from posts");
$arr = array();
$items = "";
while ($row = $st->fetch()) {
array_push($arr, explode(' - ', $row['tags']));
}
foreach($arr as $item) {
$items .= "<div class='tagdown'>" . $item . "</div>\n";
}
echo $items;
Notice: Array to string conversion...
Another Try:
for ($i = 0; $i < count($arr); ++$i) {
$items .= "<div class='tagdown'>" . $arr[$i] . "</div>\n";
}
echo $items;
Notice: Array to string conversion...
Any help?
Dont push and again traverse your array. just print out data in while loop. Try following code:
$items = "";
while ($row = $st->fetch()) {
$arr = explode(' - ', $row['tags']);
$items .= "<div class='tagdown'>".implode("</div>\n<div class='tagdown'>",$arr)."</div>\n";
}
echo $items;
Try like shown below
Example :
<?php
$items = "";
$str = "sky-earth-sea";
$arr = explode("-", $str);
$count = count($arr);
for($i = 0; $i < $count; $i++) {
$items .= "<div class='tagdown'>".$arr[$i]."</div></br>";
}
echo $items;
?>
explode() returns an array and you are pushing an array into an other array
its making 1 2D array you can check thar using print_r($arr);
use this
while ($row = $st->fetch()) {
$tag=explode('-', $row['tags'];
foreach($tag as $t){
array_push($arr,$t ));
}
}
You can also use fetch associative if using mysqli_connect
while ($row = $result->fetch_assoc()) {
array_push($arr, explode(' - ', $row['tags']));
}
foreach($arr as $a) {
foreach($a as $v){
$items .= "<div class='tagdown'>" . $v . "</div>\n";
}
}
echo $items;
-------------- OR -------------
$arr = array();
$items = "";
while ($row = $result->fetch_assoc()) {
$tag = explode(' - ', $row['tags']);
foreach($tag as $v){
$items .= "<div class='tagdown'>" . $v . "</div>\n";
}
}
echo $items;
Say I have this array
$array = array('pen' => 'blue', 'paper' => 'red', 'ink' => 'white');
When I loop through it
$string = '';
foreach ($array AS $key=>$value) {
$string .= $key . ' = ' . $value;
}
I want to get the "line number" of the element the loop is currently on.
If the loop is on "pen" I would get 1.
If the loop is on "paper" I would get 2.
If the loop is on "ink" I would get 3.
Is there an array command for this?
No. You will have to increment an index counter manually:
$string = '';
$index = 0;
foreach ($array as $key=>$value) {
$string .= ++$index . ") ". $key . ' = ' . $value;
}
Use array_values() function to extract values from array. It indexes array numerically and $key will be the index of value in loop.
$array = array('pen' => 'blue', 'paper' => 'red', 'ink' => 'white');
$array = array_values($array);
$string = '';
foreach ($array as $key => $value) {
$string .= $key + 1 . ' = ' . $value;
}
$i = 0;
foreach ($array as $key=>$value) { // For each element of the array
print("Current non-associative index: ".$i."<br />\n"); // Output the current index
$i++; // Increment $i by 1
}
Hope that helps.
This might actually be a css question but I'm hoping not because I'd like this to work in IE.
I have the following loop:
<?php
if ($category)
{
foreach($category as $item)
{
echo $item['name'];
echo ", ";
}
} ?>
Which should output
item, item, item, item,
The only thing is...I'd like to NOT have a comma after the last item. Is there any way to do this within a loop?
Well to keep your code how it is, you could add a counter, and skip the last one.
<?php
if ($category) {
$counter = 0;
foreach($category as $item)
{
$counter++;
echo $item['name'];
if ($counter < count($category)) {
echo ", ";
}
}
}
?>
Or you can do it much, much, quicker:
<?php echo implode(", ", array_map(create_function('$item', 'return $item["name"];'), $category)); ?>
Don't echo immediately but save your output into a variable that you can trim.
<?php
if ($category) {
$output = '';
foreach($category as $item) {
$output .= $item['name'];
$output .= ", ";
}
echo rtrim($output, ', ');
}
?>
The implode solution is the simplest, but you asked for a loop. This method avoids putting an extra conditional in the loop, and therefore should be somewhat more efficient. Basically, instead of doing something different for the last item, you do something different for the first item.
$myArray = array(); //Fill with whatever
$result = $myArray[0];
for ($idx = 1; $idx < count($myArray); $idx += 1)
{
$result .= ', ' . $myArray[$idx];
}
EDIT: After realizing you want $item['name'] instead of just $item:
$myArray = array(); //Fill with whatever
$result = $myArray[0]['name'];
for ($idx = 1; $idx < count($myArray); $idx += 1)
{
$result .= ', ' . $myArray[$idx]['name'];
}
As lovely as foreach is,...
<?php
if ($category) {
$count = count($category) - 1;
for ($i = 0; $i <= $count; $i++) {
echo $category[$i]['name'];
if ($i < $count)
echo ', ';
}
}
?>
...for is sometimes necessary.
Assuming $category is an array, you can use implode to get what you want:
Edit: Missed the $categories['name'] part, this should work:
<?php implode(", ", array_keys($category, 'name')); ?>
The standard solution to the "last comma" problem is to put items into an array and then implode it:
$temp = array();
foreach($category as $item)
$temp[] = $item['name'];
echo implode(', ', $temp);
If you want this more generic, you can also write a function that picks ("plucks") a specific field out of each subarray:
function array_pluck($ary, $key) {
$r = array();
foreach($ary as $item)
$r[] = $item[$key];
return $r;
}
and then just
echo implode(', ', array_pluck($category, 'name'));
Or you could check for the last key:
end($category);
$lastkey = key($category);
foreach ( $category AS $key => $item ) {
echo $item['name'];
if ( $lastkey != $key ) {
echo ', ';
}
}
And another option:
<?php
$out = "";
foreach ($category as $item)
{
$out .= $item['name']. ", ";
}
$out = preg_replace("/(.*), $/", "$1", $out);
echo $out;
?>