Separate columns from a multidimensional array

1

In the form I have INPUT array name=INPUT_1[].. that are dynamic, cloned added other values. In the example below each INPUT has 2 values, plus there may be several!

Note: PHP 5.3.8

When submitting the form I have the Array.

Array (
    [INPUT_1] => Array
        (
            [0] => 74
            [1] => 69
        )

    [INPUT_2] => Array
        (
            [0] => 45
            [1] => 1
        )

    [INPUT_3] => Array
        (
            [0] => 5
            [1] => 2
        )

    [INPUT_4] => Array
        (
            [0] => 88
            [1] => 3
        )

    [INPUT_5] => Array
        (
            [0] => 123.389,89
            [1] => 12,33
        )
)

In my PHP, I'd like to treat the array above for:

Array (
    [INPUT_1] => 74,
    [INPUT_2] => 45,
    [INPUT_3] => 5,
    [INPUT_4] => 88,
    [INPUT_5] => 123.389,89
)

Array (
    [INPUT_1] => 69,
    [INPUT_2] => 1,
    [INPUT_3] => 2,
    [INPUT_4] => 3,
    [INPUT_5] => 12,33
)

Attempt

I'm trying to:

$array = array(
    'INPUT_1' => array(111, 112, 113),
    'INPUT_2' => array(222, 223, 224),
    'INPUT_3' => array(333, 334, 335),
    'INPUT_4' => array(444, 445, 446),
    'INPUT_5' => array(555, 556, 557)
);

$out = array();
foreach($array as $nome => $arr) {
    foreach($arr as $key => $value) {
        $out[$nome] = $value;
    }
}

but you're only getting the last values of each array:

Array
(
    [INPUT_1] => 113
    [INPUT_2] => 224
    [INPUT_3] => 335
    [INPUT_4] => 446
    [INPUT_5] => 557
)
    
asked by anonymous 17.05.2017 / 15:56

4 answers

2

Try this:

<?php
/*EXEMPLO DO ARRAY DE INPUT*/
$input = array('INPUT_1'=>array(74,69),'INPUT_2'=>array(45,1));
print_r($input);

$a = array();//Array que conterá os valores do índice 0
$b = array();//Array que conterá os valores do índice 1
//percorre o array INPUT
foreach($input as $key => $in){
    //colocar o primeiro valor no array $a com a chave do array principal
    $a[$key] = $in[0];
    //colocar o segundo valor no array $b com a chave do array principal
    $b[$key] = $in[1];
}
//imprime
print_r($a);
print_r($b);

IDEONE Example

EDIT

If the array has dynamic number of indexes the solution is to create another output array with the values separated, for example:

<?php
$input = array('INPUT_1'=>array(74,69,189,155),'INPUT_2'=>array(45,1,10));
echo "****ENTRADA****".PHP_EOL;
print_r($input);

$out = array();

foreach($input as $key => $in){
    for($i = 0; $i < sizeof($in); $i++){
        $out[$key][$i] = $in[$i];
    }
}
echo "****SAÍDA****".PHP_EOL;
print_r($out);

EXAMPLE IDEONE 2

    
17.05.2017 / 16:10
0

One practical way I found was to use this way, see if it suits you:

<?php

$array = array(
                array('74','69'),
                array('45','1'),
                array('5','2'),
                array('88','3'),
                array('123.389,89', '12,33'),           );

$arrayA = array(); $arrayB = array();


foreach ($array as $key => $value) {    
    foreach ($value as $k => $v) {
        if($k == 0) {               
            array_push($arrayB, $v);        
        } 
        else {          
            array_push($arrayA, $v);        
        }   
    } 
}

var_dump('<pre>',$arrayA,$arrayB);

link

    
17.05.2017 / 16:11
0

The best way is to use the tools that language offers you. Remember that programming language allows you to avoid doing repetitive work. If you do, something does not agree. For example, PHP provides the array_column function that returns an array with the values present in a given column. So, considering the example:

$arr = [
  ['74','69'],
  ['45','1'],
  ['5','2'],
  ['88','3'],
  ['123.389,89', '12,33'],
];

Just do it:

array_column($arr, 0);

To get:

Array
(
    [0] => 74
    [1] => 45
    [2] => 5
    [3] => 88
    [4] => 123.389,89
)

E

array_column($arr, 1);

To get:

Array
(
    [0] => 69
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 12,33
)
  

See working at Ideone .

    
17.05.2017 / 16:20
0

As of PHP 5.5.0 there is the function array_column ()

Example usage, similar to your case:

$arr = array(
    array('74','69'),
    array('45','1'),
    array('5','2'),
    array('88','3')
);

//Pega todos do primeiro índice (0)
$a = array_column($arr, 0);
print_r($a);

//Pega todos do segundo índice (1)
$b = array_column($arr, 1);
print_r($b);

Remember that a smaller code is not always synonymous with better performance.

Also be aware that it is only available from version 5.5.0

Because there are still lower-level environments, we recommend avoiding new features if your project will be used in a variety of environments. Otherwise, you will have to create a conditional that checks the PHP version or if the function exists.

    
17.05.2017 / 16:27