Jump to content


How to split hash key and value without using "foreach"

  • Please log in to reply
3 replies to this topic

#1 henka

  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 02 August 2006 - 06:08 PM

I just can't see the obvious here, so forgive the question if it seems silly.

Given the following:

$array['keyval'] = "some data";

Is there a shorter way of coding this expression:

$val = $array['keyval'] ? $array['keyval'] : 'keyval';

The above may seem quite short already, but my code uses some rather long 'keyval' strings, eg:

$val = $array['this is a rather long string...'] ? $array['this is a rather long string...'] : 'this is a rather long string...';

In other words, without using "foreach ($array as $key => $val)", I want to get at the $key string of a hash.

Any ideas?

#2 effigy

  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 02 August 2006 - 06:32 PM

Why not foreach? I think that is as short as you're going to get without it. Although, you could try extract.
Regexp | Unicode Article | Letter Database

#3 ronverdonk

  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 02 August 2006 - 07:43 PM

Don't you want to use foreach for a specific reason? If you want to access all keys you'll have to do some looping, whether foreach or after an extract or key/next combinations or whatever key extraction you want to use.

Ronald  8)

RTFM is an almost extinct art form, it should be subsidized.

#4 ryanlwh

  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 02 August 2006 - 07:51 PM

the way you have it there, you'll have to type the keys again anyway.. can you post the code so we know exactly how are you doing it??
Please use EDIT * 100...
Please use
or [php] * 1000...


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users