0

Apologies for the vague title, I'm not sure what you might call this puzzle.

I have a list of Attributes and Variations in two tables. Variations belong to Attributes. For example :

Attributes = Size and Length
Variations = 
  Size.Small, Size.Medium, Size.Large,
  Length.Short, Length.Regular, Length.Long

So an SQL search might return something like

'Attribute' => array(
    'title' => 'Size',
    'Variation' => array(
        'title' => 'Small',
        'title' => 'Medium',
        'title' => 'Large',
    ),
    'title' => 'Length',
    'Variation' => array(
        'title' => 'Short',
        'title' => 'Regular',
        'title' => 'Long',
    ),
)

What Im trying to do is present this to the browser as follows :

Small : Short
Small : Regular
Small : Long
Medium : Short
Medium : Regular
Medium : Long
Large : Short
Large : Regular
Large : Long

Bear in mind, the data may not be limited to just two Attributes, but could be several. Similarly with variations.

Is there a neat way to do this using array manipulation commands in PHP, or should it be done with foreach loops etc.

I suspect this may not be the right forum for this kind of question but if anyone can point me in the right direction it would be appreciated.

Robin Walmsley
  • 85
  • 1
  • 1
  • 10
  • Hi Robin, so you want to replace a value with another text? So incase Small is displayed, you want to have it changed to Short? – Ronnie Oosting Nov 25 '19 at 14:00
  • Ok no need to answer, I think I found a solution here [How to generate in PHP all combinations of items in multiple arrays](https://stackoverflow.com/questions/8567082/how-to-generate-in-php-all-combinations-of-items-in-multiple-arrays) – Robin Walmsley Nov 25 '19 at 14:01
  • 1
    Possible duplicate of [How to generate in PHP all combinations of items in multiple arrays](https://stackoverflow.com/questions/8567082/how-to-generate-in-php-all-combinations-of-items-in-multiple-arrays) – Ronnie Oosting Nov 25 '19 at 14:02
  • Ronnie, I couldn't phrase the question properly, but my other comment described it perfectly. All sorted thanks. – Robin Walmsley Nov 25 '19 at 14:02

1 Answers1

1

There's a few problems with your code. First, you have multiple keys with the same name: 'title' => 'Short', 'title' => 'Regular'. If you set it up like this, you'll end up losing data because array can't have duplicate keys. Also, consider using short array syntax $arr = []; instead of $arr = array(). That said, here's working code that you can use: https://3v4l.org/EIUEQ . The output is (with three demo attributes):

Array
(
    [0] => Small Short Black
    [1] => Small Short White
    [2] => Small Regular Black
    [3] => Small Regular White
    [4] => Small Long Black
    [5] => Small Long White
    [6] => Medium Short Black
    [7] => Medium Short White
    [8] => Medium Regular Black
    [9] => Medium Regular White
    [10] => Medium Long Black
    [11] => Medium Long White
    [12] => Large Short Black
    [13] => Large Short White
    [14] => Large Regular Black
    [15] => Large Regular White
    [16] => Large Long Black
    [17] => Large Long White
)
Pavel Lint
  • 3,252
  • 1
  • 18
  • 18
  • Thanks for your input, however my data was just an example. I do already use the short array syntax, I merely copy-pasted the output from the SQL find which presents the results in the old array syntax. The use of title is extensive in my code, but is always prefixed with the Table name .. so Variation.title, Attribute.title. I use CakePHP and I've never experienced any problems with this. In my sample array, the keys are numeric I merely removed them for clarity. Thanks anyway. – Robin Walmsley Nov 27 '19 at 11:12
  • hey @RobinWalmsley, check out the sample code at https://3v4l.org/EIUEQ , you could probably adjust it for your use :) – Pavel Lint Nov 27 '19 at 11:14
  • Thanks, similar code to what I'm now using but useful none the less. :) – Robin Walmsley Nov 28 '19 at 12:30