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";
5
  • If sort_order is numeric and unique for any set of categories at the same level in the heirarchy, use that to index the array... Otherwise this is a standard recursive parent->child array building. Commented Feb 7, 2011 at 23:21
  • 1
    Could you please provide a more complete example of your array? (best with var_export). Commented Feb 7, 2011 at 23:21
  • Arrays and array functionality in PHP are very flexible. However, I don't understand your array structure. Can you print_r() your array? If you put the output between a pre tag, it would show up very nicely: <pre><?php print_r($yourArray); ?></pre> Commented Feb 7, 2011 at 23:29
  • @ChristianSciberras: <?php echo '<pre>'.print_r($yourArray,1).'<pre>';?> seems more elegant, no? ;-) Commented Feb 7, 2011 at 23:37
  • @Brad - But more code intensive, and less IDE-friendly (the IDE can't know about the pre inside the string ;) Commented Feb 8, 2011 at 10:59

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

First you create an empty array, it will be used to store your result.

$result = array();

You need to iterate through your initial array, you can use foreach().

Then, given your parent_category_id simply use an if statement to check whether it's the given id or not.

If it is, just construct and push your result to your $result array.

Use any of the sort functions you like

Use the magic return $result;

You're done.

Comments

0
function returnSortedParents($categories, $target_parent){

    $new_list = array();

    foreach($categories as $index => $array){
        //FIND ONLY THE ELEMENTS MATCHING THE TARGET PARENT ID 
        if($array['parent_category_id']==$target_parent){ 
            $new_list[$index = $array['sort_order'];
        }

    return asort($new_list);  //SORT BASED ON THE VALUES, WHICH IS THE SORTING ORDER
}

Comments

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.