PHP 循环通过多维到一维数组


PHP Loop through Multidimensional to Onedimentional Array

我有一些javascript,通过ajax,将一些内容传递给PHP控制器。它传递的是一个多维数组,例如:

Array
(
  [0] => Array
    (
      [a] => - Item 1.1
    )
  [1] => Array
    (
      [a] => - Item 1.2
    )
  [2] => Array
    (
      [a] => - Item 1.3
    )
  [3] => Array
    (
      [b] => - Item 2.1
    )
  [4] => Array
    (
      [b] => - Item 2.2
    )
  [5] => Array
    (
      [b] => - Item 2.3
    )
  [6] => Array
    (
      [b] => - Item 2.4
    )
  [7] => Array
    (
      [c] => - Item 3.1
    )
  [8] => Array
    (
      [c] => - Item 3.2
    )
...
)

关于数组:0, 1, 2 等由数组自动制作。字母a, b, c等。是 HTML 中内容的类别,或者基本上是.classes .以下是我的一些JavaScript,以获取更多信息:

$("div[data-category]").each(function() {
    $cat = $(this).data("category");
    $str = $(this).siblings().each(function() {
        if ($(this).children().hasClass("selected")) {
            var response = {};
            response[$cat] = $(this).children().data("response-text") + "'n";
            responses.push(response);
        ...
        }
    });
});

我的最终目标是拥有一个

Array (
  [a] => - Item 1.1 'n Item 1.2 'n Item 1.3
  [b] => - Item 2.1 'n Item 2.2 'n Item 2.3 'n Item 2.4
  [c] => - Item 3.1 'n Item 3.2
)

所以我可以在我的JavaScript中做到这一点(我可能应该这样做),或者我可以保持JavaScript不变,并在PHP中创建一个循环来循环数组,获取所有子键,比如a,将所有a连接在一起(每个人'n新行),然后有一个一维数组。

请建议一个PHP循环解决方案或一个JavaScript解决方案,这将制作我想要的数组。

===== 编辑 =====

我的解决方案如下

(在答案部分)

这不会像您要求的那样创建换行符分隔的条目,但我真的不相信将格式硬编码到数据安利中。您可以轻松地遍历条目以创建所需的内容。

var data = [
    {a: "Item 1.1"},
    {a: "Item 1.2"},
    {a: "Item 1.3"},
    {b: "Item 2.1"},
    {b: "Item 2.2"},
    {b: "Item 2.3"},
    {b: "Item 2.4"},
    {c: "Item 3.1"},
    {c: "Item 3.1"},
];
var result = {};
for (var index in data) {
  for (var item in data[index]) {
    if (result[item] === undefined) {
        result[item] = [];
    }
    result[item].push(data[index][item]);
  }
}
console.log(result);

https://jsfiddle.net/oru62n80/

试试这个:

$data = [
    ['a' => '- Item 1.1'],
    ['a' => '- Item 1.2'],
    ['a' => '- Item 1.3'],
    ['b' => '- Item 2.1'],
    ['b' => '- Item 2.2'],
    ['b' => '- Item 2.3'],
    ['b' => '- Item 2.4'],
    ['c' => '- Item 3.1'],
    ['c' => '- Item 3.2'],
];
$uniqueKeys = [];
$callback = function ($item) use (&$uniqueKeys) {
    $uniqueKeys[] = array_keys($item)[0];
};
array_map($callback, $data);
$uniqueKeys = array_unique($uniqueKeys);
$combinedData = [];
foreach ($uniqueKeys as $key){
    $combinedData[$key] = implode(' 'n ', array_column($data, $key));
}
var_dump($combinedData);

我刚刚在 JavaScript 方面发现了一个超级简单的修复:

而不是var responses = [];声明:

var responses = {
    a: "",
    b: "",
    c: "",
    d: "",
    e: "",
    f: "",
    g: ""
};

然后我只是直接访问主数组,并使用+=继续添加到特定的键,每次循环时:

$("div[data-category]").each(function() {
    $cat = $(this).data("category");
    $str = $(this).siblings().each(function() {
        if ($(this).children().hasClass("selected")) {
            // NEW CODE-SOLUTION BELOW
            responses[$cat] += $(this).children().data("response-text") + "'n";
        ...
        }
    });
});

现在产生这种格式:

Array (
  [a] => - Item 1.1
        - Item 1.2
        - Item 1.3
  [b] => - Item 2.1
        - Item 2.2
        - Item 2.3
        - Item 2.4
  [c] => - Item 3.1
        - Item 3.2
) 

因此,在JavaScript端有一个更好的修复,而不是将一个混乱的数组发送到PHP控制器,并且必须在那里"修复"它。

PHP 中的循环:

foreach($array as $subArray){
    foreach($subArray as $val){
        $newArray[] = $val;
    }
}

换句话说,您可以使用PHP函数:

$flat = call_user_func_array('array_merge', $array);