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

  • SEARCH
  • Home
  • 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 3959836
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:46:46+00:00 2026-05-20T02:46:46+00:00

I have a multi-dimensional array that looks like this: The base array is indexed

  • 0

I have a multi-dimensional array that looks like this:
The base array is indexed based on category ids from my catalog.

$cat[category_id]

Each base array has three underlying elements:

['parent_id']
['sort_order']
['name']

I want to create a function that allows us to create a list of category_id’s and names for a given parent_category_id in the correct sort order. Is this possible? Technically it is the same information, but the array is constructed in a weird way to extract that information.

Here is an example definition for the array:

$cat = array();
$cat[32]['parent_id']= 0;
$cat[32]['sort_order']= 1;
$cat[32]['name']= 'my-category-name1';
$cat[45]['parent_id']= 0;
$cat[45]['sort_order']= 0;
$cat[45]['name']= 'my-category-name2';
$cat[2]['parent_id']= 0;
$cat[2]['sort_order']= 2;
$cat[2]['name'] = "my-category-name3";
$cat[3]['parent_id']= 2;
$cat[3]['sort_order']= 1;
$cat[3]['name'] = "my-category-name4";
$cat[6]['parent_id']= 2;
$cat[6]['sort_order']= 0;
$cat[6]['name'] = "my-category-name5";
  • 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. Editorial Team
    Editorial Team
    2026-05-20T02:46:46+00:00Added an answer on May 20, 2026 at 2:46 am

    Assuming it’s something of this sort:

    $ary = Array(
      0 => Array(
        'parent_category_id' => null,
        'sort_order' => 0,
        'name' => 'my-category-name0'
      ),
      1 => Array(
        'parent_category_id' => 0,
        'sort_order' => 1,
        'name' => 'my-category-name1'
      ),
      2 => Array(
        'parent_category_id' => 0,
        'sort_order' => 2,
        'name' => 'my-category-name2'
      ),
      3 => Array(
        'parent_category_id' => null,
        'sort_order' => 0,
        'name' => 'my-category-name3'
      ),
      4 => Array(
        'parent_category_id' => 3,
        'sort_order' => 0,
        'name' => 'my-category-name4'
      )
    );
    

    You can use a combination of a foreach and usort to achieve what you’re going for.

    // @array: the array you're searchign through
    // @parent_id: the parent id you're filtering by
    function getFromParent($array, $parent_id){
      $result = Array();
      foreach ($array as $category_id => $entry){
        if ($entry['parent_category_id']===$parent_id)
          $result[$category_id] = $entry;
      }
      usort($result,create_function('$a,$b','return ($a["sort_order"]>$b["sort_order"]?1:($b["sort_order"]<$a["sort_order"]?-1:0));'));
      return $result;
    }
    
    var_export(getFromParent($ary,0));
    

    EDIT Sorry, fixed some syntax errors. Tested, and works (at least to result in what I was intending)
    EDITv2 Here’s the raw output from the above:

    array (
      0 => 
      array (
        'parent_category_id' => 0,
        'sort_order' => 1,
        'name' => 'my-category-name1',
      ),
      1 => 
      array (
        'parent_category_id' => 0,
        'sort_order' => 2,
        'name' => 'my-category-name2',
      ),
    )
    

    (Used var_export just for you @FelixKling)

    EDITv3 I’ve updated my answer to go along with the OP’s update. I also now make it retain the original “category_id” values in the result array.

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

Sidebar

Related Questions

I have a multi-dimensional array right now that looks like this: function art_appreciation_feeds() {
So I have a multi-dimensional array looks like this. $config = array( First Name
I have an multidimensional array that looks like this: Array ( [0] => Array
I have a multi dimensional array, like this: array('name' => array('title'=>'Title','date'=>'Created')) I store it
I have a multidimensional array that looks something like this: ourThing = array( 'id'
I have a list of recipes obtained from a database that looks like this:
I have a multidimensional array that looks like this: Array ( [0] => Array
I have a multidimensional array that looks like this [0] => Array ( [recordId]
My problem.. I have a PHP array that looks like this: [1013] => [1154]
I have a multidimensional array that looks like this: Array ( [0] => Array

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.