Working with Array in PHP

Asked

Viewed 44 times

0

I would like to know how to proceed to achieve the result below in PHP.

The way out today is:

Array
(
    [0] => "0000000009999999"
    [1] => "20181209"
    [2] => "000000"
    [3] => "REM BASICA"
    [4] => "0.00"
    [5] => "C"

)
Array
(
    [0] => "0000000009999999"
    [1] => "20181209"
    [2] => "000000"
    [3] => "CRED JUROS"
    [4] => "0.02"
    [5] => "C"

)
Array
(
    [0] => "0000000009999999"
    [1] => "20181210"
    [2] => "102109"
    [3] => "CRED TEV"
    [4] => "70.00"
    [5] => "C"

)

What I need is:

[0]

    [Conta] => "0000000009999999"
    [Data] => "20181209"
    [Cod] => "000000"
    [Historico] => "REM BASICA"
    [Valor] => "0.00"
    [Op] => "C"

[1]

    [Conta] => "0000000009999999"
    [Data] => "20181209"
    [Cod] => "000000"
    [Historico] => "REM BASICA"
    [Valor] => "0.00"
    [Op] => "C"


[2]

    [Conta] => "0000000009999999"
    [Data] => "20181209"
    [Cod] => "000000"
    [Historico] => "REM BASICA"
    [Valor] => "0.00"
    [Op] => "C"

2 answers

5

Another way to get the same result is to use a foreach with a array_combine() this function create a new array where the first arguments defines the keys and the second the respective values, this done of positional ie the zero Indice of the key array must be the desired value of the value array.

<?php

$arr = array(
    array("0000000009999999","20181209","000000","REM BASICA","0.00","C"),
    array("0000000009999999","20181209","000000","CRED JUROS","0.02","C")
);

$chaves = array('Conta', 'Data', 'Cod', 'Historico', 'Valor', 'Op');

$novo = array();

foreach($arr as $item){
    $novo[] = array_combine($chaves, $item);
}

print_r($novo);

Example - ideone

Exit:

Array
(
    [0] => Array
        (
            [Conta] => 0000000009999999
            [Data] => 20181209
            [Cod] => 000000
            [Historico] => REM BASICA
            [Valor] => 0.00
            [Op] => C
        )

    [1] => Array
        (
            [Conta] => 0000000009999999
            [Data] => 20181209
            [Cod] => 000000
            [Historico] => CRED JUROS
            [Valor] => 0.02
            [Op] => C
        )

)
  • legal, this way complements more the answers. + 1

4

With a simple function it is easy to solve this problem, example:

<?php

$array = array(
    array("0000000009999999","20181209","000000","REM BASICA","0.00","C"),
    array("0000000009999999","20181209","000000","CRED JUROS","0.02","C")
);

function transforme($array) 
{
    $array_new = array();
    foreach($array as $values)
    {
        $array_copy = array();
        foreach ($values as $key => $value) 
        {
            switch ($key) {
                case 0:     
                    $array_copy['Conta'] = $value;  
                    break;
                case 1: 
                    $array_copy['Data'] = $value;           
                    break;
                case 2:             
                    $array_copy['Cod'] = $value;
                    break;
                case 3:             
                    $array_copy['Historico'] = $value;
                    break;
                case 4:             
                    $array_copy['Valor'] = $value;
                    break;
                case 5:             
                    $array_copy['Op'] = $value;
                    break;
            }
        }
        $array_new[] = $array_copy;
    }
    return $array_new;
}

print_r(transforme($array));

Ideone example

How it works:

Is a array is simple where each position is a starting number from 0 to 5, then each position is a field so specified and the logic goes like this until the last position of the array. It is worth remembering that roughly if all positions have the same layout, this script will work without problem.

This proposed form is as basic as possible, remembering that the PHP has other functions that can simplify the code, but, not clear (or readable), which is another fight in the world of developers.

Browser other questions tagged

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