开发者

How to extract data out of a specific PHP array

开发者 https://www.devze.com 2023-02-08 22:48 出处:网络
I have a multi-dimensional array that looks like this: The base array is indexed based on category ids from my catalog.

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 i开发者_如何学Pythons 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";


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.


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.


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
}
0

精彩评论

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