0

I created a custom user meat value and want to add a new key/value to this array. But somehow I can't get it to work correctly, I always get a new array in side the array so that I always have e new dimension. But I want to keep just one dimension and add the new key/value pair.

I know this is nothing complicated but somehow I can't get it to work. I would appreciate any help.

my entry script:

$invoice_meta = get_user_meta( $_GET['id'], 'invoices');
$random = substr(number_format(time() * rand(),0,'',''),0,5);
if(empty($invoice_meta)) {
    $invoice_array = array();
    $invoice_array[date('d.m.Y').'_'.$random] = $pdf_save_path.$pdf_name;
    add_user_meta( $_GET['id'], 'invoices', $invoice_array);
} else {
    $invoice_array = $invoice_meta;
    $invoice_array[date('d.m.Y').'_'.$random] = $pdf_save_path.$pdf_name;
    update_user_meta( $_GET['id'], 'invoices', $invoice_array );
}

The first entry var_dump looks like this:

array(1) {
  [0]=>
  array(1) {
    ["07.05.2013_51584"]=>
    string(69) "user-data/26/rechnung/tierportal-d_07-05-2013_587543369Y48693K_26.pdf"
  }
}

after second one it looks like this:

array(1) {
  [0]=>
  array(2) {
    [0]=>
    array(1) {
      ["07.05.2013_51584"]=>
      string(69) "user-data/26/rechnung/tierportal-d_07-05-2013_5875448693K_26.pdf"
    }
    ["07.05.2013_37016"]=>
    string(69) "user-data/26/rechnung/tierportal-d_07-05-2013_58754348693K_26.pdf"
  }
}

But I want to look it like this

array(1) {
  [0]=>
  array(2) {
    ["07.05.2013_51584"]=>
    string(69) "user-data/26/rechnung/tierportal-d_07-05-2013_587543369Y48693K_26.pdf",
    ["07.05.2013_51534"]=>
    string(69) "user-data/26/rechnung/tierportal-d_07-05-2013_587543367Y48693K_26.pdf"
  }
}
0

You need to pass get_user_meta a third parameter to prevent it from returning an array. That is where the mistake is, not with the add/update functions.

$invoice_meta = get_user_meta( $_GET['id'], 'invoices', true);

You will need to clean up any data already in the database or the existing nested arrays will still cause trouble.

  • Thx a lot!! If I had more reputation I would upvote your answer.. – Sebsemillia May 7 '13 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.