P
P
PP2014-08-24 12:43:35
css
PP, 2014-08-24 12:43:35

How to split a multidimensional array into many one-dimensional ones?

In general, at the input I get an array of this kind

Array (
  [0] => Array (
      [0] => Вася
      [1] => Петя
      [2] => Саня
  )
  [1] => Array (
      [0] => 16
      [1] => 25
      [2] => 55
  )
  [2] => Array (
      [0] => школьник
      [1] => программист
      [2] => инженер
  )
)

moreover, it is not known how many elements they will contain (now in the example there are three, and maybe more)
, you need to get arrays of this type
Array (
  [0] => Вася
  [1] => 16
  [2] => школьник
)
Array (
  [0] => Петя
  [1] => 25
  [2] => программист
)
Array (
  [0] => Саня
  [1] => 55
  [2] => инженер
)

Answer the question

In order to leave comments, you need to log in

6 answer(s)
S
SagePtr, 2016-07-09
@Everot

Something like this? codepen.io/anon/pen/EywNPV

D
drtvader, 2016-07-09
@drtvader

On flex-box https://jsfiddle.net/drtvader/e47cdxzx/1/

A
A person from Kazakhstan, 2016-07-09
@LenovoId

and what are the difficulties? do for you?
float:left; and that's it
codepen.io/Geyan/pen/PzJbzZ?editors=110 then train yourself

B
Boris Yakushev, 2016-07-09
@za4me

Flood.
What is difficult in this layout?
Absolutely simple layout.

I
Ilya Rostovtsev, 2014-08-24
@Kadi

Just try to save subarrays using variable variables.
Your array:

$arrays = Array (
  '0' => Array (
      '0' => 'Вася',
      '1' => 'Петя',
      '2' => 'Саня'
  ),
  '1' => Array (
      '0' => '16',
      '1' => '25',
      '2' => '55'
  ),
  '2' => Array (
      '0' => 'школьник',
      '1' => 'программист',
      '2' => 'инженер'
  )
);

Cycle code:
foreach ($arrays as $i =>$array) {
    ${"array{$i}"} = $array; 
}

As a result, in this case, 3 variables will be formed: $array0, $array1, $array2 , which will contain your subarrays of the original multidimensional array.
Result:
Array
(
    [0] => Вася
    [1] => Петя
    [2] => Саня
)
Array
(
    [0] => 16
    [1] => 25
    [2] => 55
)
Array
(
    [0] => школьник
    [1] => программист
    [2] => инженер
)

Online example

J
jlgfmhldf, 2014-08-24
@jlgfmhldf

Overwrite with a loop into separate arrays.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question