How can I Iterate over this ridiculously tedious array?
array (size=6)
0 =>
array (size=1)
'Question' =>
array (size=2)
'id' => string 'q_1' (length=3)
'question_desc' => string 'Is this correct?)' (length=15)
1 =>
array (size=1)
'Question' =>
array (size=2)
'id' => string 'q_10' (length=4)
'question_desc' => string 'Do you weigh less than 45 kilograms OR more than 160 kilograms.' (length=63)
This is a var_dump from a Session data! I need to get the question_desc field from each 'Question' array object.
This array has a purpose to its structure, but I understand your frustration as I shared it before I rtfm-ed!
$flattened_data = array();
foreach($your_main_array as $question)
{
foreach($question['Question'] as $question_param)
{
if($question_param == 'question_desc')
{
$flattened_data[] = $question_param;
// if you want to be really cool you can do this instead
// this will list the array with the question id as the key.
// $flattened_data[$question[id]] = $question_param;
}
}
}
// now flattened data has only what you require
return $flattened_data;
Cakes data form makes a lot more sense once you understand its ORM and how it uses model relations. Its actually a powerful tool for managing your data, but before you need all of that power it does seem like an encumbrance for simple tasks.
Related
I'm a novice perl programmer trying to identify which elements are in one array of hashes but not in another. I'm trying to search through the "new" array, identifying the id, title, and created elements that don't exist from the "old" array.
I believe I have it working with a set of basic for() loops, but I'd like to do it more efficiently. This only came after having tried to use grep() and failed.
These arrays are built from a database as such:
use DBI;
use strict;
use Data::Dumper;
use Array::Utils qw(:all);
sub db_connect_new();
sub db_disconnect_new($);
sub db_connect_old();
sub db_disconnect_old($);
my $dbh_old = db_connect_old();
my $dbh_new = db_connect_new();
# get complete list of articles on each host first (Joomla! system)
my $sql_old = "select id,title,created from mos_content;";
my $sql_new = "select id,title,created from xugc_content;";
my $sth_old = $dbh_old->prepare($sql_old);
my $sth_new = $dbh_new->prepare($sql_new);
$sth_old->execute();
$sth_new->execute();
my $ref_old;
my $ref_new;
while ($ref_old = $sth_old->fetchrow_hashref()) {
push #rv_old, $ref_old;
}
while ($ref_new = $sth_new->fetchrow_hashref()) {
push #rv_new, $ref_new;
}
my #seen = ();
my #notseen = ();
foreach my $i (#rv_old) {
my $id = $i->{id};
my $title = $i->{title};
my $created = $i->{created};
my $seen = 0;
foreach my $j (#rv_new) {
if ($i->{id} == $j->{id}) {
push #seen, $i;
$seen = 1;
}
}
if ($seen == 0) {
print "$i->{id},$i->{title},$i->{state},$i->{catid},$i->{created}\n";
push #notseen, $i;
}
}
The arrays look like this when using Dumper(#rv_old) to print them:
$VAR1 = {
'title' => 'Legal Notice',
'created' => '2004-10-07 00:17:45',
'id' => 14
};
$VAR2 = {
'created' => '2004-11-15 16:04:06',
'id' => 86096,
'title' => 'IRC'
};
$VAR3 = {
'id' => 16,
'created' => '2004-10-07 16:15:29',
'title' => 'About'
};
I tried to use grep() using array references, but I don't think I understand arrays, hashes, and references well enough to do it properly. My failed grep() attempts are below. I'd appreciate any ideas of how to do this properly.
I believe the problem with this is that I don't know how to reference the id field in the second array of hashes. Most of the examples using grep() that I've seen are to just look through an entire array, like you would with regular grep(1). I need to iterate through one array, checking each of the values from the id field with the id field from another array.
my $rv_old_ref = \#rv_old;
my $rv_new_ref = \#rv_new;
for my $i ( 0 .. $#rv_old) {
my $match = grep { $rv_new_ref->$_ == $rv_old_ref->$_ } #rv_new;
push #notseen, $match if !$match;
}
I also tried variations on the grep() above:
1) if (($p) = grep ($hash_ref->{id}, #rv_old)) {
2) if ($hash_ref->{id} ~~ #rv_old) {
There are a number of libraries that compare arrays. However, your comparison involves complex data structures (the arrays have hashrefs as elements) and this at least complicates use of all modules that I am aware of.
So here is a way to do it by hand. I use the shown array and its copy with one value changed.
use warnings;
use strict;
use feature 'say';
use List::Util qw(none); # in List::MoreUtils with older Perls
use Data::Dump qw(dd pp);
sub hr_eq {
my ($e1, $e2) = #_;
return 0 if scalar keys %$e1 != scalar keys %$e2;
foreach my $k1 (keys %$e1) {
return 0 if !exists($e2->{$k1}) or $e1->{$k1} ne $e2->{$k1};
}
return 1
}
my #a1 = (
{ 'title' => 'Legal Notice', 'created' => '2004-10-07 00:17:45', 'id' => 14 },
{ 'created' => '2004-11-15 16:04:06', 'id' => 86096, 'title' => 'IRC' },
{ 'id' => 16, 'created' => '2004-10-07 16:15:29', 'title' => 'About' }
);
my #a2 = (
{ 'title' => 'Legal Notice', 'created' => '2004-10-07 00:17:45', 'id' => 14 },
{ 'created' => '2004-11-15 16:xxx:06', 'id' => 86096, 'title' => 'IRC' },
{ 'id' => 16, 'created' => '2004-10-07 16:15:29', 'title' => 'About' }
);
my #only_in_two = grep {
my $e2 = $_;
none { hr_eq($e2, $_) } #a1;
} #a2;
dd \#only_in_two;
This correctly identifies the element in #a2 that doesn't exist in #a1 (with xxx in timestamp).
Notes
This finds what elements of one array are not in another, not the full difference between arrays. It is what the question specifically asks for.
The comparison relies on details of your data structure (hashref); there's no escaping that, unless you want to reach for more comprehensive libraries (like Test::More).
This uses string comparison, ne, even for numbers and timestamps. See whether it makes sense for your real data to use more appropriate comparisons for particular elements.
Searching through a whole list for each element of a list is an O(N*M) algorithm. Solutions of such (quadratic) complexity are usable as long as data isn't too big; however, once data gets big enough so that size increases have clear effects they break down rapidly (slow down to the point of being useless). Time it to get a feel for this in your case.
An O(N+M) approach exists here, utilizing hashes, shown in ikegami answer. This is much better algorithmically, once the data is large enough for it to show. However, as your array carries complex data structure (hashrefs) a bit of work is needed to come up with a working program, specially as we don't know data. But if your data is sizable then you surely want to implement this.
Some comments on filtering.
The question correctly observes that for each element of an array, as it's processed in grep, the whole other array need be checked.
This is done in the body of grep using none from List::Util. It returns true if the code in its block evaluates false for all elements of the list; thus, if "none" of the elements satisfy that code. This is the heart of the requirement: an element must not be found in the other array.
Care is needed with the default $_ variable, since it is used by both grep and none.
In grep's block $_ aliases the currently processed element of the list, as grep goes through them one by one; we save it into a named variable ($e2). Then none comes along and in its block "takes possession" of $_, assigning elements of #a1 to it as it processes them. The current element of #a2 is also available since we have copied it into $e2.
The test performed in none is pulled into a a subroutine, which I call hr_eq to emphasize that it is specifically for equality comparison of (elements in) hashrefs.
It is in this sub where the details can be tweaked. Firstly, instead of bluntly using ne for values for each key, you can add custom comparisons for particular keys (numbers must use ==, etc). Then, if your data structures change this is where you'd adjust specifics.
You could use grep.
for my $new_row (#new_rows) {
say "$new_row->{id} not in old"
if !grep { $_->{id} == $new_row->{id} } #old_rows;
}
for my $old_row (#old_rows) {
say "$old_row->{id} not in new"
if !grep { $_->{id} == $old_row->{id} } #new_rows;
}
But that's an O(N*M) solution, while there exists an O(N+M) solution that would be far faster.
my %old_keys; ++$old_keys{ $_->{id} } for #old_rows;
my %new_keys; ++$new_keys{ $_->{id} } for #new_rows;
for my $new_row (#new_rows) {
say "$new_row->{id} not in old"
if !$old_keys{$new_row->{id}};
}
for my $old_row (#old_rows) {
say "$old_row->{id} not in new"
if !$new_keys{$old_row->{id}};
}
If both of your database connections are to the same database, this can be done far more efficiently within the database itself.
Create a temporary table with three fields, id, old_count (DEFAULT 0) and new_count (DEFAULT 0).
INSERT OR UPDATE from the old table into the temporary table, incrementing old_count in the process.
INSERT OR UPDATE from the new table into the temporary table, incrementing new_count in the process.
SELECT the rows of the temporary table which have 0 for old_count or 0 for new_count.
select id,title,created from mos_content
LEFT JOIN xugc_content USING(id)
WHERE xugc_content.id IS NULL;
Gives you the rows that are in mos_content but not in xugc_content.
That's even shorter than the Perl code.
I have the following code:
print Dumper($dec_res->{repositories}[0]);
print Dumper($dec_res->{repositories}[1]);
my #repos = ($dec_res->{repositories});
print scalar #repos . "\n";
and the output is the following:
$VAR1 = {
'status' => 'OK',
'name' => 'apir',
'svnUrl' => 'https://url.whatever/svn/apir',
'id' => 39,
'viewvcUrl' => 'https://url.whatever/viewvc/apir/'
};
$VAR1 = {
'status' => 'OK',
'name' => 'CCDS',
'svnUrl' => 'https://url.whatever/svn/CCDS',
'id' => 26,
'viewvcUrl' => 'https://url.whatever/viewvc/CCDS/'
};
1
So my question is why $dec_res->{repositories} is clearly an array but #repos is not?
Here I printed the size but even trying to access elements with $repos[0] still returns an error.
Dumping $repos[0] actually print the whole structure... like dumping $dec_res->{repositories}
$dec_res->{repositories} is clearly an array
It isn't. It is an array reference.
but #repos is not?
It is an array.
You are creating a list that is one item long, and that item is the array reference. You then assign the list to the array, so the array holds that single item.
You need to dereference the array instead.
my #repos = #{$dec_res->{repositories}};
perlref explains more about references in Perl.
This should be a simple thing to solve...
I have got these estatements when a user logs in:
$query = $conn->prepare("SELECT * FROM admins WHERE username = :user AND password = :pass");
$query->bindValue(":user", $user);
$query->bindValue(":pass", md5($pass));
$query->execute();
Now if the user exists, I return the user information from the database, but if not, I return false instead...
if($query->rowCount() > 0) {
$admininfo = $query->fetchAll(PDO::FETCH_ASSOC);
} else {
return false;
}
But when I var_dump the variable $admininfo I get an array with a number key before the actual array... like this:
array (size=1)
0 => <---- I DONT KNOW WHERE THIS COME FROM. I USED PDO::FETCH_ASSOC
array (size=9)
'id' => string '1' (length=1)
'username' => string 'admin' (length=5)
'password' => string '21232f297a57a5a743894a0e4a801fc3' (length=32)
'permissionid' => string '1' (length=1)
'name' => string 'Administrador' (length=13)
'lastname' => string 'Softing' (length=7)
'phonenumber' => null
'cellphonenumber' => null
'info' => null
I will put this information inside the SESSION array, so I want to access it by $_SESSION["admininfo"]["username"] or $_SESSION["admininfo"]["phonenumber"]
but I have to put something like this instead: $_SESSION["admininfo"][0]["phonenumber"].
How can I remove that 0 from the keys? Thank you!
According to the documentation for fetchAll, it will return an array of elements. This is because it is getting all of the rows in the table that match your criteria. In your case, you are only getting back one row, located at index 0. If you know you only want one result, use fetch instead of fetchAll. It will get rid of the 0.
You're using fetchAll. This will give you an array of DB rows -- of all the rows that were matched.
Use fetch in a while loop to go through individual rows one by one.
What am I trying to do?
I have three fields (1 hidden, an id) and the user must complete one of the other two in order to pass validation.
So the user should fail validation if both fields are empty, but pass if one is completed.
1 2 3
A 0 B True
A B 0 True
A 0 0 False
I'm using CakePHP v2.1.3 so have access to the new validation rule enhancements.
The problem
I can't seem to find a reliable way to check both fields at the same time. I have so far tried looking at $this->data from the model and have found that validation is only passing a single instance of the data at a time. So there doesn't seem to be a way to compare the fields.
What I have so far
/**
* Custom validation to see if either of the two fields are set, if neither are, then we fail, if at least one is, we pass
* #param array $check
* #return boolean
*/
public function checkAttributes($check){
var_dump($check);
var_dump($this->data);
echo "<hr>";
// Check for an id value picked from a list
if(#is_numeric($check['attribute_value_id']) && isset($this->data['AdvertAttributeValue']['attribute_value_id'])){
return true;
}
// Check for a date value selected
if(#is_array($check['attribute_value_text']) && isset($this->data['AdvertAttributeValue']['attribute_value_text'])){
return true;
}
// Check for a text value
if(#is_string($check['attribute_value_text']) && isset($this->data['AdvertAttributeValue']['attribute_value_text'])){
return true;
}
return false;
}
This doesn't seem to do the trick as I think it can't check $this->data because the instance of it doesn't contain all the relevant fields.
The data
I should also mention that I am passing a large numeric array in. So these fields appear multiple times on the page, currently 12 dimensions. So accessing them directly through $this->data will be hard as they are not named dimensions, but are $this->data['Model'][<num>]['field'] = value
Validation
public $validate = array(
'attribute_value_id'=>array(
'notempty'=>array(
'rule'=>'checkAttributes',
'message'=>'Please select a value for your attribute',
'required'=>true,
),
),
'attribute_value_text'=>array(
'notempty'=>array(
'rule'=>'checkAttributes',
'message'=>'You must enter text for this attribute',
'required'=>true,
),
)
);
Data dump
Here I'll show the output of the var_dump() above. I have two validation rules in my Model, one for attribute_value_id and also one for attribute_value_text
// An id field selected from a list
array // $check
'attribute_value_id' => string '1' (length=1)
array // $this->data
'AdvertAttributeValue' =>
array
'attribute_value_id' => string '1' (length=1)
'id' => string '' (length=0)
// A text field
// Validating first time around on the id field
array // $check
'attribute_value_id' => string '' (length=0)
array // $this->data
'AdvertAttributeValue' =>
array
'attribute_value_id' => string '' (length=0)
'id' => string '' (length=0)
'attribute_value_text' => string '50' (length=2)
// Validating second time around on the text field
array // $check
'attribute_value_text' => string '50' (length=2)
array // $this->data
'AdvertAttributeValue' =>
array
'attribute_value_id' => string '' (length=0)
'id' => string '' (length=0)
'attribute_value_text' => string '50' (length=2)
// A date field
array // $check
'attribute_value_id' => string '' (length=0)
array // $this->data
'AdvertAttributeValue' =>
array
'attribute_value_id' => string '' (length=0)
'id' => string '' (length=0)
'attribute_value_text' =>
array
'month' => string '06' (length=2)
'day' => string '28' (length=2)
'year' => string '2012' (length=4)
The saveAll() method just calls saveMany() or saveAssociated() as appropriate. These methods will, by default, attempt to validate all of the data before saving any of it (via a call to validateMany()). However, as you can see in the source, that function validates each item individually, so the validation code won't have access to other records.
As I understand it, you need to cross-validate between multiple records before you save them. Although I've never done that, it sounds like a case for validation in the controller. You can make calls to Model::validate() or Model::validateAll() to ensure internal consistency of records. Then your controller can also implement whatever logic is necessary for your cross-record validation. Once that's done, you can make the save call with validation disabled:
$this->myModel->saveAll($this->request->data, array('validate'=>false));
Note that before you do any of this, you'll have to set your data to the model:
$this->myModel->set($this->request->data);
I realize this puts a lot of extra code in the controller that should ideally be in the model. I suppose it's possible that it could be done via one of the model callbacks, but I'm not sure how.
You can make use of Model::$data or Model::beforeValidate().
I have this $data array: (built on a shell, not a form)
(debugged here)
array(
(int) 0 => array(
(int) 0 => 's511013t',
(int) 1 => 'id3422',
(int) 2 => '1'
),
(int) 1 => array(
(int) 0 => 's511013t',
(int) 1 => 'id3637',
(int) 2 => '1'
)
)
And using saveMany :
$this->Dir->saveMany($data, array( 'validate' => 'false', 'fieldList' => array('name','dir_dataname', 'project_id')));
Saving fails with no error.
I'm not sure if my $data array is well formatted, (I'm confused whether it should have another level) I built it from sql selects, etc. However it does contain all info I need saved, single model.
I'm running all this from a Shell and it does work to save a single record provided the field names everytime:
// this works
$this->Dir->save(array('name' => $data[0][0], 'project_id' => $data[0][2], 'dir_dataname' => $data[0][1]));
Already read Saving your data, and I'd really like to use saveMany and a fieldList due to my custom $data format. (I wouldn't like to have to insert field names on my $data).
(no sql_dump to show since is pretty cumbersome to get it from a shell task)
I've spent all evening trying to figure it out, can you point me in the right direction, Please?
IMHO, the keys in each arrays are not valid fields in your database table. They should represent the same name as your table field.
When you build the array from sql, the output should look like these - an associative array:
array(
(int) 0 => array(
(string) name => 's511013t',
(string) dir_dataname => 'id3422',
(string) project_id => '1'
),
(int) 1 => array(
(string) name => 's511013t',
(string) dir_dataname => 'id3637',
(string) project_id => '1'
)
)
Cake2.0 Docs
$this->Dir->saveMany($data);
You can get the log via
debug($this->Dir->getDataSource()->getLog());
It looks as if you are using fieldList incorrectly. fieldList is a list of fields that are to be whitelisted for saving to the database, not a "mapping" like you are using.
You need to specify field => value pairs in the array for each record, not numerical indexes. I may be wrong, but I've never seen that and it doesn't look to be that way according to docs.