Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 176877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:57:19+00:00 2026-05-11T13:57:19+00:00

This is actually a problem that I’ve already solved, but I suspect that there

  • 0

This is actually a problem that I’ve already solved, but I suspect that there may be a better solution. I have a nested array which basically represents a list of all the nodes from a category tree (in no particular order):

Array(     [0] => Array(         [Category] => Array(             [id] => 49             [name] => Poster         )         [Child] => Array()     )     [1] => Array(         [Category] => Array(             [id] => 48             [name] => Sticker         )         [Child] => Array()     )     [2] => Array(         [Category] => Array(             [id] => 46             [name] => Hat         )         [Child] => Array()     )     [3] => Array(         [Category] => Array(             [id] => 45             [name] => Hoodie         )         [Child] => Array()     ) ) 

You’ll notice that $node['Child'] is an empty array in all instances. This is because I’ve already used array_filter() to filter out all non-leaf-nodes. Still, what I ultimately want is to have a single-dimensional array of the leaf nodes like this:

Array(     [49] => Poster     [48] => Sticker     [46] => Hat     [45] => Hoodie ) 

So what would be the most elegant way of converting the first array to the second array?

Optionally, you can also start with an array of both leaf-nodes and non-leaf-nodes, and try to both prune the array and restructure it. An example of a non-leaf-node would be something like:

[7] => Array(     [Category] => Array(         [id] => 36         [name] => Merch     )     [Child] => Array(         [0] => Array(             [id] => 49             [name] => Poster         )         [1] => Array(             [id] => 48             [name] => Sticker         )         [2] => Array(             [id] => 40             [name] => Apparel         )     ) ) 

Lastly, I’m using CakePHP, so use of CakePHP’s Set class is also acceptable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:57:20+00:00Added an answer on May 11, 2026 at 1:57 pm

    Elegant? Define elegant. I think some people are going to turn this into a golf contest as opposed to simply doing what makes sense and is evident to anyone stumbling upon the code later on:

    $cats = array(); foreach($categories as $c) {     $cats[$c['Category']['id']] = $c['Category']['name']; } 

    I suspect this is what you have, in which case I suggest you pat yourself in the back and move on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 145k
  • Answers 145k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer IF @A > 0 OR @B > 0 OR @C… May 12, 2026 at 8:53 am
  • Editorial Team
    Editorial Team added an answer Ok, for history, in case anyone else has this question… May 12, 2026 at 8:53 am
  • Editorial Team
    Editorial Team added an answer I did a modification of Manu's idea, just switching the… May 12, 2026 at 8:53 am

Related Questions

This is actually a problem that I've already solved, but I suspect that there
First off, I read all of the suggested questions that sounded halfway relevant and
I wanted to have more than one controller and view for same object/model in
In Python, I've seen the recommendation to use holding or wrapping to extend the
Could someone take the time to explain me the language underpinnings here : int

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.