Forum Moderators: coopster

Message Too Old, No Replies

Value of loop

         

Champak

8:29 am on Jan 24, 2007 (gmt 0)

10+ Year Member



I would like to capture the end value of a loop but don't know how.
Ex.
This is the main part of the code, it essentially puts a comma between each word if it exists in the array:

<?
$my_array = array("a" => "Cat", "b" => "Dog", "c" => "Horse");
extract($my_array);
$string = "$a $b $c";

$tok = strtok($string, " \n\t");

while ($tok!== false) {
$ratter = "$tok, ";
echo $ratter ;
$tok = strtok(" \n\t");
}
?>

I basically want to make the following, notice the "$GRABVALUE", but of course it isn't going to work. The reason being, I need to include the total value in an area that will not allow php script but I can put ". GRABVALUE . " in:

<?php
$my_array = array("a" => "Cat", "b" => "Dog", "c" => "Horse");
extract($my_array);
$string = "$a $b $c";

$tok = strtok($string, " \n\t");

$GRABVALUE = (while ($tok!== false) {
$ratter = "$tok, ";
echo $ratter ;
$tok = strtok(" \n\t");

})
?>

Please help, I've been trying for the last 4 hours.

[edited by: Champak at 8:30 am (utc) on Jan. 24, 2007]

cameraman

8:55 am on Jan 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, this is some interesting script. To answer your question directly, you'll need to put a counter in the while loop:
$i = 0;
while ($tok!== false) {
$i++;

If you have an array like $my_array:
$string=implode(",",$my_array);

would produce "Cat,Dog,Horse". The first parameter can be more than one character, e.g. implode(",<br>\n",$my_array) would produce:
Cat,<br>
Dog,<br>
Horse

strtok takes two parameters, the string to tokenize and the tokens.

mcibor

10:45 am on Jan 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you need a value, then yes, use the cameraman's solution with $i++

if you need the last text snippet, then you have it in
$ratter outside the loop
or without the last ", ":
$last = rtrim [php.net]($ratter, ", ");

or you can do the same by
$string=implode [php.net](", ",$my_array); //the space you can also insert
echo $string

last value of an array can be get by
$last = end [php.net]($my_array);

Hope this helps

Champak

4:11 pm on Jan 24, 2007 (gmt 0)

10+ Year Member



Thank both of you. I tried the implode thing before, I didn't use it because when one of the array values were blank/empty, the comma would still display. I need the comma to display only if there is a need for it to display...if there is something for it to come after. Anyway, I have one last problem, the echo is displaying a count instead of what is actually in the array. Maybe I did something wrong, this is what I have:

<?php
$my_array = array("a" => "Cat", "b" => "", "c" => "Horse");
extract($my_array);
$string = "$a $b $c";

$tok = strtok($string, " \n\t");

$i = 0;
while ($tok!== false) {
$ratter = "$tok, ";
echo $ratter ;
$tok = strtok(" \n\t");
$i++;
}
$echo $i;
?>

How do I fix that? One last thing if it is possible, delete the last comma on the whole thing so no comma comes after the very last value in the array.

[edited by: Champak at 4:12 pm (utc) on Jan. 24, 2007]

cameraman

7:08 pm on Jan 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We must be misunderstanding what you wanted - you said 'include the total value' which I took to mean a count - that's what $i is, so if you echo $i, that's what you'll get - the count. To remove the trailing comma, use mcibor's rtrim($ratter,", ").

pinterface

8:35 pm on Jan 24, 2007 (gmt 0)

10+ Year Member



If I am understanding you correctly, you want to turn
array("a" => "Cat", "b" => "Dog", "c" => "Horse", "d" => "")
into
"Cat, Dog, Horse"
, yes?

Let's begin by familiarizing ourselves with a couple of functions:

  • array_values [php.net] -- extracts just the values of an array
  • array_filter [php.net] -- removes all the elements of an array which meet a certain condition
  • join [php.net]/implode [php.net] -- joins bits of an array together using some string

By combining them together, we get our desired result:

    join(", ", array_filter(array_values(["a" => "Cat", "b" => "Dog", "c" => "Horse", "d" => ""])))

      ==>
      "Cat, Dog, Horse"

What's happening when we do this? Let's find out, step by step:

  1. array_values(["a" => "Cat", "b" => "Dog", "c" => "Horse", "d" => ""])

      ==>
      ["Cat", "Dog", "Horse", ""]

  2. array_filter(["Cat", "Dog", "Horse", ""])

      ==>
      ["Cat", "Dog", "Horse"]

  3. join(", ", ["Cat", "Dog", "Horse"])

      ==>
      "Cat, Dog, Horse"

(the basic technique falls under the heading of functional programming [en.wikipedia.org] which, though often sadly cumbersome in PHP, is still something every programmer ought to know)

Champak

9:31 pm on Jan 24, 2007 (gmt 0)

10+ Year Member



cameraman, you are absolutely right, my terminology always gets twisted with this stuff, what I meant was value of the returned array....if that makes any sense or I might just be off with my terminology again:).

pinterface, that is EXACTLY what I needed, and so much easier. THANK YOU! Funny thing, I was actually messing with each of those functions on their own, but wasn't getting the desired results on either fronts, and never thought about joining them together.

coopster

10:11 pm on Jan 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No need for the array_values() though as implode() works on the elements of the array anyway ...
$string = '"' . implode(', ', array_filter($array)) . '"';

mcibor

11:13 am on Jan 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And to get the last existent value you can use:

$last_item = end(array_filter($my_array));