How to pass parameters to this SOAP function in PHP? - php

I have a SOAP endpoint, so i get the functions with $client->__getFunctions() an i get this.
array(3) {
[0]=>
string(143) "ResEds_ GETcotpaq_(string $sCid_, string $sCte_, string $sCor_, string $sCds_, string $xRec_, string $xEnt_, decimal $xVld_, EstCga_ $sDatDet_)"
[1]=>
string(61) "decimal CalcListaPrcFpl(decimal $Ps_, decimal $Vl_, int $Ln_)"
[2]=>
string(55) "decimal CalcListaPrcMult(string $xKgs_, decimal $xMts_)"
}
So i'm interested in the GETcotpaq_ function, so i'm trying to pass the parameters described in the SOAP function.
$params = [
'sCid_' => 'FA',
'sCor_' => '44250',
'sCds_' => '37000',
'xRec_' => 0,
'xEnt_' => 0,
'xVld_' => 100,
'sDatDet_' => [
'xCntEmp_' => 1,
'xPesUni_' => 10,
'xVolUni_' => 10
]
];
var_dump($client->GETcotpaq_($params));
But I get this error Message: Array to string conversion. How I should pass the parameters?

Related

Extract item from multi-dimensional array from custom depth

I have multidimensional array like this:
$obj = array(
"a" => array(
"aa" => array(
"aaa" => 1
),
"bb" => 2,
),
"b" => array(
"ba" => 3,
"bb" => 4,
),
"c" => array(
"ca" => 5,
"cb" => 6,
),
);
I can not figured out a neatest way, e.g. custom-depth function, to extract item at specific location with arguments to function (or array of key names). For example:
echo $obj[someFunc("a", "aa", "aaa")];
... should return 1.
print_r($obj[someFunc("a")]);
... should return:
Array
(
[aa] => Array
(
[aaa] => 1
)
[bb] => 2
)
What is the best way to accomplished this with php7 features?
Since PHP 5.6, ["Variadic functions"][1] have existed. These provide us with a nice simple to read way to collect arguments used in calling a function into a single array. For example:
function getValue(...$parts) {
var_dump($parts);
}
getValue('test', 'part');
Will output:
array(2) {
[0]=>
string(4) "test"
[1]=>
string(4) "part"
}
This was all possible before using built-in functions to get the parameters, but this is more readable.
You could also be a little more explicit with the argument types, but I'll leave that for you to figure out if necessary.
You next major challenge is to loop through the arguments. Something like this will produce the value that you desire.
function getValue(...$parts) {
$returnValue = $obj;
foreach ($parts as $part) {
$returnValue = $obj[$part];
}
return $returnValue;
}
However, this is rather crude code and will error when you try calling it to access non-existent parts. Have a play and fix those bits.
[1]: https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list

Clojure's partition-by in PHP?

coming from an FP background and now learning PHP I'm naturally missing some beloved higher-order functions. I was wondering if there's anything like Clojure's partition-by in PHP. Here's the official description of what it does:
(partition-by f)(partition-by f coll)
Applies f [a function] to each value in coll, splitting it each time f
returns a new value. Returns a lazy seq of partitions. Returns a
stateful transducer when no collection is provided.
I don't really care about the lazy and transducer parts (which I guess are out of the question in PHP anyway, am I right?) What I need to do is tranform an array like this :
[["year" => 2019, "val" => "foo"],
["year" => 2019, "val" => "bar"],
["year" => 2020, "val" => "baz"],
["year" => 2020, "val" => "boo)"]]
into this:
[[["year" => 2019, "val" => "foo"],
["year" => 2019, "val" => "bar"]],
[["year" => 2020, "val" => "baz"],
["year" => 2020, "val" => "boo)"]]]
i.e. I want to group them by the result of a function like this:
function getYear($pair)
{
return $pair["year"];
}
I haven't tried yet to code something like this up myself, but I was wondering if anything like this exists in a functional lib (or even the core?) of PHP.
I don't think this exists as a language feature. At least I haven't or couldn't find anything.
However, (unless I misunderstood) this should give you the result you described, with the caveat that the initial array must first be sorted by the partition value:
$f = [
["year" => 2019, "val" => "foo"],
["year" => 2019, "val" => "bar"],
["year" => 2020, "val" => "baz"],
["year" => 2020, "val" => "boo)"]
];
var_dump(partitionMultidim($f, "year"));
function partitionMultidim(array $values, string $key):array{
$new = [];
$p = $values[0][$key]; // alternatively check if exists in loop and set if not
$partitionNum = 0;
foreach($values as $value){
// what is the current value?
$curPartitionVal = $value[$key]; // or getYear($b);
// has the value changed?
if ( $p !== $curPartitionVal ){
// set the next partition value
$p = $curPartitionVal;
// new partition index
$partitionNum++;
}
// add to the new array
$new[$partitionNum][] = $value;
}
return $new;
}
This does not exist as a language feature. However, there are ready classes that do the same. Example:
$f = [
["year" => 2020, "val" => "baz"],
["year" => 2019, "val" => "foo"],
["year" => 2019, "val" => "bar"],
["year" => 2020, "val" => "boo)"]
];
//https://github.com/jspit-de/tableArray
$newArr = tableArray::create($f)
->orderBy('year ASC,val ASC')
->fetchGroup(['year'])
;
//test output
echo '<pre>';
var_dump($newArr);
Output:
array(2) {
[2019]=>
array(2) {
[0]=>
array(2) {
["year"]=>
int(2019)
["val"]=>
string(3) "foo"
}
[1]=>
array(2) {
["year"]=>
int(2019)
["val"]=>
string(3) "bar"
}
}
[2020]=>
array(2) {
[2]=>
array(2) {
["year"]=>
int(2020)
["val"]=>
string(4) "boo)"
}
[3]=>
array(2) {
["year"]=>
int(2020)
["val"]=>
string(3) "baz"
}
}
}

How do I get all the values of an array without having to manually access to each key?

I would like to know how I can get all the values of an array without having to manually access to each key to then get the value.
That is, I have this arrangement.
$user = [ "Id" => 123, "Name" => "Dave", "Age" => 25, "Country" => "US"];
I want to get the values 123, "Dave," 25, "US".
PHP has a function that does exactly what you need, which is array_values Return all the values of an array.
// e.g.
$user = [
"id" => 123,
"name" => "Dave",
"age" => 25,
"country" => "US"
];
print_r(array_values($user));
Result
Array
(
[0] => 123
[1] => Dave
[2] => 25
[3] => US
)
NOTE:
But beware of boolean values because if any value of the array is TRUE, array_values it will transform to 1 and if the value is FALSE will not return anything (it has the same behavior as the isset function)
YOu can get all the values without the keys and then use implode to make it into a single string
$array = array_values($user);
var_dump($array);
you will get
array(4) {
[0]=> int(123)
[1]=> string(4) "Dave"
[2]=> int(25)
[3]=> string(2) "US" }
If you want it as a string use
$comma_separated = implode(",", $user);
echo $comma_separated;
Result will be
123,Dave,25,US

Use array_splice() to enter empty values when the array has non-numeric keys [duplicate]

This question already has answers here:
array_splice() for associative arrays
(14 answers)
Closed 7 years ago.
I've taken a look at this question, but I can't seem to get the code to work for my purposes.
I have an array ($array) structured like so, full of data from a database:
array(369) {
array(3) {
["id"] => string(5) "12345",
["title"] => string(11) "Hello World",
["description"] => string(n) "..."
}
array(3) {
["id"] => string(5) "12346",
["title"] => string(13) "Goodbye World",
["description"] => string(n) "..."
}
...
}
However, this array data will be creating a CSV, and I need to insert empty columns as well. Therefore I need the array to end up looking like this:
array(369) {
array(5) {
["id"] => string(5) "12345",
["title"] => string(11) "Hello World",
["title2"] => string(0) "",
["description"] => string(n) "...",
["description2"] => string(0) ""
}
array(5) {
["id"] => string(5) "12346",
["title"] => string(13) "Goodbye World",
["title2"] => string(0) "",
["description"] => string(n) "...",
["description2"] => string(0) ""
}
...
}
I've tried using array_splice() to enter blank values at the relevant points:
array_splice($array, 2, 0, "");
array_splice($array, 4, 0, "");
But this ends up just breaking the array, and later code utlising fputcsv() doesn't even recognise it as an array. How can I enter these blank values?
foreach ($array as $value) {
fputcsv($fp, $value);
}
Please note: What the array key is labelled as does not matter. It can be as suggested above, blank, numeric, zero... all that's important is that I need a blank value.
$array = array_map(function (array $row) {
static $default = [
'id' => null,
'title' => null,
'title2' => null,
'description' => null,
'description2' => null
];
return array_merge($default, $row);
}, $array);
array_merge keeps the structure (keys and order) of the first array, but replaces any values whose keys match with the second array.

properly render an array of numeric values, not an array with strings

I have this code in my symfony controller:
$em=$this->getDoctrine()->getManager();
$queryIndex = $em->createQuery( 'SELECT g.index
FROM MySpaceMyBundle:Graphique g');
$result = $queryIndex->getArrayResult();
$arrayResult = array_map('current', $result);
var_dump($arrayResult);
return $this->render('MySpaceMyBundle:MyFolder:myTemplate.html.twig');
With my var_dump, I have this result:
array (size=10)
0 => string '1700.000' (length=8)
1 => string '1200.000' (length=8)
2 => string '1200.000' (length=8)
3 => string '1304.000' (length=8)
4 => string '1800.000' (length=8)
5 => string '2012.000' (length=8)
6 => string '2048.000' (length=8)
7 => string '1048.000' (length=8)
8 => string '3000.000' (length=8)
9 => string '5421.000' (length=8)
But for the obHighchartBundle (using highchart.js) the result I want is:
[1700,1200,1200,1304,1800,2012,2048,1048,3000,5421]
How can I proceed?
Note that I need to pass a numeric array (the values are decimal types in my database), not array with strings.
Like this?
$result = [];
foreach ($arrayResult as $value) {
$result[] = (int) $value
}
var_dump($result);
You can use a tiny tips like array_walk function to cast your values as float to prevent highchart issue. See the documentation for this function:
http://php.net/manual/fr/function.array-walk.php
Here an example of the tiny function :
<?php
function forceFloat (&$aItems) {
$aItems = (float) $aItems;
}
$arrayResult = array("1.00","4.55","39494");
var_dump($arrayResult);
array_walk($arrayResult, 'forceFloat');
var_dump($arrayResult);
?>
The output :
array(3) {
[0]=>
string(4) "1.00"
[1]=>
string(4) "4.55"
[2]=>
string(5) "39494"
}
array(3) {
[0]=>
float(1)
[1]=>
float(4.55)
[2]=>
float(39494)
}
Best Regards,
TGA

Categories