Jump to content

Help with the dreaded multidimensional associative array extract


tcicatelli

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.