Separating columns from a multidimensional array

Asked

Viewed 1,139 times

1

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

Obs: 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 would like to treat the array above to:

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
)

Trying

Guys 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 only getting the last values of each array:

Array
(
    [INPUT_1] => 113
    [INPUT_2] => 224
    [INPUT_3] => 335
    [INPUT_4] => 446
    [INPUT_5] => 557
)

4 answers

2


Try it like 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 amount of indices the solution is to create another output array with separate values, 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);

IDEONE EXAMPLE 2

  • served no friend :(

  • @smigol What did not serve, what is the error? the array is different?

  • its example ta limited 2 columns, the values within the INPUT array is dynamic, there may be several

  • @smigol see if the issue meets.

  • 1

    now it got show! Thank you old man.

0

One practical way I found was to use it 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);

https://ideone.com/scZwIu

  • your example ta limited to 2 values, there may be several, is dynamic inclusion

  • @smigol is true, but he is a starting point for his solution

  • @smigol another thing that is very important to emphasize, by what I saw this information about being "dynamic" was inserted after the answer where we had no way to predict this and not necessarily you will find the complete answer here and yes a basis to get in it

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 function array_column that returns a array with the values present in a given column. Then, consider the example:

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

Just do:

array_column($arr, 0);

To get:

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

And

array_column($arr, 1);

To get:

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

See working on Ideone.

  • This I had not yet used very good solution

  • The system has php 5.3.8

0

From PHP 5.5.0 there is the function array_column()

Example of use, 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 not always a smaller code is synonymous with better performance.

Also be aware that it is only available from version 5.5.0

As there are still environments with lower version, it is recommended to avoid new functions if your project will be used in varied environments. Otherwise, you will have to create a condition that checks the PHP version or if the function exists.

  • The php system is 5.3.8

Browser other questions tagged

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