1

How is it possible to perform a foreach function without doing a loop for example

foreach($result['orders'] as $order) {

But I don't want to do a foreach I want something like

$result['orders'] == $order;

Or something like that instead of doing it inside an loop because $result['orders'] is only returning 1 result anyway so I don't see the point in doing it in a loop.

Thank you

Shoe
  • 74,840
  • 36
  • 166
  • 272
Curtis Crewe
  • 4,126
  • 5
  • 26
  • 31
  • Can you explain what you are trying to accomplish? – Mike Jan 26 '13 at 23:35
  • 3
    If there's only one, just access it `$result['orders'][0]` – Michael Berkowski Jan 26 '13 at 23:36
  • I'm trying to get information from an API which is stored inside $result['orders'] so you could say it's an array, but i don't want to do a foreach to be able to get the content – Curtis Crewe Jan 26 '13 at 23:36
  • @CurtisCrewe There would be lots of ways to get at the data inside, but without seeing the structure of the array we can't say what is the best method. – Michael Berkowski Jan 26 '13 at 23:37
  • possible duplicate of [Get value without knowing key in one-pair-associative-aray](http://stackoverflow.com/questions/11145185/get-value-without-knowing-key-in-one-pair-associative-aray) – nickb Jan 26 '13 at 23:38

3 Answers3

5

You can get the first (and apparently only) element in the array with any array function that gets an element from the array, e.g. array_pop() or array_shift():

$order = array_shift( $result['orders']);

Or list():

list( $order) = $result['orders'];

Or, if you know it's numerically indexed, access it directly:

$order = $results['orders'][0];
nickb
  • 59,313
  • 13
  • 108
  • 143
  • Thank you very much nick, i appreciate you taking your time to find 3 different ways. – Curtis Crewe Jan 26 '13 at 23:39
  • @Curtis - There are actually a lot more, I linked a similar question, you will get a laugh from how many ways this is possible in PHP (I know I did...) – nickb Jan 26 '13 at 23:40
1

Are you maybe just looking for this?

$result['orders'] = $result['orders'][0];
symlink
  • 11,984
  • 7
  • 29
  • 50
0

You have a comparison operator (==) rather than an assignment operator (=) in your second code example. If you are just trying to set a variable equal to a position in an array, you can use:

$order = $results['orders'];

I am not sure if that is what you are trying to accomplish though.

Mike
  • 1,718
  • 3
  • 30
  • 58