I need some help with a JSON post request, I need to post an array of fields with multiple ids and values. Posting one id and value works but I can't figure out how to include multiple field IDs in this request.
Here is my code:
$appointment = $acuity->request('/appointments', array(
'method' => 'POST',
'data' => array(
'firstName' => 'Bob',
'lastName' => 'Burger',
'email' => 'bob#example.org',
'datetime' => '2016-04-26T19:00:00-0700',
'appointmentTypeID' => 5020,
'fields' => array(
array(
'id' => 4, // Custom field ID 4
'value' => '1600 Pennsylvania Avenue',
'id' => 5, // Custom field ID 5
'value' => '5550 Somewhere St.'
)
)
)
));
I suspect the intent is for this to use nested arrays, like so:
'fields' => array(
array(
'id' => 4, // Custom field ID 4
'value' => '1600 Pennsylvania Avenue',
),
array(
'id' => 5, // Custom field ID 5
'value' => '5550 Somewhere St.'
)
)
Related
I have wordpress option_name student_data with option value a:7:{s:6:"status";s:1:"2";s:13:"name";s:73:"valuename";s:9:"address";s:32:"valueaddress";s:13:"school";s:32:"valueschool";s:4:"mother";s:270:"valuemother";s:12:"father";s:340:"valuefather";s:13:"total";s:131:"valuetotal";}
Below is my code to update the data, but it doesn't work.
$option_array = array(
'status' => '2',
'name' => 'valuename',
'address' => 'valueaddress',
'school' => 'valueschool',
'mother' => 'new_valuemother',
'father' => 'new_valuefather',
'total' => 'valuetotal'
);
update_option('student_data', $option_array);
Is there any solution? Thanks.
I want to add some data to an existing array without modifying any of the existing keys or values.
The original array is like this:
array(
'sections' => array(
array(
'id' => 'sections_id',
'title' => 'sections_title'
)
),
'settings' => array(
array(
'id' => 'settings_id',
'title' => 'settings_title'
)
)
);
Now I want to be ablle to add new arrays to sections key and to settings key that will turn the following into :
array(
'sections' => array(
array(
'id' => 'sections_id',
'title' => 'sections_title'
),
array(
'id' => 'NEW_sections_id',
'title' => 'NEW_sections_title'
)
),
'settings' => array(
array(
'id' => 'settings_id',
'title' => 'settings_title'
),
array(
'id' => 'NEW_settings_id',
'title' => 'NEW_settings_title'
)
)
);
I tried using array_push and array_merge without success I hope someone can help.
Thank you!
you would do it just like you would any other array.
$array['sections'][] = array('id' => 'sec_id', 'title' => 'sec_title');
I have a situation where I have hasMany relations between two objects. request data looks like this
array(
'Project' => array(
'name' => 'Projekt Y',
'main_contractor' => 'APOS - Zagreb',
'main_contractor_id' => '188',
'arhitect_id' => '20'
),
'ProjectAssociate' => array(
(int) 0 => array(
'project_role_id' => '1',
'Person' => array(
'input' => 'First name, last name',
'firm_id' => ''
)
),
(int) 1 => array(
'project_role_id' => '1',
'Person' => array(
'input' => '',
'firm_id' => ''
)
)
)
)
The problem is, that I can not save this data because validation does not allow Person.input to be empty. Is there a way to cancel the save proces only for ProjectAssociate.1? I want to successfully save the rest of the data
Try to unbind your ProjectAssociate model
// Let's remove the hasMany...
$this->Project->unbindModel(
array('hasMany' => array('ProjectAssociate'))
);
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
I have two tables: Posts and Likes
And I'm trying to list the posts that the current logged in user likes. The method looks like the following:
$following = $this->Follower->listFollowing($this->Auth->user('id'));
$this->paginate = array('limit'=>20,'conditions'=>array('Post.id'=>array($following['Follower']['post_id']),'Post.status'=>array(1,2)),'order'=>array('Post.datetime'=>'desc'),
'contain'=>array('User','Answer'=>array('User'),'Tag'));
$this->set('posts',$this->paginate());
So basically what I'm trying to do is first query the following (likes) table for all matching rows to the user and then use this array of post ids in my find query to list posts that were in the query.
The listFollowing method in the Follower model looks like:
public function listFollowing($user_id)
{
return $this->find('all', array(
'conditions' => array('Follower.user_id'=>$user_id)
));
}
I'm currently getting an error like: Undefined index: Follower [APP/Controller/PostsController.php, line 94] So gonna presume that the way I'm trying to pass the list of post ids from the following in the find query is incorrect.
Can anyone help? Thanks
Edit: Doing a debug on $following gives:
array(
(int) 0 => array(
'Follower' => array(
'id' => '4',
'user_id' => '6',
'post_id' => '136'
),
'User' => array(
'password' => '*****',
'id' => '6',
'username' => 'driz',
'email' => '######'
),
'Post' => array(
'id' => '136',
'user_id' => '8',
'datetime' => '2012-09-11 15:49:52',
'modified' => '2012-09-16 15:31:38',
'title' => 'Test Content',
'slug' => 'Test_content',
'content' => 'Test Content',
'status' => '1'
)
),
(int) 1 => array(
'Follower' => array(
'id' => '5',
'user_id' => '6',
'post_id' => '133'
),
'User' => array(
'password' => '*****',
'id' => '6',
'username' => 'driz',
'email' => '######'
),
'Post' => array(
'id' => '134',
'user_id' => '8',
'datetime' => '2012-09-11 15:49:52',
'modified' => '2012-09-16 15:31:38',
'title' => 'Test Content 2',
'slug' => 'Test_content_2',
'content' => 'Test Content 2',
'status' => '1'
)
)
)
Couple of questions:
Did you checked what query is being executed? If it's executed then probably the method is correct.
is $following being populated? what happens if you debug it?
if you are retreiving with 'all' then the result will look like this:
Array(
[0] => Array
(
[ModelName] => Array
(
[id] => 83
[field1] => value1
[field2] => value2
[field3] => value3
)
[AssociatedModelName] => Array
(
[id] => 1
[field1] => value1
[field2] => value2
[field3] => value3
)
)
)
So you'll never be able to find something at $following['Follower']. Check Cake's documentation for this
Based on you comments, if you need a list of id's try this inside your method:
public function listFollowing($user_id)
{
return $this->find('list', array(
'conditions' => array('Follower.user_id'=>$user_id)
'fields' => array('Follower.user_id'),
'recursive' => 0);
}
The 'list' retrieve mode does exactlye that.
It is hard to guess the error with this limited information but I'm guessing that it's a PHP error related with your $following array.
Can you try putting debug($following) after your listFollowing() function call? So in the end, it should look like this:
$following = $this->Follower->listFollowing($this->Auth->user('id'));
debug($following);
$this->paginate = array('limit'=>20,'conditions'=>array('Post.id'=>array($following['Follower']['post_id']),'Post.status'=>array(1,2)),'order'=>array('Post.datetime'=>'desc'),
'contain'=>array('User','Answer'=>array('User'),'Tag'));
$this->set('posts',$this->paginate());
You will see what is returned from listFollowing() that is causing an index error. You can further debug it with that information. I can no longer help as there is no database schema, full code, the variables/situation that causes this problem, etc :)
I have an array:
$initialarray = array(
0 = array(
'unit' => 1,
'class' => 1,
'value' => 'string1'
),
1 = array(
'unit' => 1,
'class' => 2,
'value' => 'string2'
),
2 = array(
'unit' => 1,
'class' => 2,
'value' => 'string3'
),
3 = array(
'unit' => 2,
'class' => 1,
'value' => 'string4'
)
4 = array(
'unit' => 2,
'class' => 2,
'value' => 'string5'
)
);
What would be the best way to structure it (to group the resulting sub-arrays) depending first on the 'unit' field's values, and then depending on the 'class' field's values, like so:
$resultarray = array(
// array of all the sub-arrays of 'unit' = 1
$unit[1] = array(
// array of all the sub-arrays of 'unit' = 1 and 'class' = 1
$class[1] = array(
0 = array(
'unit' => 1,
'class' => 1,
'value' => 'string1'
)
)
// array of all the sub-arrays of 'unit' = 1 and 'class' = 2
$class[2] = array(
0 = array(
'unit' => 1,
'class' => 2,
'value' => 'string2'
),
1 = array(
'unit' => 1,
'class' => 2,
'value' => 'string3'
)
)
)
// array of all the sub-arrays of 'unit' = 2
$unit[2] = array(
// array of all the sub-arrays of 'unit' = 2 and 'class' = 1
$class[1] = array(
0 = array(
'unit' => 2,
'class' => 1,
'value' => 'string4'
)
)
// array of all the sub-arrays of 'unit' = 2 and 'class' = 2
$class[2] = array(
0 = array(
'unit' => 2,
'class' => 2,
'value' => 'string5'
)
)
)
)
I have asked a similar question here and got a working answer for only one iteration, i.e. for only structuring the array by one of the fields. But I could not make the same solution work for multiple iterations, i.e. for more than one field.
Also, is there a solution to structure a multidimensional array depending on more than two fields?
I think it's not a way of asking the question. It is very simple , you can do this by playing with arrays,keys and etc.... So first you should try hard for the problem. After If you have any problem in the middle of your tries then you can ask that here. I have solved your problem here is the complete code , but next time please do some work and then only post the problem. Never ask for the code.
foreach ($initialarray as $key1=>$val1)
{
foreach ($val1 as $key2=>$val2)
{
if($key2=='unit')
{
$num=$val2;
if($val2!=$num)
$testarr['unit'.$val2]=array();
}
if($key2=='class')
{
$testarr['unit'.$num]['class'.$val2][]=$val1;
}
}
}
print_r($testarr);
I must offer a better way for you and future researchers...
You only need one loop, and you merely need to nominate the result array's key values before using [] to "push" new data into the deepest subarray.
*there is absolutely no need for any condition statements or a second loop.
Code: (Demo)
$initialarray = [
['unit' => 1, 'class' => 1, 'value' => 'string1'],
['unit' => 1, 'class' => 2, 'value' => 'string2'],
['unit' => 1, 'class' => 2, 'value' => 'string3'],
['unit' => 2, 'class' => 1, 'value' => 'string4'],
['unit' => 2, 'class' => 2, 'value' => 'string5']
];
foreach ($initialarray as $row) {
$result[$row['unit']][$row['class']][] = $row;
}
var_export($result);
Output:
array (
1 =>
array (
1 =>
array (
0 =>
array (
'unit' => 1,
'class' => 1,
'value' => 'string1',
),
),
2 =>
array (
0 =>
array (
'unit' => 1,
'class' => 2,
'value' => 'string2',
),
1 =>
array (
'unit' => 1,
'class' => 2,
'value' => 'string3',
),
),
),
2 =>
array (
1 =>
array (
0 =>
array (
'unit' => 2,
'class' => 1,
'value' => 'string4',
),
),
2 =>
array (
0 =>
array (
'unit' => 2,
'class' => 2,
'value' => 'string5',
),
),
),
)
If I may express myself in the following manner: I only see the front-end of your problem and know nothing about its back-end, e.g. "Where does the data come from?", "How is it collected and stored", etc. so my answer might not be a real help but still I'll give my "tuppence".
If you can store all that data in a relational database (in form of table(s)) it would be much more easier and faster(!) to select the needed data from the database instead of rearranging arrays, which will take some more time in comparison.
Just as an example you might then select (and store it into an array) all items which have unit = '1' and / or all items which have class = '2'. That would make life much more easier IMHO, than having all the data in a multidimensional array and then try to sort it / rearrange it. Especially if you do that based on more than one property.