A
A
artwerty2017-10-09 00:18:55
PHP
artwerty, 2017-10-09 00:18:55

How to convert two dimensional array to one dimensional PHP?

Hello.
I've been breaking my head for a couple of hours, I just can't figure out how to solve a seemingly banal problem. I have this two dimensional array:

$arr = Array(
  0 => Array(
    0 => 'name 0',
    1 => 'name 1',
    2 => 'name 2'
  ),
  1 => Array(
    0 => 100,
    1 => 200,
    2 => 300
  )
);

I wanted to get something like this:
$arr = Array(
  0 => Array(
    0 => 'name 0',
    1 => 100,
  ),
  1 => Array(
    0 => 'name 1',
    1 => 200,
  ),
  2 => Array(
    0 => 'name 2'
    1 => 300
  )
);

Tell me, is this idea feasible using PHP? If so, can you write what tools to use to implement this idea?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaximMRX, 2017-10-09
@artwerty

Exactly the array you want

$arrOne = Array(
  0 => Array(
    0 => 'name 0',
    1 => 'name 1',
    2 => 'name 2'
  ),
  1 => Array(
    0 => 100,
    1 => 200,
    2 => 300
  )
);
$arrTwo = array();

foreach ($arrOne as $keys => $names) { 
  foreach ($names as $key => $name) {
    $arrTwo[$key][] = $name;
    continue;
  }
  

}

var_dump($arrTwo);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question