0

I don't like to use the foreach for every little array, I hear'd it is also possible to replace it with some array_ function, but I forget the function name.

This is what I wan't to reach:

$down = array (
    "file.rar" => $l->get()->fileDesc(),
    "file1.rar" => $l->get()->clientDesc()
);

normally, I'm using this to display the data:

foreach ( $down as $key => $value )
     $data .= $key . ' = ' . $value . '<br/>';

So it'll return:

//echo $data;
    file.rar = File Description One
    file1.rar = Client Description

It there a way to prevent from using the foreach and display the same $data anyway?

I'm just curious, so please be nice.

4
  • 3
    Why wouldn't you use a foreach? You can struggle with array_keys, but a foreach is the easiest thing you can do. Commented Jan 21, 2012 at 18:31
  • 1
    I'm not sure what you're asking that you haven't answered in your own post. If you simply want to see what's in an array for debug purposes use what @tandu suggested. Commented Jan 21, 2012 at 18:32
  • @rdlowrey Did I said anywhere its for debuging purposes? I didnt, because, I don't want to use foreach but I want to use _some_function_ I've seen that function before, but I forgot its name, unfotunately. Commented Jan 21, 2012 at 18:34
  • @Cyclone Then I suggest Reading The Fine Manual entry on array functions over at php headquarters instead of saying, "hey, how about you guys tell me so I don't have to use a search feature." Commented Jan 21, 2012 at 18:44

3 Answers 3

1

Why don't you just write your own function?

function print_array($down) {
   $data = '';
   foreach ( $down as $key => $value )
      $data .= $key . ' = ' . $value . '<br/>';
   return $data;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I don't know why you accepted this answer... It use a foreachloop so it's exactly the same as your code...
0

You can use print_r with true on the second parameter, but I would suggest to use a foreach (as I say in my comment)

<?php
  $data = print_r($down, true);
?>

1 Comment

wrap it with <pre> and </pre> to see the result in a prettier format
0

well the only time you would need to use a foreach is for a dynamic array. if thats not the case, just use isset to find out what is set. you can also use count if your just trying to figure out how big an array is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.