php - Combine Array with same values, but add another array dictionary into the combined array -
i have php code fetch mysql data:
while ($row = $stmt->fetch(pdo::fetch_assoc)) { // create array results $menuitems['menu_items'][] = array( 'item_id' => $row['item_id'], 'rest_id' => $row['rest_id'], 'item_name' => $row['item_name'], 'item_genre' => $row['item_genre'], 'item_price' => $row['item_price'], 'item_descript' => $row['item_descript'], 'wait_time' => $row['wait_time'], 'ingredients' => array_filter(array(// not display ingredients if null 'ingredient_id' => $row['ingredient_id'], 'ingredient_name' => $row['ingredient_name'], 'ingredient_price' => $row['ingredient_price'], 'ingredient_default' => $row['ingredient_default'] )) ); // print success or no error $menuitems['error'] .= ''; } // check if menu items exists if( $menuitems != null ) { echo json_encode($menuitems); print_r($menuitems); } else { echo json_encode(array('error' => 'there no menu items location')); }
this json output array, it's outputting same menu item each ingredient associated it:
array ( [menu_items] => array ( [0] => array ( [item_id] => 102 [rest_id] => 67 [item_name] => tilapia , rice [item_genre] => fish %26 seafood [item_price] => 9.50 [item_descript] => tilapia , rice [wait_time] => 45 [ingredients] => array ( [ingredient_id] => 1 [ingredient_name] => herbs [ingredient_price] => 0.50 ) ) [1] => array ( [item_id] => 102 [rest_id] => 67 [item_name] => tilapia , rice [item_genre] => fish %26 seafood [item_price] => 9.50 [item_descript] => tilapia , rice [wait_time] => 45 [ingredients] => array ( [ingredient_id] => 2 [ingredient_name] => lemon [ingredient_price] => 0.00 [ingredient_default] => 1 ) ) [2] => array ( [item_id] => 105 [rest_id] => 67 [item_name] => ninja roll [item_genre] => japanese [item_price] => 8.00 [item_descript] => sushi roll. 6-8 pieces. captain's orders! [wait_time] => 30 [ingredients] => array ( ) ) [3] => array ( [item_id] => 106 [rest_id] => 67 [item_name] => sushi [item_genre] => japanese [item_price] => 8.00 [item_descript] => menu item description (optional) [wait_time] => 30 [ingredients] => array ( ) ) ) [error] => )
i need following though. merge ingredients associated menu item
array ( [menu_items] => array ( [0] => array ( [item_id] => 102 [rest_id] => 67 [item_name] => tilapia , rice [item_genre] => fish %26 seafood [item_price] => 9.50 [item_descript] => tilapia , rice [wait_time] => 45 [ingredients] => array [0] => array ( [ingredient_id] => 1 [ingredient_name] => herbs [ingredient_price] => 0.50 ) [1] => array ( [ingredient_id] => 2 [ingredient_name] => lemon [ingredient_price] => 0.00 [ingredient_default] => 1 ) ) [1] => array ( [item_id] => 105 [rest_id] => 67 [item_name] => ninja roll [item_genre] => japanese [item_price] => 8.00 [item_descript] => sushi roll. 6-8 pieces. captain's orders! [wait_time] => 30 [ingredients] => array ( ) ) [2] => array ( [item_id] => 106 [rest_id] => 67 [item_name] => sushi [item_genre] => japanese [item_price] => 8.00 [item_descript] => menu item description (optional) [wait_time] => 30 [ingredients] => array ( ) ) ) [error] => )
how can create desired output array? confused , getting no this!
make use of associative arrays. when control index life becomes easier. true when dealing one-to-many database results, are.
for example (and i'm making assumptions/guesses model):
if ($stmt = $dbh->prepare($query)) { // initialise array results $menuitems = array(); if ( $stmt->execute(array($rest_id)) ) { while ($row = $stmt->fetchall(pdo::fetch_assoc)) { // have seen menu before? if not, add array if ( !isset($menuitems['menu_items'][$row['id']]) ) { $menuitems['menu_items'][$row['id']] = array(ingredients => array()); } // add ingredient. $menuitems['menu_items'][$row['id']]['ingredients'][$row['ingred_id']] = $row['ingred_id']; $menuitems['error'] .= ''; } if( $menuitems != null ) { echo json_encode($menuitems); //print_r($menuitems); } else { echo json_encode(array('error' => 'there no menu items location')); } } }
in example above create list of menus menu.id array key. each menu has value 'ingredients', likewise array. in example use ingred_id key , value. you'd modify code use ingred_id key plus array of other ingredient info value.
note depending on size of database might end being more efficient run multiple queries. now, you're pulling awful lot of redundant data each row.
Comments
Post a Comment