How to feed an array using data from another array in php?

Asked

Viewed 88 times

0

Does not accept the foreach?

{        $ssparray = array(
            //Array numérico sendo a segunda dimensão.
            'mensagem' => array(
              'mensagem'                => 'OK - preventiva realizada',
              'usuario_api'             => 'userapi', 
              'relatorio_fotografico'   => 'rf', 
              'data_de_execucao_inicio' => $inicio,
              'data_de_execucao'        => $fim,
              'preventiva_id'           => $ordemservico->codigocliente,
            ),
            /*
            //Array Associativo sendo a segunda dimensão.
            foreach ($fotos as $foto) {
                'checklist' => array(
                    'id[i]'    => $foto->checklistitens_id,
                    'nome'  => $foto->checklistitens->descricao,
                    'valor' => $foto->situacao,
                )
            }
            */
}
  • This part of your code is incomplete and you can’t even analyze the problem. You are creating an array within another array already having its construction with a foreach. And I’m not sure, but I don’t think it works.

  • Yes, this is the problem. I’m not sure how to do it! A foreach inside an array does not work. I put it there to give you an idea of what I need but I’ll rephrase it. Thank you.

  • 1

    I’m going to make a structure a code the right way to do it, but I don’t know if it will suit you, because there are still snippets of your code.

  • 1

    Inside this sspaarray can have as many checklists?

  • Inside Checklist today has 28 items but I get from a table that can increase or decrease. Thanks. It cleared the way you did and yes, I’m studying right now how to do.

  • Now I understand [$i] what you put, I will make the adaptation.

  • If I put the return of Eloquent I can help with the code, with this code I only see pure PHP

Show 2 more comments

2 answers

1

Marcos, good afternoon as this using Eloquent, there are several ways to map an Array in Laravel and get the result you want, you can use the function map collecet(), since its return returned a Collect, Voce can create a new Collect and use the push method, iterating over its current array or as previously spoken with the map.

0


In the new way of making an array we use $var = [] instead of $var = array() I suggest you study the subject on PHP manual

Since your question is a little vague, I only made syntax corrections and I believe it will help.

//Array Associativo sendo a segunda dimensão.
$checklist = [];
foreach ($fotos as $foto)
{
    $checklist['id']    = $foto->checklistitens_id;
    $checklist['nome']  = $foto->checklistitens->descrica;
    $checklist['valor'] = $foto->situacao;
}

$ssparray = [
//Array numérico sendo a segunda dimensão.
'mensagem' => [
'mensagem'                => 'OK - preventiva realizada',
'usuario_api'             => 'userapi',
'relatorio_fotografico'   => 'rf',
'data_de_execucao_inicio' => $inicio,
'data_de_execucao'        => $fim,
'preventiva_id'           => $ordemservico->codigocliente,
],
'checklist' => $checklist
];
  • The result was: In the case "Checklist" => array:3 can have 28 items { array:2 [ "message" => array:6 [ "message" => "OK - preventive performed" "usuario_api" => "userapi" "reportario_fotografico" => "Rf" "data_de_execucao_start" => "2019-10-31 10:51:55" "data_de_execucao" => "2019-10-31 16:46:32" "preventiva_id" => null ] "Checklist" => array:3 [ "id" => 437 "name" => "OTHER ENVIRONMENTS (SPECIFY)" "value" => "C" ] ] ] }

  • 1

    It worked, that’s what I wanted?

  • I use eloquent to relate and pick as an array the service order data, Checklist and checklistitens like: { $fotos = Photo::with('checklistitens', 'ordemservico') ->orderby('checklistitens_id', 'asc') ->Where('ordemservico_id', "$ordemservico") ->get(); }

  • 1

    Let me get this straight. You take the values from the table photos and then use this result to bring values from another table (checklistitens), is that it? And it’s bringing the result you want?

  • Result (Laravel-eloquent) of table photos related to checklistitens. There are 29 checklistitens and these must be in the Checklist array you made. I had to cut into the code because of the size. { Collection {#668 #items: array:30 [ 0 => Photo {#634 #observables: [] #Relations: array:2 [ "checklistitens" => Checklistitens {#669 ... 26} "ordemservico" => Ordemservico {#632 ... 26} ] } 1 => Photo {#635 } 1 => Photo {#635 } 2 => Photo {#636 } 3 => Photo {#637 ▶}

  • Yes, it brings the result I need. I need to loop the Checklist array to mount with the total of checklistitens (There are 29).

  • Well if my answer helped, mark as answered

  • It did work. Thank you. .

Show 3 more comments

Browser other questions tagged

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