My request with zend 1 :
$table_mail = Table_Factory::getInstance()->getTable('Table_Dossier');
$req = $table_mail->select()->where('id_email =?', $id_email);
$data_email = $table_mail->fetchRow($req);
$data_email contains :
object(Zend_Db_Table_Row)[75]
protected '_data' =>
array (size=17)
'id_email' => string '1685' (length=4)
'id_operation' => string '931' (length=3)
...
But after i want add one element (to_moi) in $data_email (without use of toArray()).
I have try without success :
$data_email[] = array('to_moi' => true);
$data_email->put('to_moi' , true);
$data_email->to_moi = true;
$data_email(array('to_moi' => true));
Thank you.
Try this will may help you ,
$data_email['to_moi'] = true;
Related
I send data via ajax in controller. I create form dynamically in jquery, that's why i have array in my post request. It look likes this
array (size=3)
'login' =>
array (size=2)
0 => string 'trololo' (length=7)
1 => string 'trololo2' (length=8)
'pass' =>
array (size=2)
0 => string 'trololo' (length=7)
1 => string 'trololo2' (length=8)
'email' =>
array (size=2)
0 => string 'trololo#gmail.com' (length=17)
1 => string 'trololo#gmail.com2' (length=18)
In my controller I want persist it in my db. I do that like:
$data = $request->request->all();
$i = 0;
foreach($data as $ud)
{
$user[] = new User();
$user->setLogin($ud['login[$i]']);
$user->setPass($ud['pass[$i]']);
$user->setEmail($ud['email[$i]']);
$em->persit($user[$i])
$em->flush();
$em->clear();
$i++
}
But it does not working. Please, help me to solve this problem
You are mixed $user as array as as object. Also input $data contain arrays to save, but you do not need foreach for $data itself.
Try change it to:
$data = $request->request->all();
foreach($data['login'] as $i => $login)
{
$user = new User();
$user->setLogin($login);
$user->setPass($data['pass'][$i]);
$user->setEmail($data['email'][$i]);
$em->persit($user);
$em->flush();
$em->clear();
}
I have a problem I am getting a json file I can echo it and it looks like this
[{"sys_data":"0MxPPaza","date":"2015-02-15","objective":"VIDEO"}]
in my code I am doing this
$json = FROM THE SERVER;
$obj = json_decode($json);
$res = $obj->["objective"];
echo $res;
res is NULL obj is NULL also
Your json_decode call returns an array, with one member.
Here is a dump of your json object:
array (size=1)
0 =>
object(stdClass)[10]
public 'sys_data' => string '0MxPPaza' (length=8)
public 'date' => string '2015-02-15' (length=10)
public 'objective' => string 'VIDEO' (length=5)
so replace this line:
$res = $obj->["objective"];
With this:
$res = $obj[0]->objective;
Just replace [] quotes to {}. Like $res = $obj[0]->{"objective"};
Or you may use assoc array convertation instead of object:
$json = FROM THE SERVER;
$obj = json_decode($json, true);
$res = $obj["objective"];
echo $res;`
I got the following parameters as a response from SOAP client. But i only want few to show as a result. I am getting the results properly but its only for 1 vehicle and i have more than 1 vehicles. So i dont know how to loop to get the results.
Output for 1 vehicle
array (size=5)
'SchwackeCode' => int 10130969
'WE_Number' => int 19373134
'HSN' => string '0005' (length=4)
'TSN' => string 'AMP' (length=3)
'VIN' => string '12345678901472583' (length=17)
Code:
$client = new SoapClient($wsdl, $options);
$result = $client->getVehicleValuation($params);
$return = array(
'SchwackeCode' => $result->vehicle->SchwackeCode,
'WE_Number' => $result->vehicle->WE_Number,
'HSN' => $result->vehicle->HSN,
'TSN' => $result->vehicle->TSN,
'VIN' => $result->vehicle->Ident_Number,
'WE_Number' => $result->vehicle->WE_Number
);
return $return;
ok, then just try this simple code,
$cnt=0;
$arr=Array('SchwackeCode','WE_Number','HSN','TSN' );
foreach($result->Vehicle[$cnt]->Customer[0] as $key=>$val)
{
if(in_array($key,$arr)
{
your_piece of code;
}
$cnt++;
}
Didnt tested this code, but hopefully it will work. :)
I have an object with a property:
protected $recipients = array();
In one of my methods I set the contents of the $recipients property with an associative array of data using this method:
public function setRecipientData($recipientData){
foreach($recipientData as $key => $value){
$this->recipients[$key] = $value;
}
}
When I dump the contents of $this->recipients, the array ends up looking like this:
array (size=2)
0 =>
array (size=6)
'email' => string 'info#mycompany.com' (length=29)
'userEmail' => string 'xxx#yyy.com' (length=11)
'first_name' => string 'Test' (length=4)
'last_name' => string 'User' (length=4)
'jobTitle' => string 'Customer User' (length=13)
'phoneNumber' => string '123.456.7890' (length=12)
I also have a property that will extract only the email addresses from the property like this:
private function getRecipientEmails(){
$emails = array();
foreach($this->recipients as $recipient){
$emailPair = array('email' => $recipient['userEmail']);
$emails[] = $emailPair;
}
return $emails;
}
The problem that I'm running into is that when I run the method getRecipientsEmails() I get the error:
Undefined index: userEmail
I don't understand why it's not seeing the index 'userEmail'. If I dump the nested array it shows a value:
private function getRecipientEmails(){
$emails = array();
foreach($this->recipients as $recipient){
dd($emailPair = array('email' => $recipient['userEmail']));
$emails[] = $emailPair;
}
return $emails;
}
Result:
array (size=1)
'email' => string 'xxx#yyy.com' (length=20)
I've tried dumping the autoload with composer but it makes no difference.
Can anyone help point me in the right direction??
EDIT: Sorry, I realized that I wrote the wrong method into my original post. I've added the original getRecipientEmails() method and the dump that shows the data.
I am making a web app. In one part of it, I have JS send a JSON string to PHP. The conent of the string is:
{"date":"24-03-2014","Cars":["Cheap","Expensive"]}
I want to convert the string into an object, for which I am doing:
$meta = $_POST["meta"];
$obj = json_decode($meta);
echo $obj->date;
Anyhow, Instead of having 24-03-2014 as the output, I am getting a blank line as the output.
What's wrong? What's the correct way of doing this?
Not able to re-produce it:
$jsonStr = '{"date":"24-03-2014","Cars":["Cheap","Expensive"]}';
$jsonObj = json_decode($jsonStr);
var_dump($jsonObj);
var_dump($jsonObj->date);
Outputs:
object(stdClass)[1]
public 'date' => string '24-03-2014' (length=10)
public 'Cars' =>
array (size=2)
0 => string 'Cheap' (length=5)
1 => string 'Expensive' (length=9)
string '24-03-2014' (length=10)
Are you sure your $_POST['meta'] is set & has values?
Below works like a charm. Your $_POST["date"] has not correct value inside. Try var_dump($_POST) to debug it.
<?php
$input = '{"date":"24-03-2014","Cars":["Cheap","Expensive"]}';
$meta = $input;
$obj = json_decode($meta);
var_dump($obj->date); //Prints string(10) "24-03-2014"
?>