<?php $data = [ 'NUM' => [ 101, 102, 103, ], 'NOM' => [ 'Smith', 'Robson', 'Daniels', ], 'PRENOM' => [ 'Alice', 'Bob', 'Charlie', ], ]; // Grab all possible outer keys $outerKeys = array_keys($data); // Make sure all child arrays are the same size $innerCount = null; foreach($outerKeys as $key) { // Also make sure that we've got a list so that we don't have to keep // track of child keys. For a 7.x polyfill see // https://www.php.net/manual/en/function.array-is-list.php if(!array_is_list($data[$key])){ throw new RuntimeException('Child array is not a list'); } // Count the first item if(null === $innerCount){ $innerCount = count($data[$key]); continue; } // Compare the known count with the current child array if($innerCount !== count($data[$key])){ throw new RuntimeException('Child array does not have equal item count'); } } $lines = []; for($idx = 0; $idx < $innerCount; $idx++){ $buf = []; foreach($outerKeys as $key){ $buf[] = $key . ':' . $data[$key][$idx]; } $lines[] = implode(' ', $buf); } echo implode(PHP_EOL, $lines);
You have javascript disabled. You will not be able to edit any code.