• Resolved davidkeatscroft

    (@davidkeatscroft)


    Hi Ali,

    We spoke on twitter earlier, but I thought I’d post here to make my issue a little clearer, hopefully you can help me !

    I’m using the update_user_meta to send a JSON array to a custom field against the user called wc_other_addresses.

    The output I’d like is:

    a:3:{i:1;a:9:{s:19:"shipping_first_name";s:6:"Darren";s:18:"shipping_last_name";s:11:"Keats-Croft";s:16:"shipping_company";s:28:"K3 Business Technology Group";s:16:"shipping_country";s:2:"GB";s:18:"shipping_address_1";s:15:"Baltimore House";s:18:"shipping_address_2";s:16:"50 Kansas Avenue";s:13:"shipping_city";s:7:"Salford";s:14:"shipping_state";s:18:"GREATER MANCHESTER";s:17:"shipping_postcode";s:7:"M50 2GL";}i:2;a:9:{s:19:"shipping_first_name";s:4:"John";s:18:"shipping_last_name";s:3:"Doe";s:16:"shipping_company";s:8:"Arcadium";s:16:"shipping_country";s:2:"GB";s:18:"shipping_address_1";s:17:"26 Arcadia Avenue";s:18:"shipping_address_2";s:10:"Kensington";s:13:"shipping_city";s:9:"Liverpool";s:14:"shipping_state";s:10:"Merseyside";s:17:"shipping_postcode";s:7:"L14 3AD";}i:3;a:9:{s:19:"shipping_first_name";s:5:"David";s:18:"shipping_last_name";s:11:"Keats-Croft";s:16:"shipping_company";s:4:"dmkc";s:16:"shipping_country";s:2:"DZ";s:18:"shipping_address_1";s:15:"Appt 1012 Abito";s:18:"shipping_address_2";s:15:"4 Clippers Quay";s:13:"shipping_city";s:7:"Salford";s:14:"shipping_state";s:18:"GREATER MANCHESTER";s:17:"shipping_postcode";s:6:"M503BS";}}

    which converted to JSON using an online converter looks like this:

    {
       "1":{
          "shipping_first_name":"Darren",
          "shipping_last_name":"Keats-Croft",
          "shipping_company":"K3 Business Technology Group",
          "shipping_country":"GB",
          "shipping_address_1":"Baltimore House",
          "shipping_address_2":"50 Kansas Avenue",
          "shipping_city":"Salford",
          "shipping_state":"GREATER MANCHESTER",
          "shipping_postcode":"M50 2GL"
       },
       "2":{
          "shipping_first_name":"John",
          "shipping_last_name":"Doe",
          "shipping_company":"Arcadium",
          "shipping_country":"GB",
          "shipping_address_1":"26 Arcadia Avenue",
          "shipping_address_2":"Kensington",
          "shipping_city":"Liverpool",
          "shipping_state":"Merseyside",
          "shipping_postcode":"L14 3AD"
       },
       "3":{
          "shipping_first_name":"David",
          "shipping_last_name":"Keats-Croft",
          "shipping_company":"dmkc",
          "shipping_country":"DZ",
          "shipping_address_1":"Appt 1012 Abito",
          "shipping_address_2":"4 Clippers Quay",
          "shipping_city":"Salford",
          "shipping_state":"GREATER MANCHESTER",
          "shipping_postcode":"M503BS"
       }
    }

    However, when trying to pass the above JSON via postman, I get the following in the db, which doesn’t work with the plugin:

    a:27:{i:0;s:29:"[{"shipping_first_name":"Tom"";i:1;s:34:""shipping_last_name":"Keats-Croft"";i:2;s:49:""shipping_company":"K3 Business Technology Group"";i:3;s:23:""shipping_country":"GB"";i:4;s:38:""shipping_address_1":"Baltimore House"";i:5;s:39:""shipping_address_2":"50 Kansas Avenue"";i:6;s:25:""shipping_city":"Salford"";i:7;s:37:""shipping_state":"GREATER MANCHESTER"";i:8;s:31:""shipping_postcode":"M50 2GL" }";i:9;s:30:"{ "shipping_first_name":"John"";i:10;s:26:""shipping_last_name":"Doe"";i:11;s:29:""shipping_company":"Arcadium"";i:12;s:23:""shipping_country":"GB"";i:13;s:40:""shipping_address_1":"26 Arcadia Avenue"";i:14;s:33:""shipping_address_2":"Kensington"";i:15;s:27:""shipping_city":"Liverpool"";i:16;s:29:""shipping_state":"Merseyside"";i:17;s:31:""shipping_postcode":"L14 3AD" }";i:18;s:31:"{ "shipping_first_name":"David"";i:19;s:34:""shipping_last_name":"Keats-Croft"";i:20;s:29:""shipping_company":"crw345tf"";i:21;s:23:""shipping_country":"DZ"";i:22;s:38:""shipping_address_1":"Appt 1012 Abito"";i:23;s:38:""shipping_address_2":"4 Clippers Quay"";i:24;s:25:""shipping_city":"Salford"";i:25;s:37:""shipping_state":"GREATER MANCHESTER"";i:26;s:31:""shipping_postcode":"M50 3BS"}]";}

    It seems that what’s happening is that the JSON is being combined into 1 array with 27 strings, rather than 1 array containing 3 sub-arrays with 9 strings each in.

    I’ve tried json_decode and also serialize, but can’t seem to get the correct result into the database.

    Please could you help me resolve this?

    https://wordpress.org/plugins/json-api-user/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Ali Qureshi

    (@parorrey)

    I think a better solution for you will be adding different custom addresses separately such as:

    ‘wc_other_address1’, ‘wc_other_address2’, ‘wc_other_address3’ as meta_key and insert its value as array.

    Then retrieve the value for each address using its meta_key.

    Does not that make sense?

    Thread Starter davidkeatscroft

    (@davidkeatscroft)

    Sorry about the delay in replying, I was on holiday !

    That would be a great idea yes, if I was in control of the plugin that uses the data, but sadly I’m not.

    It’s this one:

    https://www.woothemes.com/products/shipping-multiple-addresses/

    It’s looking for an array, so I need to provide it one really, in the format as shown above…. so I guess it’s back to the drawing board….

    Plugin Author Ali Qureshi

    (@parorrey)

    You can still use this logic.. Just fetch the values with each key, and combine the arrays at code level for this variable ‘wc_other_address’.

    Thread Starter davidkeatscroft

    (@davidkeatscroft)

    ah right, ok, I wouldn’t know where to even start with that but I’ll give it a go! Thanks 🙂

    Plugin Author Ali Qureshi

    (@parorrey)

    try this code in your wordpress php page, assuming you have data saved for each key wc_other_address1 and so on.

    $wc1 = get_user_meta( $user_id, 'wc_other_address1');
    $wc2 = get_user_meta( $user_id, 'wc_other_address2');
    $wc3 = get_user_meta( $user_id, 'wc_other_address3'); 
    
    $wc_other_address = array_combine($wc1, $wc2, $wc3);

    you can then use $wc_other_address with your other call.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘JSON combining arrays into single array’ is closed to new replies.