Jump to content

Archived

This topic is now archived and is closed to further replies.

tcicatelli

Help with the dreaded multidimensional associative array extract

Recommended Posts

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

Share this post


Link to post
Share on other sites
[code]
<?php
foreach ($data['laundry'] as $item => $itemdata) {
        $color = $itemdata['color'];
        $status = $itemdata['status'];
       
        echo $item, ' ', $color, ' ', $status, '<br>';
}
?>
[/code]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.