Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of Array's and end() 1

Status
Not open for further replies.

JamesGordon

Programmer
Jan 14, 2002
70
GB
I was using the following code:

Code:
$a = array("col1", "col2");
$txt = "SELECT ";

foreach($a in $acol) {
  $txt .= $acol;
  if($acol !== end($a)) {
    $txt .= ", ";
}

print($acol);

This worked okay, only putting a "," if the foreach wasn't at the end of the array.

I then tried to make things better by doing this:

Code:
$a = array("col1" => array("title"=>"c1title", "hidden"=>false), "col2" => array("title"=>c2title", "hidden"=>true));
$txt = "SELECT ";

foreach($a in $acol=>$value) {
  txt .= $acol;

  [COLOR=red]if() { /* This test is the problem. */[/color red]
    txt .= ", ";
  }
}

It is the final "if" test that I do not know how to code.
If I print(end($a)) then I get returned "Array".

Am I doing this right in the first place?

James.
 
What are you trying to do?

If you are trying to create a statement like "select col1, col2 ..."

Use the following for the first case:
Code:
$a = array("col1", "col2");
$txt = "SELECT ";
$txt .= implode(', ',$a);
The implode() function < will return a string consisting of the values in an array delimited by the string given in the first argument.

In the second case, you need to get an array with the keys of $a and implode() that.
Code:
$a = array("col1" => array("title"=>"c1title", "hidden"=>false), "col2" => array("title"=>c2title", "hidden"=>true));
$txt = "SELECT ";
$txt .= impode(', ',array_keys($a));

Ken
 
Just trying to learn PHP/MySQL. Many thanks. I'll not make the same mistake twice.

James.
 
Right I am now really wanting to do the second code example above. Any ideas on how to test for the end condition?
 
if you want to get rid of the last , in you're string you can also strip it like

$fieldlines=substr($fieldlines,0,strlen($fieldlines)-1); // get rid of the last ,

I copied this from code I use to build op an select statement from an array, $fieldlines is in you're case $text

 
What do you want your output to look like?

Here's one possible option:
Code:
<?
$a = array("col1" => array("title"=>"c1title", "hidden"=>'false'), "col2" => array("title"=>"c2title", "hidden"=>'true'));
$set = array();
foreach ($a as $key=>$ary) 
   foreach ($ary as $subkey => $val)
      $set[] = $key . '.' . $subkey . "='" . $val ."'";
echo 'SELECT ' . implode(', ',array_keys($a)) . '<br>';
echo 'UPDATE xyx set ' . implode(', ',$set);
?>

Ken
 
What I wanted was to do something to each array key, with the exception of the last value e.g. produce "col1, col2".

I found a solution by using

Code:
$a = array("col1" => array("title"=>"c1title", "hidden"=>false), "col2" => array("title"=>c2title", "hidden"=>true));
$txt = "SELECT ";

foreach($a in $acol=>$value) {
  txt .= $acol;

[COLOR=red]  if(current($a) != end($a)) {[/color]
    txt .= ", ";
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top