简体   繁体   English

如何基于键合并嵌套数组?

[英]How to merge nested arrays based on keys?

PHP数组

I have array as shown on picture above, I am trying to merge all the nested arrays based on keys, so for example right now there are 5 arrays with key 34. I want one array with 34 key and 5 values. 我有如上图所示的数组,我试图基于键合并所有嵌套的数组,因此例如现在有5个带有键34的数组。我想要一个包含34个键和5个值的数组。 How can I achieve that? 我该如何实现? Here is my code. 这是我的代码。

    $proba = array();
while($row = $result->fetch(PDO::FETCH_ASSOC)){
    extract($row);
    if(!in_array($id, array_column($posts_arr['lokacije'], 'id'))) {
        $post_item = array(
            'id' => $id,
            'name' => $name,
            'state' => $state,
            'address' => $address,
            'phone' => $phone,
            'working_hours' => $working_hours,
            'description' => $description,
            'email' => $email,
            'fb_page' => $fb_page,
            'website' => $website,
            'additional_info' => $additional_info,
            'lat' => $lat,
            'lng' => $lng
        );
    array_push($posts_arr['lokacije'], $post_item);
    };

    array_push($proba, array($id => $naziv));
    $test = array_merge_recursive($proba, array($id => $naziv));
}
print_r($test);
$proba = array();
while($row = $result->fetch(PDO::FETCH_ASSOC)){
    extract($row);

    //....

    if (empty($proba[$id])) {
        $proba[$id] = array();
    }
     $proba[$id][] = $naziv;
}

Also I don't understand why you use extract - I suppose, $post_item is the same as $row . 我也不明白为什么要使用extract -我想, $post_item $row 相同

I created a demo just like yours, here is the code: 我像您一样创建了一个演示,这是代码:

$arr = [
    [2 => 'A'],
    [34 => 'B'],
    [34 => 'C'],
    [34 => 'D'],
    [34 => 'E'],
    [34 => 'F'],
    [20 => 'G'],
];

$result = [];

foreach($arr as $a) {
    foreach($a as $key => $value) {
        array_key_exists($key, $result) ?
            $result[$key][] = $value:
            $result[$key] = [$value];
    }
}

The result is: 结果是:

array(3) {
  [2]=>
  array(1) {
    [0]=>
    string(1) "A"
  }
  [34]=>
  array(5) {
    [0]=>
    string(1) "B"
    [1]=>
    string(1) "C"
    [2]=>
    string(1) "D"
    [3]=>
    string(1) "E"
    [4]=>
    string(1) "F"
  }
  [20]=>
  array(1) {
    [0]=>
    string(1) "G"
  }
}

The simple answer to what I understand you're trying to do: 我了解您正在尝试做的一个简单答案:

foreach($input as $nr=>$data) {
    foreach($data as $key=>$val) {
        $output[$key][$nr] = $val;
    }
}

This will simply flip the hierarchy levels. 这将简单地翻转层次结构级别。 If you just want to collect values, this is one way to do it. 如果您只想收集值,这是一种方法。 It keeps the original array indexes, but instead of "[$nr]" you can just say "[]" to make it a simple sequential array. 它保留了原始数组的索引,但是您可以说“ []”以使其成为简单的顺序数组,而不是“ [$ nr]”。

If you present your array as text instead of that big image there, I'll show you the results. 如果您将数组显示为文本而不是那里的大图像,我将向您显示结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM