Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I redistribute an array into another array of a certain "shape". PHP

Tags:

arrays

php

I have an array of my inventory (ITEMS A & B)

Items A & B are sold as sets of 1 x A & 2 x B.

The items also have various properties which don't affect how they are distributed into sets.

For example:

$inventory=array(
array("A","PINK"),
array("A","MAUVE"),
array("A","ORANGE"),
array("A","GREY"),
array("B","RED"),
array("B","BLUE"),
array("B","YELLOW"),
array("B","GREEN"),  
array("B","BLACK")  
);

I want to redistribute the array $inventory to create $set(s) such that

$set[0] => Array
                (
                    [0] => array(A,PINK)
                    [1] => array(B,RED)
                    [2] => array(B,BLUE)

                )

$set[1] => Array
                (
                    [0] => array(A,MAUVE)
                    [1] => array(B,YELLOW)
                    [2] => array(B,GREEN)

                )

$set[2] => Array
                (
                    [0] => array(A,ORANGE)
                    [1] => array(B,BLACK)
                    [2] => NULL

                )

$set[3] => Array
                (
                    [0] => array(A,GREY)
                    [1] => NULL
                    [2] => NULL

                )

As you can see. The items are redistributed in the order in which they appear in the inventory to create a set of 1 x A & 2 x B. The colour doesn't matter when creating the set. But I need to be able to find out what colour went into which set after the $set array is created. Sets are created until all inventory is exhausted. Where an inventory item doesn't exist to go into a set, a NULL value is inserted.

Thanks in advance!

like image 457
matt Avatar asked Aug 30 '10 05:08

matt


2 Answers

I've assumed that all A's come before all B's:

$inventory=array(
                array("A","PINK"),
                array("A","MAUVE"),
                array("A","ORANGE"),
                array("A","GREY"),
                array("B","RED"),
                array("B","BLUE"),
                array("B","YELLOW"),
                array("B","GREEN"),  
                array("B","BLACK")  
                );

for($b_start_index = 0;$b_start_index<count($inventory);$b_start_index++) {
        if($inventory[$b_start_index][0] == 'B') {
                break;
        }
}

$set = array();
for($i=0,$j=$b_start_index;$i!=$b_start_index;$i++,$j+=2) {
        isset($inventory[$j])?$temp1=$inventory[$j]:$temp1 = null;
        isset($inventory[$j+1])?$temp2=$inventory[$j+1]:$temp2 = null;
        $set[] = array( $inventory[$i], $temp1, $temp2);                                                                                                                                                       
}
like image 137
codaddict Avatar answered Oct 25 '22 14:10

codaddict


To make it easier to use your array, you should make it something like this

$inv['A'] = array(
    'PINK',
    'MAUVE',
    'ORANGE',
    'GREY'
);
$inv['B'] = array(
    'RED',
    'BLUE',
    'YELLOW',
    'GREEN',
    'BLACK'
);

This way you can loop through them separately.

$createdSets = $setsRecord = $bTemp = array();
$bMarker = 1;
$aIndex = $bIndex = 0;

foreach($inv['A'] as $singles){
    $bTemp[] = $singles;
    $setsRecord[$singles][] = $aIndex;
    for($i=$bIndex; $i < ($bMarker*2); ++$i) {
        //echo $bIndex.' - '.($bMarker*2).'<br/>';
        if(empty($inv['B'][$i])) {
            $bTemp[] = 'null';
        } else {
            $bTemp[] = $inv['B'][$i];
            $setsRecord[$inv['B'][$i]][] = $aIndex;
        }
    }

    $createdSets[] = $bTemp;
    $bTemp = array();
    ++$bMarker;
    ++$aIndex;
    $bIndex = $bIndex + 2;
}


echo '<pre>';
print_r($createdSets);
print_r($setsRecord);
echo '</pre>';

To turn your array into an associative array, something like this can be done

<?php
$inventory=array(
    array("A","PINK"),
    array("A","MAUVE"),
    array("A","ORANGE"),
    array("A","GREY"),
    array("B","RED"),
    array("B","BLUE"),
    array("B","YELLOW"),
    array("B","GREEN"),
    array("B","BLACK")
);

$inv = array();
foreach($inventory as $item){
    $inv[$item[0]][] = $item[1];
}
echo '<pre>';
print_r($inv);
echo '</pre>';
like image 30
Edward Hew Avatar answered Oct 25 '22 15:10

Edward Hew