0

I am able to access and output a full array list of Zip items like so (this is working as expected):

... (this is a foreach within a foreach)

foreach ($plan_edit['Zip'] as $zip) :
    echo $zip['title'] . "<br />";
endforeach; ...

Returns:

Array
(
    [0] => Array
        (
            [id] => 110
            [state_id] => 1
            [title] => 97701
            [PlansZip] => Array
                (
                    [id] => 83698
                    [plan_id] => 443
                    [zip_id] => 110
                )

        )

    [1]

I am trying to ONLY get the first and last value (of ['title']) of each array set for each main record.

I've been messing around with phps array current() and end() functions, but I can only get "Array " to print out with those.

I know I am doing something wrong, but kind of lost direction at this point.

Any constructive criticism of my work/methods is welcome.

This is where I am at currently:

<?php

foreach ($plan_edit['Zip'] as $zip) :
    echo current($zip['title']) . "<br />";
     endforeach;

foreach ($plan_edit['Zip'] as $zip) :
    echo end($zip['title']) . "<br />";
     endforeach; 

     ?>
1
  • What's messing my thought process up is I am trying various ways to do this like: <?php foreach ($plan_edit['Zip'] as $key=>$value) : echo max($value) . "<br />"; endforeach; ?> - but the $value output is "Array" not the actual value. Commented Mar 24, 2011 at 23:03

1 Answer 1

3
$first = reset($plan_edit['Zip']);
$last = end($plan_edit['Zip']);
echo $first['title'];
echo $last['title'];

If the array is numerically indexed, you can also just do:

echo $plan_edit['Zip'][0]['title'];
echo $plan_edit['Zip'][count($plan_edit['Zip']) - 1]['title'];
Sign up to request clarification or add additional context in comments.

1 Comment

your second suggestion solved the problem. I was trying to work on a solution with the first suggestion, but the array is numeric. Cheers.

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.