0

I know this is a simple question, and one that has certainly been asked but my googlefu is seriously failing me here and I'm drowning.

I have an array of values like so

array( 
    array(topCat: "1", secondCat: "1", listItem: "List Item 1"), 
    array(topCat: "1", secondCat: "1", listItem: "List Item 2"),
    array(topCat: "1", secondCat: "2", listItem: "List Item 1"), 
    array(topCat: "1", secondCat: "3", listItem: "List Item 2"), 
    array(topCat: "2", secondCat: "1", listItem: "List Item 1")) etc etc

I need it to be like this:

array(
    array(topCat: 1, secondCat: 2, array("list item 1", "List Item 2"))).

One of my searches gave me this:

  $newArray = array();
    foreach ($oldArray as $row){
      $newArray[$row['topCat']][$row['secondCat']][] = $row['listItem'];

    }

And that almost works except it assigns the key to the value so it becomes array(1, 2: array())

Which isn't what I want.

I need to loop over the values and assign those as keys, how do I do this in PHP?

In the end the array needs to have 3 dimensions, topCat which would contain all of the second categories, secondCat which would contain all of the list items.

larsAnders
  • 3,813
  • 1
  • 15
  • 19
Patrick Cauley
  • 961
  • 2
  • 11
  • 20
  • Actually this is what you want, just maybe not clearly shown. The question is, what are you trying to do with it and why is that wrong. – sunshinejr Mar 17 '14 at 14:33
  • @ailvenge I will be returning it to my view in Laravel and then looping over it so each block will be a list of items sorted by category. – Patrick Cauley Mar 17 '14 at 14:34
  • @ailvenge So I need to be able to do something like this for each item in the top category create a div inside the div for each list item create a list item. Currently its more like for each item create a div which isn't what I want – Patrick Cauley Mar 17 '14 at 14:37

1 Answers1

1

try this :

<pre><?php

$oldArray = array( 
    array('topCat'=> "1", 'secondCat'=> "1", 'listItem'=> "List Item 1"), 
    array('topCat'=> "1", 'secondCat'=> "1", 'listItem'=> "List Item 2"),
    array('topCat'=> "1", 'secondCat'=> "2", 'listItem'=> "List Item 1"), 
    array('topCat'=> "1", 'secondCat'=> "3", 'listItem'=> "List Item 2"), 
    array('topCat'=> "2", 'secondCat'=> "1", 'listItem'=> "List Item 1"));

var_dump($oldArray);

$newArray =  array();

  $newArray = array();
    foreach ($oldArray as $row){
      $newArray[$row['topCat']][$row['secondCat']][] = $row['listItem'];

    }
var_dump($newArray);

$t2 = array();
foreach($newArray as $index=>$back){
foreach($back as $index2=>$back2){
    $t2[] = array('topCat'=>$index, 'secondCat'=>$index2, 'listItem'=>$back2);
}}
var_dump($t2);

returns :

array(4) {
  [0]=>
  array(3) {
    ["topCat"]=>
    int(1)
    ["secondCat"]=>
    int(1)
    ["listItem"]=>
    array(2) {
      [0]=>
      string(11) "List Item 1"
      [1]=>
      string(11) "List Item 2"
    }
  }
  [1]=>
  array(3) {
    ["topCat"]=>
    int(1)
    ["secondCat"]=>
    int(2)
    ["listItem"]=>
    array(1) {
      [0]=>
      string(11) "List Item 1"
    }
  }
  [2]=>
  array(3) {
    ["topCat"]=>
    int(1)
    ["secondCat"]=>
    int(3)
    ["listItem"]=>
    array(1) {
      [0]=>
      string(11) "List Item 2"
    }
  }
  [3]=>
  array(3) {
    ["topCat"]=>
    int(2)
    ["secondCat"]=>
    int(1)
    ["listItem"]=>
    array(1) {
      [0]=>
      string(11) "List Item 1"
    }
  }
}
n00b
  • 5,642
  • 2
  • 30
  • 48
  • I will try that, I swear I looked into just about each array function on that page too. I know I checked array_merge not sure if I checked recursive. – Patrick Cauley Mar 17 '14 at 14:30
  • One problem, I have a lot of arrays, like 700 of them all being returned as one array. That seems to require arguments for each array, is there anyway for it to take an array of arrays? – Patrick Cauley Mar 17 '14 at 14:31
  • Yep I just tried it, after some searching I think I found something close to what I need and it appears what I want is at least somewhat complex based on the answers I've found which makes me feel at least a bit better. – Patrick Cauley Mar 17 '14 at 14:35
  • http://stackoverflow.com/questions/15929440/build-multidimensional-from-single-array?rq=1 This is the closest I think I've found to what I need. – Patrick Cauley Mar 17 '14 at 14:35
  • This is indeed the answer I was looking for, I was trying to do this with a nested foreach however it looks like I should have expanded it. This is perfect, thank you! – Patrick Cauley Mar 17 '14 at 14:43
  • that only almost works, it only gives me the first index for the second level, but it's enough that I can do the rest of the work. Thank you again – Patrick Cauley Mar 17 '14 at 14:57