0

This is what i am currently using

<?php $sidebar = $this->data['sidebar']; 
$lastKey = array_pop(array_keys($sidebar));
$sidebar = $this->data['sidebar'][$lastKey]; ?>
<?php foreach($sidebar as $key => $item) { ?>
<li id="<?php echo Sanitizer::escapeId( "pt-$key" ) ?>"<?php
    if ($item['active']) { ?> class="active"<?php } ?>><a href="<?php echo htmlspecialchars($item['href']) ?>"><?php echo htmlspecialchars($item['text']) ?></a></li>
<?php } ?>

This is what i get (http://pastebin.com/t6Y2ZtMF) when i print_r($sidebar); I want to get the last Array which is Categories and turn it into links.

I am new to php, so my method could be wrong even though it works. I is there a right way to pull the Categories Array or the above code is good as it is?

5 Answers 5

5
$lastValue = end($array);
$lastKey = key($array); // current key, which is the last since you called end()

After update:

You don't seem to be needing the key, only the array:

<?php $lastSidebarValue = end($this->data['sidebar']); ?>
<?php foreach ($lastSidebarValue as $key => $item) : ?>
    business as usual...
<?php endforeach; ?>

Since you know you want the key 'Categories' though (not the last key), this seems the most logical thing to do:

<?php foreach ($this->data['sidebar']['Categories'] as $key => $item) : ?>
    business as usual...
<?php endforeach; ?>
Sign up to request clarification or add additional context in comments.

10 Comments

I get only the last key but not the values of the last key
@user What does your array look like? print_r($array)
@user So the last value is an array itself. What exactly do you expect to get out of this?
Nothing! since it is array, So how would i go about getting Array->Array->Key-> Values?
@user You need to clarify what you want. Update your question with the example array and your expected output. Do you want the last value of the innermost array of the last array, or what exactly do you need?
|
2

I think the end() function would be a great solution: http://php.net/manual/en/function.end.php It basically returns the value of the last element in the array being passed to it.

$sidebar = end($sidebar);

2 Comments

from this echo end($sidebar); i get "Array"
Sorry, I forgot that you also needed to return the last key along with the last value. You could definitely do what deceze posted for the key.
2

If you want to get a key/value pair without popping & pushing the array, set the internal cursor to the end of the array and then use list and each to get the key & value.

// set up your array however you had it
$array = ...;

// move the cursor to the end of the array
end($array);

// use list() and each() to extract your key/value pair    
list($key,$val) = each($array);  

// $key will now have the last key
// $val will have the last value

Comments

1

perhaps, end:

$fruits = array('apple', 'banana', 'cranberry');
echo end($fruits); // cranberry

1 Comment

i was expecting you to make the obvious changes, i was just quoting from the manual.
1

You can use `end()` instead of `array_pop()`. But both works for the **last element** of the array. The only difference is `end()` **points out** the **last element** of the array without effecting it and `array_pop()` **pops** the element off the **end** of array.

Please go through the following links for detail information

end() | array_pop()

1 Comment

i see, does this look fine $lastKey = end(array_keys($sidebar));

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.