user1628514
user1628514

Reputation: 3

PHP Multidimensional Array with multiple array with 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!

Upvotes: 0

Views: 1911

Answers (2)

ESP32
ESP32

Reputation: 8708

can you do this?

$my_array['arrayparams'] = array(
    array('Param' => array( 'Name' =>     'sending_key', 'Value' => 'blah',)), 
    array('Param' => array( 'Name' => 'sending_key2', 'Value' => '2',)),
);

Upvotes: 1

Mike J
Mike J

Reputation: 425

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']
];

Upvotes: 0

Related Questions