开发者

CakePHP echo first and last record of array output foreach() iteration

I am able to access and output a 开发者_StackOverflowfull 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; 

     ?>


$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'];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜