Working with Array in PHP

0

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

The output I have 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"
    
asked by anonymous 24.12.2018 / 00:19

2 answers

5

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

<?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

Output:

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
        )

)
    
24.12.2018 / 01:24
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:

It is a array is simple where each position is a number starting from 0 to 5, so each position is a field so specified and the logic goes like this until the last position of array ends. Note 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 PHP has other functions that can simplify the code, but does not make it clear (or readable) that it is another in>.

    
24.12.2018 / 01:17