tcicatelli Posted October 1, 2006 Share Posted October 1, 2006 Hi-I'm writing an application that extracts a ton of information from a client and pushes it to a server in an associative array. What I am looking to do, is be able to create a foreach loop to take the "laundry array" and extract "shirt", "red", "clean" and "pants", "blue", "dirty" as separate variables so I can insert them into a mysql table. I want to ignore all the client_info data because I take care of that separately, since I know there's always just one client, but I'm never sure of how many "laundry" items will be forwarded. Here's a sample data dump:array(2) { ["client_info"]=> array(2) { ["client_fname"]=> string(20) "John" ["client_lname"]=> string(20) "Smith" } ["laundry"]=> array(2) { ["Shirt"]=> array(2) { ["color"]=> string(15) "red" ["status"]=> string(5) "clean" } ["Pants"]=> array(2) { ["color"]=> string(15) "blue" ["status"]=> string(5) "dirty" } }}I'm hoping to be able to write:mysql_query("INSERT INTO Inventory (Item, Color, Status ) VALUES($item, $color, $status)")I hope I've explained this well enough.Thanks,-Tom Quote Link to comment Share on other sites More sharing options...
Barand Posted October 1, 2006 Share Posted October 1, 2006 [code]<?phpforeach ($data['laundry'] as $item => $itemdata) { $color = $itemdata['color']; $status = $itemdata['status']; echo $item, ' ', $color, ' ', $status, '<br>';}?>[/code] Quote Link to comment Share on other sites More sharing options...
sasa Posted October 1, 2006 Share Posted October 1, 2006 TRY [CODE]<?php$a = array("client_info" => array("client_fname" => "John", "client_lname" => "Smith"),"laundry" => array("Shirt" => array("color" => "red", "status" => "clean"), "Pants" => array("color" => "blue", "status" => "dirty") ));foreach ($a['laundry'] as $k => $v) { $b[] = "('$k', '{$v[color]}', '{$v[status]}')";}$b = implode(', ', $b);$sql = "INSERT INTO Inventory (Item, Color, Status ) VALUES {$b}";echo $sql;?>[/CODE] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.