Ajvanho's avatar
Level 14

Flatten keys?

Is it possible to get keys from flatten() helper

0 likes
11 replies
Ajvanho's avatar
Level 14

@MohamedTammam It looks like this, I had to change the data due to the reason of the protection, but structure is this...

Illuminate\Support\Collection {#2573
  #items: array:9 [
    0 => array:3 [
      "lhfdfhhf" => "hfddf"
      "hfddf" => "XIUkKlzEufCabjfh"
      "hfdhhfdf" => array:1 [
        "content" => """
          <p>By converting the collection to a LazyCollection, we avoid having to allocate a ton of additional memory. Though the original collection still keeps its values in memory, the subsequent filters will not. Therefore, virtually no additional memory will be allocated when filtering the collection's results..</p>
          """
      ]
    ]
    1 => array:3 [
      "hfdhf" => "tfdhcfjcfgn"
      "hfdhdf" => "t1xJlgzJZkCPweBi"
      "hffdhfd" => array:2 [
        "tafdhfde" => ""
        "fhdhdhf" => array:1 [
          0 => array:3 [
            "lhfdhdf" => "hfdhdh"
            "kdhf" => "Q4o8MWBjywy9H4R5"
            "hfdhfdhf" => array:1 [
              "hfc" => array:6 [
                0 => "438"
                1 => "439"
                2 => "440"
                3 => "443"
                4 => "444"
                5 => "447"
              ]
            ]

Ajvanho's avatar
Level 14

@MohamedTammam For example, the key ‘hfdhfdhf’ appears in several places, but not in a clear sequence. I need all those key value pairs with that key.

tykus's avatar
collect($array)->flatMap(fn ($val) => $val)->all();
2 likes
jlrdw's avatar

@Ajvanho try an array flatten routine, example:

    public function array_flatten($a, $flat = []) {
        $entry = [];
        foreach ($a as $key => $el) {
            if (is_array($el)) {
                $flat = $this->array_flatten($el, $flat);
            } else {
                $entry[$key] = $el;
            }
        }
        if (!empty($entry)) {
            $flat[] = $entry;
        }
        return $flat;
    }
2 likes
Snapey's avatar

it would help you if you actually considered what you asked for

For example, the key ‘hfdhfdhf’ appears in several places

no it doesn't. You are not confused with hfdhf, or hfdhdf or fhdhdhf are you?

1 like

Please or to participate in this conversation.