I have 2 arrays. 1 fetching from database and other one is from view page. I am developing a online test exam website so i have to check if the answer entered by user is correct or not.Here is the 1st one.
Array
(
[0] => Array
(
[Question] => Array
(
[id] => 51f92e34-c5a8-4de3-b264-0ff0d0483c4c
[aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[paper_id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
[qus] => What is ur name?
[slug] => name-find
[image] =>
[opt1] => x
[opt2] => y
[opt3] => a
[opt4] => b
[opt5] => c
[answer_id] => 4
[description] =>
[ansimage] =>
)
[Aptitude] => Array
(
[id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[name] => php
[slug] => php
)
[Paper] => Array
(
[id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
[aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[name] => aptitude1
[slug] => aptitude1
)
[Answer] => Array
(
[id] => 4
[name] => D
)
)
[1] => Array
(
[Question] => Array
(
[id] => 51fe4098-c344-4790-9e46-0fb4d0483c4c
[aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[paper_id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
[qus] => Place?
[slug] => place
[image] =>
[opt1] => ss
[opt2] => sss
[opt3] => ss
[opt4] => ss
[opt5] => ss
[answer_id] => 3
[description] =>
[ansimage] =>
)
[Aptitude] => Array
(
[id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[name] => php
[slug] => php
)
[Paper] => Array
(
[id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
[aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[name] => aptitude1
[slug] => aptitude1
)
[Answer] => Array
(
[id] => 3
[name] => C
)
)
[2] => Array
(
[Question] => Array
(
[id] => 51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c
[aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[paper_id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
[qus] => hayywep?
[slug] => dada
[image] =>
[opt1] => a
[opt2] => a
[opt3] => a
[opt4] => a
[opt5] => a
[answer_id] => 3
[description] =>
[ansimage] =>
)
[Aptitude] => Array
(
[id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[name] => php
[slug] => php
)
[Paper] => Array
(
[id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
[aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
[name] => aptitude1
[slug] => aptitude1
)
[Answer] => Array
(
[id] => 3
[name] => C
)
)
)
and
Array
(
[51f92e34-c5a8-4de3-b264-0ff0d0483c4c] => 3
[51fe4098-c344-4790-9e46-0fb4d0483c4c] => 3
[51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c] => 3
)
i wrote this code
foreach($res as $res1):
foreach($ans as $ans1):
if($res1['Question']['answer_id']==$ans1)
{
print_r($res1['Question']['id']);
}
endforeach;
endforeach;
Output:
51f92e34-c5a8-4de3-b264-0ff0d0483c4c
51f92e34-c5a8-4de3-b264-0ff0d0483c4c
51f92e34-c5a8-4de3-b264-0ff0d0483c4c
51fe4098-c344-4790-9e46-0fb4d0483c4c
51fe4098-c344-4790-9e46-0fb4d0483c4c
51fe4098-c344-4790-9e46-0fb4d0483c4c
51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c
51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c
51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c
how to remove the duplication?
Try array_unique
$_results = array();
foreach($res as $res1):
foreach($ans as $ans1):
if($res1['Question']['answer_id']==$ans1)
{
$_results[] = $res1['Question']['id'];
}
endforeach;
endforeach;
$results = array_unique($_results);
pr($results);
Related
After printing my $my_values['outer_group']['fieldset'] I'm getting the following output as per the data:
Array
(
[fieldset] => Array
(
[1] => Array
(
[title] => Dummy_Value1
[inner_group] => Array
(
[fieldset] => Array
(
[1] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[2] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[3] => Array
(
[id] => 13
[title] => Dummy_Value13
)
[actions] => Array
(
[add] => Add InnerGroup
[remove] => Remove InnerGroup
)
)
)
)
[2] => Array
(
[title] => Dummy_Value2
[inner_group] => Array
(
[fieldset] => Array
(
[1] => Array
(
[id] => 21
[title] => Dummy_Value21
)
[actions] => Array
(
[add] => Add InnerGroup
)
)
)
)
[actions] => Array
(
[add] => Add OuterGroup
[remove] => Remove OuterGroup
)
)
)
My requirement is to re-index the output data, hence I've performed the following code to re-index the same:
<?php
if (isset($my_values['outer_group']) && !empty($my_values['outer_group'])) {
$outer_types = $my_values['outer_group']['fieldset'];
$inner = [];
foreach ($outer_types as $outer_key => $outer_value) {
if (is_numeric($outer_key)) {
if (isset($outer_value['inner_group']['fieldset'])) {
foreach ($outer_value['inner_group']['fieldset'] as $k => $v) {
if (is_numeric($k)) {
$inner[] = [
'id' => $v['id'],
'title' => !empty($v['title']) ? $token->replace($v['title']) : NULL,
];
}
}
}
$my_values['outer'][$outer_key] = [
'title' => !empty($outer_value['title']) ? $token->replace($outer_value['title']) : NULL,
'inner' => $inner,
];
}
}
}
As per the output its getting re-indexed but with some errors in data. I'm getting trouble while populating the [inner] data, following is the output for the same:
Array
(
[0] => Array
(
[title] => Dummy_Value1
[inner] => Array
(
[0] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[1] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[2] => Array
(
[id] => 13
[title] => Dummy_Value13
)
)
)
[1] => Array
(
[title] => Dummy_Value2
[inner] => Array
(
[0] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[1] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[2] => Array
(
[id] => 13
[title] => Dummy_Value13
)
[3] => Array
(
[id] => 21
[title] => Dummy_Value21
)
)
)
)
Whereas, it should be:
Array
(
[0] => Array
(
[title] => Dummy_Value1
[inner] => Array
(
[0] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[1] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[2] => Array
(
[id] => 13
[title] => Dummy_Value13
)
)
)
[1] => Array
(
[title] => Dummy_Value2
[inner] => Array
(
[0] => Array
(
[id] => 21
[title] => Dummy_Value21
)
)
)
)
$inner = []; needs to be within the foreach loop so that it is empty before building each internal element.
Untested - but as follows:
<?php
if (isset($my_values['outer_group']) && !empty($my_values['outer_group'])) {
$outer_types = $my_values['outer_group']['fieldset'];
foreach ($outer_types as $outer_key => $outer_value) {
$inner = [];
if (is_numeric($outer_key)) {
if (isset($outer_value['inner_group']['fieldset'])) {
foreach ($outer_value['inner_group']['fieldset'] as $k => $v) {
if (is_numeric($k)) {
$inner[] = [
'id' => $v['id'],
'title' => !empty($v['title']) ? $token->replace($v['title']) : NULL,
];
}
}
}
$my_values['outer'][$outer_key] = [
'title' => !empty($outer_value['title']) ? $token->replace($outer_value['title']) : NULL,
'inner' => $inner,
];
}
}
}
i have two arrays and i want two merge using the same value within each array..
the first array is :
[0] => Array
(
[id] => 1
[uid] => 0090000157
[cid] => 0090000007
[extension] => 202
[secret] => Myojyo42f!
[leader] => 1
[simultaneous] =>
[confbridge_id] => 2
[created_at] => 2015-07-26 12:20:20
[updated_at] => 2015-07-26 12:20:20
)
[1] => Array
(
[id] => 2
[uid] => 0090000159
[cid] => 0090000007
[extension] =>
[secret] => Myojyo42f!
[leader] =>
[simultaneous] =>
[confbridge_id] => 2
[created_at] => 2015-07-26 14:23:41
[updated_at] => 2015-07-26 14:23:41
)
)
and the second array is:
Array
(
[0] => Array
(
[_id] => 55b52f4c2bab38fc63b6272a
[event] => ConfbridgeJoin
[channel] => SIP/peer_voip301confbridge-0000001b
[uniqueid] => 1437937478.63
[conference] => 0090000156
[calleridnum] => 0090000157
[calleridname] => 0090000157
[__v] => 0
[sipSetting] => Array
(
[accountcode] =>
[accountcode_naisen] => 202
[extentype] => 0
[extenrealname] =>
[name] => 0090000157
[secret] => Myojyo42f!
[username] => 0090000157
[context] => innercall_xdigit
[gid] => 101
[cid] => 0090000007
)
)
[1] => Array
(
[_id] => 55b53a2e2bab38fc63b6272b
[event] => ConfbridgeJoin
[channel] => SIP/peer_voip301confbridge-0000001c
[uniqueid] => 1437940260.66
[conference] => 0090000156
[calleridnum] => 0090000158
[calleridname] => UID2
[__v] => 0
[sipSetting] => Array
(
[accountcode] =>
[accountcode_naisen] => 203
[extentype] => 0
[extenrealname] =>
[name] => 0090000158
[secret] => Myojyo42f!
[username] => 0090000158
[context] => innercall_xdigit
[gid] => 101
[cid] => 0090000007
)
)
)
i want to merge array with the same value for example :
first array has = [uid] => 0090000157
second array has = [calleridnum] => 0090000157
is it possible to merge them?
this is my code
{foreach from=$participants item=participant key=p }
{foreach from=$conference_participants item=conference_participant key=c}
{if$participants.calleridnum == $conference_participants.uid}
//how to get data here ?
{/if}
{/foreach}
{/foreach}
Could this be what you're looking for?
Sorry for all the changes, I should have tested it... grr
(I've put this in PHPFiddle:
<pre>
<?php
$participants = [
[ 'calleridnum' => 1,
'test' => 'yay'
]
];
$conferance_participants = [
[ 'uid' => 1,
'test' => 'yay2',
'dit' => 'deze'
]
];
foreach ($participants as $participant=>$p) {
foreach ($conferance_participants as $conferance_participant=>$c) {
if ($p['calleridnum'] == $c['uid']) {
// ID's match do the magic here
foreach ( $c as $key=>$val ) {
if (!isset($p[$key])) {
// Value is new, copy the conferance_participant to the participant
$participants[$participant][$key] = $val;
}
} // Merging data
} // If: Match
}
}
print_r( $participants );
?>
)
I am running following query in CakePHP:
$options = array('conditions' => array('Patient.' . $this->Patient->primaryKey => $id),array('recursive'=>2,'group'=>array('group_id')));
$this->set('patient', $this->Patient->PatientTest->find('all', $options));
But my group is not working as needed. It gives me output as follows:
Array
(
[0] => Array
(
[PatientTest] => Array
(
[patient_id] => 2
[test_id] => 1
[date] => 2014-03-27 17:44:17
[result] => 55
[group_id] => 1
)
[Patient] => Array
(
[id] => 2
[name] => Test Patient
[age] => 44
[gender] => 0
[email] => emailid#gmail.com
[contact] => 789654123
[date] => 0000-00-00 00:00:00
)
[Test] => Array
(
[id] => 1
[name] => Hb
[group_id] => 1
[normal] => 12 - 16 gma%
)
[Group] => Array
(
[id] => 1
[name] => Haematology
)
)
[1] => Array
(
[PatientTest] => Array
(
[patient_id] => 2
[test_id] => 2
[date] => 2014-03-27 17:44:17
[result] => 55
[group_id] => 1
)
[Patient] => Array
(
[id] => 2
[name] => Test Patient
[age] => 44
[gender] => 0
[email] => emailid#gmail.com
[contact] => 789654123
[date] => 0000-00-00 00:00:00
)
[Test] => Array
(
[id] => 2
[name] => PCV
[group_id] => 1
[normal] => 35-50%
)
[Group] => Array
(
[id] => 1
[name] => Haematology
)
)
)
I need them group by either group_id or by Group.id.
I am unable to find why it is not grouping.
not
$options = array
(
'conditions' => array
(
'Patient.' . $this->Patient->primaryKey => $id
),
array(
'recursive'=>2,
'group'=>array('group_id')
)
);
but
$options = array
(
'conditions' => array
(
'Patient.' . $this->Patient->primaryKey => $id
),
'recursive'=>2,
'group'=>array('group_id')
);
recursive, conditions and group are at the same level
Simple and sweet code :
$groupBy = 'Patient.group_id';
$options = array (
'conditions' => array
(
'Patient.' . $this->Patient->primaryKey => $id
),
'group'=>array($groupBy)
);
I have a weird error when using contain to limit my data to only what I need. Here is my contain
$this->Movie->contain(array(
'Review' => array(
'fields' => array('id', 'title', 'spoilers', 'body', 'created'),
'User' => array(
'fields' => array('id', 'username'),
'Profile' => array('fields' => array('id', 'image_thumb_small'))
),
'order' => 'Review.created DESC',
'limit' => 2,
),
'Movieupdates' => array(
'User' => array('Profile' => array('fields' => 'image_thumb_small')),
'order' => 'Movieupdates.created DESC',
'limit' => 2
),
'Actor' => array(
'fields' => array('name', 'slug', 'image_thumb'),
),
'Genre' => array(
'fields' => array('name', 'id'),
)
));
The problem is that Cakephp throws me two errors:
Warning (2): array_merge() [function.array-merge]: Argument #1 is not an array [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 400]
Warning (2): array_unique() expects parameter 1 to be array, null given [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 400]
I think the problem has to do with the following line (in contain: Review -> User):
'fields' => array('id', 'username'),
If I remove that everything is fine, but then again I dont want all the data from User, especially not the passwords so I have to limit that.
What is wrong with my Contain? The error suggest that I should be using an array somewhere, but I have no idea where.
Edit added query outcome
Array
(
[Movie] => Array
(
[id] => 27
[user_id] =>
[title] => The Amazing Spider-Man
[slug] => The_Amazing_Spider_Man_2012
[year] => 2012
[summary] => Peter Parker finds a clue that might help him understand why his parents disappeared when he was young.
[poster] => /files/movies/53cb8be658a2e04a21909e288cdcb8.jpg
[poster_thumb] => /files/movies/53cb8be658a2e04a21909e288cdcb8_resized_101x150.jpg
[backdrop] => /files/movies/backdrop/amazing_spider_man.jpg
[rating] => 7.4285714285715
[like_count] => 5
[review_count] => 7
[see_count] => 7
[rating_count] => 14
)
[Movieupdates] => Array
(
[0] => Array
(
[id] => 20
[user_id] => 4
[movie_id] => 27
[type] => 4
[info] => 26
[created] => 2012-12-08 11:06:18
[User] => Array
(
[id] => 4
[username] => Ceriksen
[email] => skdji#gmail.com
[password] => ---
[validation_code] => 082b7735cfb3a3d5c74547d702bd97f9af517870
[group_id] => 3
[created] => 2012-11-27 11:23:57
[modified] => 2012-11-27 11:23:57
[Profile] => Array
(
[id] => 5
[user_id] => 4
[image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
[image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
[image_thumb_medium] =>
[image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
[name] =>
[last_name] =>
[bio] =>
[country] =>
[city] =>
)
)
)
[1] => Array
(
[id] => 19
[user_id] => 4
[movie_id] => 27
[type] => 3
[info] => 10
[created] => 2012-12-08 11:06:08
[User] => Array
(
[id] => 4
[username] => Ceriksen
[email] => skdji#gmail.com
[password] => ---
[validation_code] => 082b7735cfb3a3d5c74547d702bd97f9af517870
[group_id] => 3
[created] => 2012-11-27 11:23:57
[modified] => 2012-11-27 11:23:57
[Profile] => Array
(
[id] => 5
[user_id] => 4
[image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
[image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
[image_thumb_medium] =>
[image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
[name] =>
[last_name] =>
[bio] =>
[country] =>
[city] =>
)
)
)
)
[Genre] => Array
(
[0] => Array
(
[name] => Action
[id] => 11
[MoviesGenre] => Array
(
[id] => 56
[movie_id] => 27
[genre_id] => 11
)
)
[1] => Array
(
[name] => Adventure
[id] => 12
[MoviesGenre] => Array
(
[id] => 57
[movie_id] => 27
[genre_id] => 12
)
)
[2] => Array
(
[name] => Fantasy
[id] => 18
[MoviesGenre] => Array
(
[id] => 58
[movie_id] => 27
[genre_id] => 18
)
)
)
[Actor] => Array
(
[0] => Array
(
[name] => Emma
[slug] => Emma_Stone
[image_thumb] => /files/actors/d59efc7614151acb7ea5d08da47112_resized_30x30.jpg
[MoviesActor] => Array
(
[id] => 1
[movie_id] => 27
[actor_id] => 8
)
)
[1] => Array
(
[name] => Andrew
[slug] => Andrew_Garfield
[image_thumb] => /files/actors/0a9354d741328f4cf3e2954641e4eb_resized_25x30.jpg
[MoviesActor] => Array
(
[id] => 2
[movie_id] => 27
[actor_id] => 9
)
)
[2] => Array
(
[name] => Martin
[slug] => Martin_Sheen
[image_thumb] => /files/actors/2383fc87054b2e8b4249fc7e3ffba5_resized_30x27.jpg
[MoviesActor] => Array
(
[id] => 3
[movie_id] => 27
[actor_id] => 10
)
)
)
[Review] => Array
(
[0] => Array
(
[id] => 26
[title] => W00t
[spoilers] => 0
[created] => 2012-12-08 11:06:18
[user_id] => 4
[MoviesReview] => Array
(
[id] => 25
[movie_id] => 27
[review_id] => 26
)
[User] => Array
(
[id] => 4
[username] => Ceriksen
[Profile] => Array
(
[id] => 5
[user_id] => 4
[image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
[image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
[image_thumb_medium] =>
[image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
[name] =>
[last_name] =>
[bio] =>
[country] =>
[city] =>
)
)
)
[1] => Array
(
[id] => 25
[title] => Testing the update
[spoilers] => 1
[created] => 2012-12-08 11:04:44
[user_id] => 4
[MoviesReview] => Array
(
[id] => 24
[movie_id] => 27
[review_id] => 25
)
[User] => Array
(
[id] => 4
[username] => Ceriksen
[Profile] => Array
(
[id] => 5
[user_id] => 4
[image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
[image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
[image_thumb_medium] =>
[image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
[name] =>
[last_name] =>
[bio] =>
[country] =>
[city] =>
)
)
)
)
)
Solved it, I had an error in my contain. Stupid one.
'User' => array('Profile' => array('fields' => 'image_thumb_small')),
Should have been:
'User' => array('Profile' => array('fields' => array('image_thumb_small'))),
I have the following models:
Kin, which you can think of as an animal--a pet.
Species hasAndBelongsToMany Kin
GrowthStage hasAndBelongsToMany Species
Kin hasAndBelongsToMany GrowthStage
and KinGrowthStage, which defines the relationship between a kin and its growth stages (it contains its related images and such for the kin at that growth stage).
So in my controller I'm trying to get the following array to come out the other end. In this scenario the Species => GrowthStage => KinGrowthStages are related to the Kin id:
Array
(
[Kin] => Array
(
[id] => 4
[name] => Hobbes
[generation] => 1
[current_growth_stage_id] => 4
[species_id] => 1
)
[Species] => Array
(
[id] => 1
[name] => Tiger
[GrowthStage] => Array
(
[0] => Array
(
[id] => 5
[name] => cub
[order] => 1
[KinGrowthStage] => Array
(
[0] => Array
(
[id] => 1
[growth_stage_id] => 1
[kin_id] => 4
[image] => /img/hobbes/cub.png
)
)
)
[1] => Array
(
[id] => 2
[name] => juvenile
[order] => 2
[KinGrowthStage] => Array
(
[0] => Array
(
[id] => 1
[growth_stage_id] => 1
[kin_id] => 4
[image] => /img/hobbes/juvenile.png
)
)
)
[2] => Array
(
[id] => 9
[name] => adult
[order] => 3
[KinGrowthStage] => Array
(
// Nothing here because Hobbes hasn't reached this growth stage yet
)
)
)
)
)
What I'm getting is this. The Species => GrowthStage => KinGrowthStages is joining on the GrowthStage id rather than the Kin id:
Array
(
[Kin] => Array
(
[id] => 4
[name] => Hobbes
[generation] => 1
[current_growth_stage_id] => 4
[species_id] => 1
)
[Species] => Array
(
[id] => 1
[name] => Tiger
[GrowthStage] => Array
(
[0] => Array
(
[id] => 5
[name] => cub
[order] => 1
[KinGrowthStage] => Array
(
[0] => Array
(
[id] => 1
[growth_stage_id] => 1
[kin_id] => 1
[image] => /img/tony/cub.png
)
)
)
[1] => Array
(
[id] => 2
[name] => juvenile
[order] => 2
[KinGrowthStage] => Array
(
[0] => Array
(
[id] => 1
[growth_stage_id] => 2
[kin_id] => 1
[image] => /img/tony/juvenile.png
)
)
)
[2] => Array
(
[id] => 9
[name] => adult
[order] => 3
[KinGrowthStage] => Array
(
[0] => Array
(
[id] => 1
[growth_stage_id] => 3
[kin_id] => 1
[image] => /img/tony/adult.png
)
)
)
)
)
)
I understand why this happening, I just want to know if there's any way to modify the relationship 'on the fly' and join it on the kin id. I've tried adding the following conditions array to my contain array:
$this->Kin->contain(
array(
'Species' => array(
'GrowthStage' => array(
'KinGrowthStage' => array(
'conditions' => array('Kin.id = KinGrowthStage.kin_id'),
),
'order' => 'GrowthStage.order ASC'
)
)
)
);
But I get an error about 'unknown table Kin'.
Use the Linkable Behavior https://github.com/Terr/linkable
$this->Kin->find('all', array(
'link' => array(
'Species' => array(
'GrowthStage' => array(
'KinGrowthStage' => array(
'conditions' => 'Kin.id = KinGrowthStage.kin_id',
)
)
)
),
'order' => 'GrowthStage.order ASC'
));