2

I got a array of array in PHP that look like this :

array (size=3)
  0 => 
    array (size=3)
      0 => string 'abc' (length=3)
      1 => string 'def' (length=3)
      2 => string 'ghi' (length=3)
  1 => 
    array (size=3)
      0 => string '01234' (length=5)
      1 => string '01234' (length=5)
      2 => string '01234' (length=5)
  2 => 
    array (size=3)
      0 => string '98765' (length=5)
      1 => string '98765' (length=5)
      2 => string '98765' (length=5)

Now I want the first array to be the key of a assosiative array for the rest of the parent array, or kind :

array (size=2)
  0 => 
    array (size=3)
      'abc' => string '01234' (length=5)
      'def' => string '01234' (length=5)
      'ghi' => string '01234' (length=5)
  1 => 
    array (size=3)
      'abc' => string '98765' (length=5)
      'def' => string '98765' (length=5)
      'ghi' => string '98765' (length=5)

EDIT: But I can only get the first array like this to define the header :

$header = reset($tabOfTabs);

2
  • Have you tried something to get to your goal? Commented Mar 7, 2016 at 11:35
  • Yes I can get the first array, to define a variable that contains the "header" : $header = reset($tabOfTabs); Commented Mar 7, 2016 at 11:36

1 Answer 1

2

You can try this -

$indexes = array_shift($your_array); // pop out the first array to set the indexes

foreach($your_array as $key => $array) {
    $your_array[$key] = array_combine($indexes, $array); // combine the keys & sub-arrays
} 

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

It works thanks but I got another tab and only the header and the first line of data have the same size, and the rest got (size - 1), I get this error and try to solve it with array_pad : Warning: array_combine(): Both parameters should have an equal number of elements
In that case you can remove one from indexes or add an empty value to sub arrays.
Thanks I succeed by try catching the exeption and a call to array_pop($indexes);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.