currently i am using this
$big_test = array(
'username' => 'test',
'password' => 'test'
);
$test = json_encode($big_test);
echo $test;
the result is :
{"username":"test","password":"test"}
But I data to be wrap inside results,maybe liek this
{"Results":[{"username":"test","password":"test"}]}
and what about to create as result array :
$big_test = array( "Results" => array ( array(
'username' => 'test',
'password' => 'test'
)));
$test = json_encode($big_test);
echo $test;
you get same thing what you put there
Simple:
$array = array("Results" => array('username' => 'test','password' => 'test'));
Related
I am using phpdocx to generate an array with my data in a docx format.
$contact is an array of multiple object. Sometimes $contact contain 1 object, and sometimes more.
I want to make a loop, to add contact as much as I need.
My problem : For exemple, If I am doing this I will get an error like "Undefined array key 3" if my contact data only contain 3 object or less.
important : Here, if my datas contain 4 object (from 0 to 3 ) it will work but doesn't work when i have 2 objects.
$contact= array(
array(
'name' => $request->get('contact')[0]['name'],
'userName' => $request->get('contact')[0]['userName'],
'number' => $request->get('contact')[0]['number'],
'mail' => $request->get('contact')[0]['mail'],
),
array(
'name' => $request->get('contact')[1]['name'],
'userName' => $request->get('contact')[1]['userName'],
'number' => $request->get('contact')[1]['number'],
'mail' => $request->get('contact')[1]['mail'],
),
array(
'name' => $request->get('contact')[2]['name'],
'userName' => $request->get('contact')[2]['userName'],
'number' => $request->get('contact')[2]['number'],
'mail' => $request->get('contact')[2]['mail'],
),
array(
'name' => $request->get('contact')[3]['name'],
'userName' => $request->get('contact')[3]['userName'],
'number' => $request->get('contact')[3]['number'],
'mail' => $request->get('contact')[3]['mail'],
),
);
$docx->replaceTableVariable($contact, array('parseLineBreaks' => true));
what i am actually trying with no success for the moment : https://www.phpdocx.com/en/forum/default/topic/1773
I have make a loop and it is now working
$contactData = $request->get('contact');
$contactTab = array();
for ($i = 0; $i < count($contactData); $i++) {
$rowData = array(
'name' => $contactData[$i]['name'],
'userName' => $contactData[$i]['userName'],
'number' => $contactData[$i]['number'],
'mail' => $contactData[$i]['mail'],
);
$contactTab[] = $rowData;
}
$docx->replaceTableVariable($contactTab, array('parseLineBreaks' => true));
I am trying to use ConditionExpression when inserting an item on the database, but it dont work, the php script breaks when the Putitem() function runs.
I want to insert the item if he dont exist.
$response = $client->putItem(array(
'TableName' => 'tablename',
'Item' => array(
'serialNumber' => array('S' => 'test123'),
'deviceType' => array('S' => '1')
),
'ConditionExpression' => 'attribute_not_exists(serialNumber)'
));
I tried to var_dump the $response but the code breaks on the function above.
serialNumber its a Partition Key which should work as intended.
The code below works fine, but he replaces the existing item with new values, which its what i dont want to happen.
$response = $client->putItem(array(
'TableName' => 'tablename',
'Item' => array(
'serialNumber' => array('S' => 'test123'),
'deviceType' => array('S' => '1')
)
));
It is expected that you are returned a CondidtionCheckFailedException when the condition you set evaluates to false. Try wrapping your code in a try/catch block to see if it works as expected?
try {
$response = $client->putItem(array(
'TableName' => 'tablename',
'Item' => array(
'serialNumber' => array('S' => 'test123'),
'deviceType' => array('S' => '1')
)
));
}
catch(Exception $e) {
echo $e->getMessage();
}
I have an txt file where I output the variable using the command :
$h = fopen('log.txt', 'w');
fwrite($h, var_export($my_var, true));
fclose($h);
The content of log.txt looks similar to this :
array (
'IMType' => '1',
'Email' => 'test#gmail.com',
'SignupName' => 'test11',
'Password' => '11111',
'Encrypted' => '',
'Confirm' => '11111',
'OldPassword' => '',
'Name' => 'test',
'SignupProvinceText' => 'province',
'SignupCity' => 'cityname',
'Street' => 'street x.y',
'SignupIndustry' => 'IT',
'SignupCompany' => 'jobirn',
'SignupJt' => 'engineer',
'CellPhoneNum' => '',
'linked_in' => '',
)
Now, I would like to read this file again (later) and convert into an associative array ?
Any idea how this can be done ?
I would recommend using serialize() and unserialize() or json_encode() and json_decode() for this task. They are more appropriate for storing data.
file_put_contents('path/to/your_file.txt', serialize($my_var));
and
$my_var = unserialize(file_get_contents('path/to/your_file.txt'));
Or for the json version:
file_put_contents('path/to/your_file.txt', json_encode($my_var));
and
$my_var = json_decode(file_get_contents('path/to/your_file.txt'), true);
I have an array to store the configuration like this
$config = array (
'db' => array(
'db1' => array(
'dbname' => 'mydatabase',
'user' => 'myusername',
'pass' => 'mypassword',
'host' => 'myhost'
)
),
'url' => array(
'homeUrl' => 'http://www.example.com'
)
)
And I'm writing a function to retrieve the data from the array by passing in a string like db.db1.dbname and it supposes to give me 'mydatabase'
I tried to explode the string into an array in order to get the keys, db, db1, and dbname. but after that, I got kinda stuck on how exactly I'm supposed to use them like $config -> db -> db1 -> dbname or $config['db']['db1']['dbname'] in order to get 'mydatabase'.
Ideally, say I have the function named read($arg, $array), and I would like to retrieve results like this
read('db.db1.dbname', $config), returns 'mydatabase'
read('url.homeUrl', $config), returns 'http://www.example.com'
Since I don't know how many keys are contained in the string, I need this to be more dynamic.
Thanks in advance
I think you're thinking about JSON
try $config['db']['db1']['dbname'];
I'm really wondering why you would want to do this, but here it goes:
function read($item, $config)
{
$selectors = explode('.', $item);
$configItem = $config;
foreach($selectors as $selector) {
$configItem = $configItem[$selector];
}
return $configItem;
}
$config = array (
'db' => array(
'db1' => array(
'dbname' => 'mydatabase',
'user' => 'myusername',
'pass' => 'mypassword',
'host' => 'myhost',
),
),
'url' => array(
'homeUrl' => 'http://www.example.com',
),
);
read('db.db1.dbname', $config); // will return mydatabase
Note that you would have to check whether the keys exists and throw an error or exception if that's not the case.
Instead of using the function is there a reason why you cannot do:
function functionThatNeedsDatabaseInfo($databaseInfo)
{
// do database stuff
}
functionThatNeedsDatabaseInfo($config['db']['db1']);
They are just plain nested arrays.
$config['db'] will give you the first inner array
$config['db']['db1'] will give you the array with all the database configurations you're looking for.
$config['db']['db1']['dbname']: 'dbname' is the index in the 'db1' array that will give you the value you want.
function read($layers, $arr){
$toReturn = $arr;
foreach(split('.', $layers) as $layer)
$toReturn = $toReturn[$layer];
return $toReturn;
}
Why would you want to do this, I haven't the foggiest. But there you go.
Cast that thing to an Object! Or rather, cast every array as an object, then you can use it like one.
$config = (object) array (
(object) 'db' => array(
(object) 'db1' => array(
'dbname' => 'mydatabase',
'user' => 'myusername',
'pass' => 'mypassword',
'host' => 'myhost'
)
),
(object) 'url' => array(
'homeUrl' => 'http://www.example.com'
)
)
I'm building a simple WebService. I have a User PHP class and a webmethod getUser to retrieve a user's information.
I've declared a complexType and everything seems to work perfectly.
$server->wsdl->addComplexType('User',
'complexType','struct', 'all', '',
array( 'id' => array('name' => 'id', 'type' => 'xsd:int'),
'username' => array('name' => 'username','type' => 'xsd:string'),
'password' => array('name' => 'password','type' => 'xsd:string'),
'email' => array('name' => 'email','type' => 'xsd:string'),
'authority' => array('name' => 'authority','type' => 'xsd:int'),
'isActive' => array('name' => 'isActive','type' => 'xsd:int'),
'area' => array('name' => 'area','type' => 'xsd:string')
)
);
$server->register('ws_getUser',
array('user_id' => 'xsd:integer'),
array('user' => 'tns:User'),
$namespace,
"$namespace#ws_getUser",
'rpc',
'encoded',
'Retorna un usuario'
);
function ws_getUser($user_id){
return new soapval('return', 'tns:User', getUser($user_id));
}
However, on the getUser function I'm retrieving the user info as an assoc. array, not the User Object itself.
What I would like to do on getUser is to return a User instance instead, and have nuSOAP serialize it for me. Is this possible?
Edit: I've tried returning a new User() for testing purposes, but the response is
<user xsi:type="tns:User"/>
Guess I've found out an answer that may apply to this case here: http://www.php.net/manual/en/class.soapserver.php
If you want to return a custom object
array from a nusoap webservice, you
have to cast the objects to arrays
like so:
<?php
$users = array();
while($res = $db_obj->fetch_row())
{
$user = new user();
$user->Id = $res['id'];
$user->Username = $res['username'];
$user->Email = $res['email'];
$users[] = (array) $user;
}
return ($users);