0

I can't figure this out...

How do I display a value from the first part of an array like this:

$payments['plan'][] = array(
    'hours'   => '2 Hours', 
    'price_a' => '90.00',
    'price_b' => '190.00',
);
$payments['plan'][] = array(
    'hours'   => '3 Hours', 
    'price_a' => '110.00',
    'price_b' => '220.00',
);
$payments['plan'][] = array(
    'hours'   => '4 Hours', 
    'price_a' => '120.00',
    'price_b' => '350.00',
);

In the above, let's say I wanted to display -- "90.00" -- how would I get that from the array?

I've tried different variations of reset, done some searching, can't figure this out... just how to display that value from the first part of the array.

Any suggestions?


Doing this:

reset($payments);
echo key($payments['plan']);

I just get the result: "0"

2
  • 3
    echo $payments['plan'][0]['price_a']; - Take note of example #4 Commented May 21, 2015 at 8:47
  • Thanks for sending the link, this one thing always fly's right over my head! It's mind boggling to me lol, I feel like foreach loops handle it differently because I never have to add the 0 in those? Commented May 21, 2015 at 8:51

2 Answers 2

2

Key of the first 'block' is 0, so:

echo $payments['plan'][0]['price_a']
Sign up to request clarification or add additional context in comments.

1 Comment

My god, I don't know why I always miss that when I'm working with arrays! That damn 0 value always gets me lol, thanks... feels like such a waste asking on here when it's something that just fly's over my head
2

just use echo $payments['plan'][0]['price_a'];

Inside a loop, you could use :

foreach($payments['plan'] as $index => $values) {
    echo $values['price_a'];
}

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.