I am trying to add data to JSON object. The output result doesn't show appended data "people3". Could you please let me know if I am doing right.
$postArray = array(
"persons" => array(
"person" => array(
"i_date"=> $DatabaseDate,
"i_location"=>$_POST["location"],
"i_summary"=>$_POST["summary"]
),
"people" => array(
"people1"=> array(
"first_name"=> $_POST["first-1"],
"last_name"=>$_POST["last-1"]
),
"people2"=> array(
"first_name"=>$_POST["first-2"],
"last_name"=>$_POST["last-2"],
)
)
)
);
array_push($postArray['people'],
array(
"people3"=> array(
"first_name"=>$_POST["first-2"],
"last_name"=>$_POST["last-2"],
)
));
var_dump(json_encode( $postArray ));
Updated code:
array_push($postArray["persons"]['people'],
array("people3"=> array(
"first_name"=>$_POST["first-2"],
"last_name"=>$_POST["last-2"],
)));
Check this:
$postArray = array(
"persons" => array(
"person" => array(
"i_date"=> $DatabaseDate,
"i_location"=>$_POST["location"],
"i_summary"=>$_POST["summary"]
),
"people" => array(
"people1"=> array(
"first_name"=> $_POST["first-1"],
"last_name"=>$_POST["last-1"]
),
"people2"=> array(
"first_name"=>$_POST["first-2"],
"last_name"=>$_POST["last-2"],
)
)
)
);
$postArray["persons"]['people']['people3']=
array(
"first_name"=>$_POST["first-2"],
"last_name"=>$_POST["last-2"],
);
var_dump(json_encode( $postArray ));
Output in php: print_r :-
Array
(
[persons] => Array
(
[person] => Array
(
[i_date] =>
[i_location] =>
[i_summary] =>
)
[people] => Array
(
[people1] => Array
(
[first_name] =>
[last_name] =>
)
[people2] => Array
(
[first_name] =>
[last_name] =>
)
[people3] => Array
(
[first_name] =>
[last_name] =>
)
)
)
)
As mentioned by others, that is correct as well.
array_push($postArray['persons']['people'],
array(
"people3"=> array(
"first_name"=>$_POST["first-2"],
"last_name"=>$_POST["last-2"],
)
));
Related
I'm trying to array_merge_recursive two arrays.
$arrayA = array(
'properties' => array(
'path' => array(
'default' => '',
),
'multiple' => array(
'default' => true,
),
)
);
$arrayB = array(
'properties' => array(
'path' => array(
'default' => 'foo',
),
'multiple' => array(
'default' => false,
),
)
);
$array = array_merge_recursive($arrayA,$arrayB);
print_r($array);
Which gives
Array (
[properties] => Array (
[path] => Array (
[default] => Array (
[0] =>
[1] => foo
)
)
[multiple] => Array (
[default] => Array (
[0] => 1
[1] =>
)
)
)
)
As you can see, the default and multiple properties are merged into arrays since it exists in both arrays.
But I don't want them to be transformed into arrays, I want that the last value to override the previous one.
And of course they DO exists in both array, I cannot declare it only in the second one.
How could I achieve this ?
Thanks !
I am new in MongoDB. I have created query in MongoDB using PHP. My query is as follows:
$date_start = new MongoDate(strtotime("2016-06-08T18:30:00.000Z"));
$default_date = new MongoDate(strtotime("1970-01-01T00:00:00.000Z"));
$pipeline = array(
array('$project' => array(
'MainsPower' => 1,
'EventTS' => 1
)),
array('$unwind' => array(
'path' => '$MainsPower',
'includeArrayIndex' => "arrayIndex",
'preserveNullAndEmptyArrays' => true
)),
array('$match' => array(
'$and' => array(
//array('EventTS' => array('$gt' => $date_start)),
//array('PanelID' => 'A00911'),
array("MainsPower" => array('$ne' => null))
)
)),
array(
'$project' => array(
'MainsPower' => 1,
'_id' => 0,
'EventTS' => array(
'$add' => array(
array('$subtract' => array('$EventTS', $default_date)),
array('$multiply' => array(60000, '$arrayIndex'))
)
)
)
),
);
$result = $collection - > aggregate($pipeline);
Output of query is as below
Array
(
[0] => Array(
[EventTS] => 1497033900000[MainsPower] => 204
)
[1] => Array(
[EventTS] => 1497034800000[MainsPower] => 204
)
[2] => Array(
[EventTS] => 1497035700000[MainsPower] => 204
)
)
But, I want output as below because while plotting graph I need data in below format I searched many solution and I tried to apply the same but failed to get records in required format
Array
(
[0] => Array(
[1497033900000, 204]
)
[1] => Array(
[1497034800000, 204]
)
[2] => Array(
[1497035700000, 204]
)
)
Use the below code:
$formattedResponse = array();
foreach($mongoOutput as $out){
$formattedResponse[] = array($out["EventTS"],$out["MainsPower"]);
}
where mongoOutput is the default output you are getting from mongo, and formattedResponse is the variable in which you will get the output in desired format.
Please change variable names as per your needs.
im using yii1 on my application.
i want to convert from cActivedataProvider to array
this is the code
$dataSS = new CActiveDataProvider('category', array(
'criteria' => array(
'condition' => 'menu=:menu',
'params' => array(':menu' => $menu),
),
'pagination' => false
));
$dataMenu = array();
foreach ($dataSS->getData() as $record) {
$dataMenu[] = array(
'label' => $record->name,
'url' => '#',
);
}
this is the result :
Array (
[0] => Array ( [label] => Food and Drink [url] => # )
[1] => Array ( [label] => Sleman [url] => # )
)
the result that i expected :
Array (
Array ( 'label' => 'Food and Drink', 'url' => '#' ) ,
Array ( 'label' => 'Sleman', 'url' => '#' ) ,
)
any suggestion?
Finally get the answer,
this is really my bad because i call the function on wrong way.
This is the wrong way :
'items' =>array(Category::model()->getMenu("2");),
and this is the correct way :
'items' =>Category::model()->getMenu("2"),
I have to make a webapp in Yii framework.
I want to work with select2 And want to use a column in my database..
This is my code
<?php
$users= User::model()->findAll(array(
'select'=>'name',
));
echo CHtml::textField('User','',array('id'=>'user', 'style'=>'width:300px;'));
$this->widget('ext.select2.ESelect2',array(
'selector'=>'#user',
'options' => array(
'tags'=> $names,
),
));
?>
When I use vardump to check what is in the variable $users then I get this:
Array
(
[0] => User Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[name] => "asd"
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] =>
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[1] => User Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[name] => "assss"
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] =>
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
And a lot more.
But the only thing I want is this: [name] => "assss" .
I think I have too use an foreach loop or something like that because select2 wants a "clear" array.
Do you guys know how to do this?
You will get array
$array = CHtml::listData(User::model()->findAll(), 'user_id', 'user_name');
For example the following code
<?php echo $form->dropDownList($model, 'country', CHtml::listData(Country::model()->findAll(), 'id', 'name'), array('id' => 'country_list', 'multiple' => true)) ?>
<?php $this->widget('application.extensions.select2.ESelect2',
array(
'selector' => "#country_list",
'options' => array(
'dropdownCssClass' => 'bigdrop',
'width' => '60%',
)
))?>
<?php
$users= User::model()->findAll(array(
'select'=>'name',
));
echo CHtml::textField('User','',array('id'=>'user', 'style'=>'width:300px;'));
$this->widget('ext.select2.ESelect2',array(
'selector'=>'#user',
'options' => array(
'tags'=> $names,
),
));
?>
I need to retrieve all users infos via api, looking for it in the documentation I found this:
http://apidocs.mailchimp.com/api/2.0/lists/member-info.php
This is my code:
$params = array(
'id' => $list_id,
'emails' => array(
'euid' => $member_id,
),
);
$infos = $this->MailChimp->call('lists/member-info', $params);
print_r($infos);
and this is the result:
Array ( [success_count] => 0 [error_count] => 1 [errors] => Array ( [0] => Array ( [email] => 63a885b7cf [error] => "email" should be a struct [code] => -100 ) ) [data] => Array ( ) )
What does " "email" should be a struct " means?
This also works and is slightly simpler:
$result = $MailChimp->call('lists/member-info', array(
'id' => $list_id,
'emails' => array(array('email'=>$email))
));
The above example uses this API: https://github.com/drewm/mailchimp-api/
Solved!
My $params array was wrong.
MailChimp need an array format in this way:
$params = array(
'id' => $list_id,
'emails' => array(
0 => array(
'euid' => $member_id,
),
),
);
Mine does this as follows:
array(
'0' => array('email' => $mail1)
'1' => array('email' => $mail2)
...
...
);
Thanks,