Item relocation in the array key

Asked

Viewed 93 times

1

Good afternoon! Although I have researched the subject and searched in the documentation, I have not yet been able to come up with a solution to the following problem.. I have an array brought from the bank with titles and items.

The array comes to me as follows:

$a[0] = [
    "titulo" => "titulo1",
    "item" => "item 1",
];
$a[1] = [
    "titulo" => "titulo 2",
    "item" => "item 2",
];
$a[2] = [
    "titulo" => "titulo 2",
    "item" => "item 3",
];

My goal is to transform this array into a new one, with the items already relocated, example:

$limpo[0] = [
    "titulo" => "titulo1",
    "item" => "item 1",
];
$limpo[1] = [
    "titulo" => "titulo 2",
    "item" => "item 2",
    "item" => "item 3,
];

That is, the titles will not repeat themselves.

So far, I’ve assembled the code as follows..

foreach ($a as $key => $value) {

    if($key>0){
        $comparacao = ($a[$key-1]["titulo"]);   
        echo($comparacao."<br>");
    }

    if($key==0){
    $limpo[$key] = [
        "titulo" => $value['titulo'],
        "item" => $value['item']
    ];
    }else if($value["titulo"] != $comparacao){
        $limpo[$key] = [
        "titulo" => $value['titulo'],
        "item" => $value['item']
    ];
    }
    else if($value["titulo"] == $comparacao){

        //array_push($limpo[($key-1)]['item'], $value['item']);

        $limpo[($key-1)] = [
        "titulo" => $value['titulo'],
        "item" => $value['item']
    ];
    }
}

However, the moment you fall in ($value["titulo"] == $comparacao), the "item 3" is allocated in place of item 2, being as follows:

$limpo[0] = [
    "titulo" => "titulo1",
    "item" => "item 1",
];
$limpo[1] = [
    "titulo" => "titulo 2",
    "item" => "item 3",
];

Is there any way to push in the previous position? The snippet with commented push is returning an error by indicating the previous position..

Hugs.

  • 1

    You cannot have two keys with the same name on the same level.

1 answer

1

You can use the title as the index of the array since they are unique. So it’s easy to test if the title already exists. If it exists add the item to an array, it creates the title. Ex.:

<?php

$limpo = [];

foreach ($array as $a) {
    $titulo = $a["titulo"];

    if (! isset($limpo[$titulo])) {
        $limpo[$titulo] = [
            "titulo" => $a["titulo"],
            "item" => [ $a["item"] ],  // cria uma array de items
        ];
    } else {
        $limpo[$titulo]["item"][] = $a["item"];  // adiciona item ao array
    }
}

/* print:

    [
    'titulo 1' => [
        'titulo' => 'titulo 1',
        'item' => ['item 1']
    ],
    'titulo 2' => [
        'titulo' => 'titulo 2',
        'item' => ['item 2', 'item 3']
    ]
]

*/

Code working...

  • Thank you very much! It worked perfectly within the class, but when trying to play in the view, I get the following error "expects Parameter 1 to be string, array Given", would it be necessary to convert to string? I thought about using something like json_encode but the return comes empty..

  • You will probably need to use a for to print this on the screen... If the answer helped you and you think it is the solution. You can mark it as right.

  • I already voted friend, but there was a warning about having less than 15 reputation points, apparently it was computed but will not be displayed. Thanks for the tips, big hug!

  • No problem.. I forget air reputation limitations.. I didn’t mean to be boring. I’m glad it helped :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.