Php 在多维数组中循环并一次从每个数组中取出数据1

Php 在多维数组中循环并一次从每个数组中取出数据1,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,解析一些数据后,我得到了以下示例数组: array ( 0 => array ( 0 => '3 1/2 cups peeled and diced potatoes', 1 => '1/3 cup diced celery', 2 => '1/3 cup finely chopped onion', 3 => '2 tablespoons chicken bouillon granules', 4 =>

解析一些数据后,我得到了以下示例数组:

    array (
  0 => 
  array (
    0 => '3 1/2 cups peeled and diced potatoes',
    1 => '1/3 cup diced celery',
    2 => '1/3 cup finely chopped onion',
    3 => '2 tablespoons chicken bouillon granules',
    4 => '3 1/2 cups peeled and diced potatoes',
    5 => '1/3 cup diced celery',
    6 => '1/3 cup finely chopped onion',
    7 => '2 tablespoons chicken bouillon granules',
  ),
  1 => 
  array (
    0 => '3 1/2',
    1 => '1/3',
    2 => '1/3',
    3 => '2',
    4 => '3 1/2',
    5 => '1/3',
    6 => '1/3',
    7 => '2',
  ),
  2 => 
  array (
    0 => 'cup',
    1 => 'cup',
    2 => 'cup',
    3 => 'tablespoon',
    4 => 'cup',
    5 => 'cup',
    6 => 'cup',
    7 => 'tablespoon',
  ),
  3 => 
  array (
    0 => 'peeled and diced potatoes',
    1 => 'diced celery',
    2 => 'finely chopped onion',
    3 => 'chicken bouillon granules',
    4 => 'peeled and diced potatoes',
    5 => 'diced celery',
    6 => 'finely chopped onion',
    7 => 'chicken bouillon granules',
  ),
)   
不再需要第一个阵列。数组1-3我需要循环并将结果存储在mySQL中,但是它们需要与数组0相互关联。因此:

数组1 0、数组2 0、数组3 0都属于一个整体 数组1 1、数组2 1、数组3 1都属于一个整体 等等

以下是我完成此任务的代码:

//make sure there were matches found and if there were, organize the array
if(!empty($matches)) {
    $info_array = array();
    for ($i = 0; $i < count($matches); $i++) {
        for ($j = 1; $j < count($matches[$i]); $j++) {
            if ($j == 1) {
                $key = 'amount';
            }
            elseif ($j == 2) {
                $key = 'size';
            }
            elseif ($j == 3) {
                $key = 'ingredient';
            }
            $info_array[$i][$key] = $matches[$j][$i];
        }
    }

它只生产4个阵列,我需要8个。count($matches)是=到8,因此它运行第一个循环8次。我不确定我错在哪里。有什么想法吗?非常感谢您的帮助

用以下代码替换循环代码

if(!empty($matches)) {
    $info_array = array();
    $total = count($matches[0]);
    for ($i = 0; $i < $total; $i++) {
        for ($j = 1; $j < count($matches); $j++) {
            if ($j == 1) {
               $key = 'amount';
            }
            elseif ($j == 2) {
                $key = 'size';
            }
            elseif ($j == 3) {
                $key = 'ingredient';
            }
            $info_array[$i][$key] = $matches[$j][$i];
        }
    }
}
if(!empty($matches)){
$info_array=array();
$total=计数($matches[0]);
对于($i=0;$i<$total;$i++){
对于($j=1;$j
if(!empty($matches)) {
    $info_array = array();
    $total = count($matches[0]);
    for ($i = 0; $i < $total; $i++) {
        for ($j = 1; $j < count($matches); $j++) {
            if ($j == 1) {
               $key = 'amount';
            }
            elseif ($j == 2) {
                $key = 'size';
            }
            elseif ($j == 3) {
                $key = 'ingredient';
            }
            $info_array[$i][$key] = $matches[$j][$i];
        }
    }
}