PHP单数组多维价值观

PHP单数组多维价值观

本文介绍了PHP单数组多维价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个简单的问题,另一个肯定已经问,但我的googlefu严重失败在这里我和我淹没。

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.

我有值的这样一个数组

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'];

    }

和几乎工程,使其成为数组除了它分配的关键值(1,2:阵列())

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

这是不是我想要的。

我需要循环的价值和分配这些钥匙,我该怎么做这在PHP?

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

在年底数组需要有3个尺寸,topCat这将包含所有类别的第二,secondCat这将包含所有的列表项的。

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.

推荐答案

试试这个:

<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);

返回:

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"
    }
  }
}

这篇关于PHP单数组多维价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 17:55