skip to Main Content

I’m trying to recursively create a php array for my phone database.
The database has many different phones and I want to categorize them like this:

iPhone 14 Pro:
    128gb:
       Black: 2
       White: 1
    256gb:
        Red: 3
        Black: 1
iPhone 13 Pro:
................

The database is formatted like this:

model: iPhone 14 Pro, storage: 128gb, color: Black
model: iPhone 14 Pro, storage: 256gb, color: Blue
...........

And so on

I tried using some nested loops with too many foreach statements and it didn’t work at all.
I think I was using the array_push method incorrectly because I never got the correct result

My hope is that someone here knows how to recursively create this array by looping through the full dataset.
Thank you in advance!

2

Answers


  1. Assuming the defining factors, like model, storage and color are columns in the database, you could create a simple function like the following

    function generateProductArray($database) {
        $result = array();
    
        foreach ($database as $entry) {
            $model = $entry['model'];
            $storage = $entry['storage'];
            $color = $entry['color'];
    
            if (!isset($result[$model])) {
                $result[$model] = array();
            }
    
            if (!isset($result[$model][$storage])) {
                $result[$model][$storage] = array();
            }
    
            if (!isset($result[$model][$storage][$color])) {
                $result[$model][$storage][$color] = 0;
            }
    
            $result[$model][$storage][$color]++;
        }
    
        return $result;
    }
    

    You also have the ability to simplify the functionality/code using inbuilt PHP functions like array_reduce( ):

    function generateProductArray($database) {
        return array_reduce($database, function ($result, $entry) {
            [$model, $storage, $color] = array_values($entry);
    
            $result[$model][$storage][$color] = ($result[$model][$storage][$color] ?? 0) + 1;
    
            return $result;
        }, []);
    }
    

    Which provides the desired output.

    Array
    (
        [iPhone 14 Pro] => Array
            (
                [128gb] => Array
                    (
                        [Black] => 2
                    )
    
                [256gb] => Array
                    (
                        [Blue] => 1
                        [Red] => 2
                    )
    
            )
    
    )
    

    Example

    Login or Signup to reply.
  2. Not sure how you data is structured, but something like this might work:

    $data = [
      [
        'model' => 'iPhone 14 Pro',
        'storage' => '128gb',
        'color' => 'Black',
      ],
      [
        'model' => 'iPhone 14 Pro',
        'storage' => '128gb',
        'color' => 'Green',
      ],
      [
        'model' => 'iPhone 14 Pro',
        'storage' => '256gb',
        'color' => 'Blue',
      ],
      [
        'model' => 'iPhone 14 Pro',
        'storage' => '256gb',
        'color' => 'Blue',
      ],
    ];
    
    $devices = [];
    foreach ($data as $phone) {
      list($model, $storage, $color) = array_values($phone);
      $count = $output[$model][$storage][$color] ?? 0;
      $output[$model][$storage][$color] = ++$count;
    }
    

    The above would create an array like this:

    Array
    (
        [iPhone 14 Pro] => Array
            (
                [128gb] => Array
                    (
                        [Black] => 1
                        [Green] => 1
                    )
    
                [256gb] => Array
                    (
                        [Blue] => 2
                    )
    
            )
    
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search