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 9239073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:54:13+00:00 2026-06-18T07:54:13+00:00

I have a product array containing a list of products sorted by their display

  • 0

I have a product array containing a list of products sorted by their display order.

$products = array(
[0] = array(
 "productID" => 189736,
 "title" => "Spice Girls Album",
 "category" => "CD",
 "order" => "0"
),
[1] = array(
 "productID" => 23087,
 "title" => "Snakes on a plane",
 "category" => "DVD",
 "order" => "0"
),
[2] = array(
 "productID" => 9874,
 "title" => "The Beatles Album",
 "category" => "CD",
 "order" => "1"
), ... etc etc

I’m trying to figure out the logic of turning it into a category array like this:

$categories = array(
   [0] => array(
        "title" => "CD",
        "products" => array (
            [0] => "Spice Girls Album",
            [1] => "The Beatles Album"
        ) 
    ),
   [1] => array(
        "title" => "DVD",
        "products" => array (
            [0] => "Snakes on a plane"
        ) 
)

So for each product I have:

if (!in_array($product['cateogry'], $categories)){
    $categories[] = $product['cateogry'];
    $categories[$product['category']][] = $product; 
} else {
    $categories[$product['category']][];
}

But this isn’t working, because I don’t think in_array is checking deep enough into the category array. Does anyone have any advice on the best way to solve this? Many thanks

  • 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-06-18T07:54:14+00:00Added an answer on June 18, 2026 at 7:54 am

    You have the right idea with $categories[$product['category']][] = $product. What you need to be checking is if the key $product['category'] exists in $categories:

    if (array_key_exists($product['category'], $categories)) {
        $categories[$product['category']]['products'][] = $product['title'];
    } else {
        // initialize category data with first product
        $categories[$product['category']] = array(
            'title' => $product['category'],
            'products' => array($product)
        );
    }
    

    This will give you an array in the form:

    $categories = array(
       "CD" => array(
            "title" => "CD",
            "products" => array (
                [0] => "Spice Girls Album",
                [1] => "The Beatles Album"
            ) 
        ),
       "DVD" => array(
            "title" => "DVD",
            "products" => array (
                [0] => "Snakes on a plane"
            ) 
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have product list ( 9 products in multidimensional array ), i want to
I have a problem building an unordered list from multidimensional array containing entities and
I have a object (product), with a property of type 'array' e.g. product.tags =
I have a products array of widgets. Some widgets have the reserved period symbol
I have an array of products IDs and I need to find the IDs
I have a category collection and each category has a array of hashes containing
I have a 2-d array containing pairs of values and I'd like to make
Hi i have got the particular * product * array from my code for
I have an array of products $products = array_count_values($products); now I have an array
I have an bundle product as array like this: (take from params when add

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.