【问题标题】:Flatten laravel relationship collection / array展平 laravel 关系集合/数组
【发布时间】:2020-05-07 11:55:34
【问题描述】:

我遇到了数组转换问题 我去使用 laravel 集合使用 map 或 php 数组

大批 ( [0] => 数组 ( [id] => 3487 [title_fa] => 父亲 [代码] => 01 [father_id] => 0 [webmaster_id] => 8 [孙子] => 数组 ( [id] => 3488 [title_fa] => 孩子 1 [代码] => 02 [父亲ID] => 3487 [孙子] => ) ) [1] => 数组 ( [id] => 3489 [title_fa] => 父亲 2 [代码] => 03 [father_id] => 3488 [孙子] => 数组 ( [id] => 3490 [title_fa] => 孩子 2 [代码] => 04 [father_id] => 3489 [孙子] => ) ) )

数组转换成数组

大批 ( [0] => 数组 ( [title_fa] => 父亲 [代码] => 01 ) [1] => 数组 ( [title_fa] => 父亲>孩子1, [代码] => 0102 ) [2] => 数组 ( [title_fa] => 父亲2 [代码] => 03 ) [3] => 数组 ( [title_fa] => 父亲2>孩子2 [代码] => 0304 ) )

Illuminate\Support\Collection::filter()

 public function filter(callable $callback = null)

{

    if ($callback) {

        return new static(Arr::where($this->items, $callback));

    }



    return new static(array_filter($this->items));

}

【问题讨论】:

  • 数组将只有一个元素树[0]?如果不止一个,它会是什么样子?
  • 检查我的答案。
  • 你的问题是什么?有什么代码需要分享吗?
  • 是数组多元素树

标签: php arrays laravel collections


【解决方案1】:

虽然答案已经给出,但输出不是 OP 的期望输出。

这可以使用recursion这样解决:

<?php
$codes=array();
$code="";
$title="";
$outerArray = array();

function callAgain($arr,&$outerArray,&$code,&$title){
   $check=0;
  foreach($arr as $value){
      if(is_array($value)){
         callAgain($value,$outerArray,$code,$title);
      }
      else{
        if($check==0){
            $code.=$arr['code'];
            if($title==""){
                $title.=$arr['title_fa']; 
            }
            else{
             $title.=">".$arr['title_fa'];
            }
            $outerArray[] = array ('title_fa'=>  $title,'code'=>$code);
         $check=1;    
        }  
      }

  }

}



$arr = [
        ['title_fa' => 'Father', 'code' => '02', 'grandchildren'=>[
            'title_fa' => 'Child 1', 'code' => '01', 'grandchildren'=>[
                'title_fa' => 'Child 2', 'code' => '01', 'grandchildren'=> 
                [
                'title_fa' => 'Child 3', 'code' => '01', 'grandchildren'=> '',
            ]
            ]
        ]] 
    ];


callAgain($arr,$outerArray,$code,$title);
print_r($outerArray);

输出

Array
(
    [0] => Array
        (
            [title_fa] => Father
            [code] => 02
        )

    [1] => Array
        (
            [title_fa] => Father>Child 1
            [code] => 0201
        )

    [2] => Array
        (
            [title_fa] => Father>Child 1>Child 2
            [code] => 020101
        )

    [3] => Array
        (
            [title_fa] => Father>Child 1>Child 2>Child 3
            [code] => 02010101
        )

)

【讨论】:

  • 看OP想要的输出,修复title_fa
  • @AksenP 我觉得他可以做到,因为逻辑在那里,但我再次更新了我的答案。
  • 伙计,这个 OP 很奇怪:D 他甚至看不出提到的期望输出和其他解决方案的最终结果之间的区别。
  • 没问题,我只是想写一个递归解决方案:D
  • 是数组多元素树
【解决方案2】:

扁平化 laravel 关系集合/数组

public function flatten($array)
{
        $flatArray = [];

        if (!is_array($array)) {
            $array = (array)$array;
        }

        foreach($array as $key => $value) {
            if (is_array($value) || is_object($value)) {
                $flatArray = array_merge($flatArray, $this->flatten($value));
            } else {
                $flatArray[0][$key] = $value;
            }
        }

        return $flatArray;
}

//...
$flatten = $this->flatten($myData);
//...

【讨论】:

  • 这不是想要的输出,奇怪的 OP,奇怪的答案
  • @FouedMOUSSI 输出有问题,代码未按要求生成。
  • 是的,如我所见,您可以在上面编辑3v4l.org/YANZS 并更新我的答案;)
  • @AksenP 你可以查看我的解决方案。
猜你喜欢
  • 2017-09-09
  • 1970-01-01
  • 2017-10-08
  • 2017-07-22
  • 2022-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
相关资源
最近更新 更多