简体   繁体   中英

PHP add new data to multidimensional array

I'm a newbie to multidimensional array's

I have a form that stores data into an array.
I would like my users to re-use the form and store data to the array.
So my idea was a multidimensional array that stores a new array everytime the form is used.

But my problem is that I have no idea how to do this.

Here is my form:

            $customer = '';
            $customer .= '<tr><td>customername:<br/><input type="text" name="customer[customername]" value="" /> </td></tr>';
            $customer .= '<tr><td>customertitle 1:<br/><input type="text" name="customer[customertitle1]" value="" /> </td></tr>';
            $customer .= '<tr><td>customeremail 1:<br/><input type="text" name="customer[customeremail1]" value="" /> </td></tr>';
            $customer .= '<tr><td>customertitle 2:<br/><input type="text" name="customer[customertitle2]" value="" /> </td></tr>';
            $customer .= '<tr><td>customeremail 2:<br/><input type="text" name="customer[customeremail2]" value="" /> </td></tr>';
            echo $customer;

This saves the form in an array:

if(isset($_POST['Submit'])) {
$customer = $_POST['customer'];

And this shows the first value of the array:

        $customers = array(get_option('customer'));
        foreach($customers as $customer){
          echo $customer["customername"];
        }           

I hope this makes sence to anyone!!!!

Yes figured it out

Here goes for the ones that wish to know: First create a function to get the current array. Save the new value from the form with update_option

function savearray (){
if(isset($_POST['Submit'])) {
// get the option
$customers = get_option( 'customers' );
// add new data to the option
$customers[] = $_POST['customers'];
// save it back to the db
update_option( 'customers', $customers );
}

Then create a form that places data in arrays by name

<?php 
$customers = '';
$customers .= '<tr><td>Name:<br/><input type="text" name="customers[name]" value="" /> </td></tr>';
$customers .= '<tr><td>Contact 1:<br/><input type="text" name="customers[contact1]" value="" /> </td></tr>';
echo $customers;
?>

So this works.... Now the next step.

I want to only show the name of the costumer and create a link to the data of the specific costumer. So I did this:

 <?php      
$customers = get_option('customers');
foreach($customers as $val){
  echo '<a href="#">'.$val["name"] . '</a><br>';
}           
?>  

This tells me that I want to view the name of all costumers in tha array and create a link. I have no idea how to target the specific data in the array.

Anyone?????

I think you need to use ArrayObject

Ex:

<?php
 // add a new consumer in your new array  
   $consumer["name"] = "John Doe";
   $consumer["email"] = "toto@yopmail.com";
   ...
   $a = new ArrayObject();
   $a->append($consumer)


   // for get a consumer in the array
    foreach ($arr as $key => $value)
    {
          // Some instruction
    }
?>

Hope it will help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM