Monday 3 September 2018

PHP multidimensional array with multiple arrays with the same keys?

I've been trying multiple things and for the life of me can not get this to work. I'm beginning to think it maybe isn't possible at this point.

So I have a SOAP API I'm sending this array too. Below is the code I currently have that works, but does not send the multiple values. It just uses the last one as it overwrite the previous.
Looking at this thread, what I'm doing should work?
$my_array['sn'] = "234234232";
$my_array['arrayparams'] = array(
'Param' => array( 'Name' =>     'sending_key', 'Value' => 'blah',),
'Param' => array( 'Name' => 'sending_key2', 'Value' => '2',),
);
$my_array['push'] = true;
$my_array['endsession'] = false;

returns:
array(4) {
  ["sn"]=>
  string(12) "234234232"
  ["arrayparams"]=>
  array(1) {
    ["Param"]=>
    array(2) {
      ["Name"]=>
      string(61) "sending_key2"
      ["Value"]=>
      string(1) "2"
    }
  }
  ["push"]=>
  bool(true)
  ["endsession"]=>
  bool(false)
}

I'm just having a time getting it to send this instead:
array(4) {
  ["sn"]=>
  string(12) "234234232"
  ["arrayparams"]=>
  array(2) {
    ["Param"]=>
    array(2) {
      ["Name"]=>
      string(61) "sending_key"
      ["Value"]=>
      string(1) "blah"
    }
    ["Param"]=>
    array(2) {
      ["Name"]=>
      string(61) "sending_key2"
      ["Value"]=>
      string(1) "2"
    }
  }
  ["push"]=>
  bool(true)
  ["endsession"]=>
  bool(false)
}

The 'Param' array is very strict and has to have this value, I can not change to 'Param2' to get it to work. Thanks in advanced!

The problem is you can't have the key 'Param' set in more than one key.
You would need to define 'Param' as an actual array, instead of as multiple keys within in array.
like so...
$my_array['Param'] = [
    ['Name' => 'sending_key', 'Value' => 'blah'],
    ['Name' => 'sending_key2', 'Value' => '2']
];

0 comments:

Post a Comment