0
votes

After parsing some data I have this example 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 => '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',
  ),
)   

The first array isn't needed anymore. Arrays 1 - 3 I need to loop through and store the result in mySQL, however they need to be associated with each other ala array 0. So:

array1 0, array2 0, array3 0 all belong together array1 1, array2 1, array3 1 all belong together etc.

here's my code for accomplishing this:

//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];
        }
    }

unfortunately this isn't working. It's producing this output:

array (
  0 => 
  array (
    'amount' => '3 1/2',
    'size' => 'cup',
    'ingredient' => NULL,
  ),
  1 => 
  array (
    'amount' => '1/3',
    'size' => 'cup',
    'ingredient' => NULL,
  ),
  2 => 
  array (
    'amount' => '1/3',
    'size' => 'cup',
    'ingredient' => NULL,
  ),
  3 => 
  array (
    'amount' => '2',
    'size' => 'tablespoon',
    'ingredient' => NULL,
  ),
)

It's only producing 4 arrays and I need 8. count($matches) is = to 8, so it's running the first loop 8 times. I'm not sure where I'm going wrong. Any ideas? Help is much appreciated!

1

1 Answers

2
votes

replace your loop code by following code

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];
        }
    }
}