Zend_Form_SubForm flattening array notation with getValues() - php

I am working with a series of forms that have subforms embedded into them and I am trying to work out if I can make getValues return the values without the array notation on the subform.
ie:
$form = new Zend_Form();
$subForm = new Zend_Form_SubForm();
$form->addSubForm( $subForm, 'contact' );
$form->addElement(new Zend_Form_Element_Text('name'));
$subForm->addElement( new Zend_Form_Element_Text('phone') );
var_dump($form->getValues());
Gives me the output:
array(2) {
["name"]=>
NULL
["contact"]=>
array(1) {
["phone"]=>
NULL
}
}
But I would actually like the output to be:
array(2) {
["name"]=>
NULL
["phone"]=>
NULL
}
Any easy way of doing this without overriding Zend_Form functions?

You can do it quite simply by using:
$subform->setIsArray(false);

Something like this may be a start:
$data = array();
foreach ($form->getSubForms() as $subform) {
$data += $subform->getValues();
}

Related

Avoid deep nesting when iterating through many combinations

Is there a more declarative / less horrible way of writing this (deliberately simplified) code?
I realise I can use ->each(), but that still doesn't get rid of the nesting in this case?
Note I do need to generate all the combinations, and I have a mixture of things to loop through - configuration data, ordinary arrays, Eloquent, though obviously I could convert them first...
foreach(config('app.goals') as $goal) {
foreach(config('app.age_groups') as $ages) {
foreach($regions as $region_name => $region_id) {
foreach($interests->pluck('name')->prepend('') as $interest) {
foreach(config('app.devices') as $device) {
$record = new Foo();
$record->goal = $goal;
$record->age = $age;
$record->region = $region_id;
$record->interest = $interest;
$record->device = $device;
$record->save();
}
}
}
}
}
Unclear if there's a Collection method that can help? e.g.
holygrail(config('app.goals'),config('app.age_groups'),$regions...)->each(function($combination) {
// logic for every combination
});
crossJoin() seems to do what I need, it creates an array matrix of every combination.
$matrix = collect($goals)->crossJoin(
$ages,
$regions,
$interests,
$devices
);
So you have an array full of elements, each of them a permutation, e.g:
array(5) {
[0]=>
string(5) "REACH"
[1]=>
string(5) "18-65"
[2]=>
string(9) "Worldwide"
[3]=>
string(11) "Programming"
[4]=>
string(7) "desktop"
}
The Laravel source is in: \Illuminate\Support\Arr::crossJoin

PHP is accessing nested arrays copying them?

Let's say I have an array (it really could have any depth):
$arr = array(
"lvl1" => array(
"lvl2 => ...
)
)
Now in a function I need to access it like this:
$path = array("lvl1", "lvl2", ...); // array of ordered indexes
$tmp = $arr;
foreach($path as $lvl){
...// other read-only/copy stuff happening on the array, no editing
$tmp = $tmp[$lvl];
}
At this point, just out of curiosity (no real optimization here), am I making copies of copies everytime? Or is it just using references automatically?
TL;DR If you are using PHP 7 the array won't be copied internally unless you change it. This is called copy-on-write.
To understand how PHP works under the hood you can read Reference Counting Basics:
A PHP variable is stored in a container called a "zval".
PHP is smart enough not to copy the actual variable container when it is not necessary.
Let's try to illustrate this on your simplified example using debug_zval_dump:
$array = [
'lvl1' => [
'lvl2' => [
'lvl3' => [
],
],
],
];
$path = ['lvl1', 'lvl2', 'lvl3'];
$tmp = $array;
foreach ($path as $lvl) {
debug_zval_dump($array);
$tmp = $tmp[$lvl];
}
debug_zval_dump($array);
If you run this code you will get the following output:
array(1) refcount(4){
["lvl1"]=>
array(1) refcount(1){
["lvl2"]=>
array(1) refcount(1){
["lvl3"]=>
array(0) refcount(1){
}
}
}
}
array(1) refcount(3){
["lvl1"]=>
array(1) refcount(2){
["lvl2"]=>
array(1) refcount(1){
["lvl3"]=>
array(0) refcount(1){
}
}
}
}
array(1) refcount(3){
["lvl1"]=>
array(1) refcount(1){
["lvl2"]=>
array(1) refcount(2){
["lvl3"]=>
array(0) refcount(1){
}
}
}
}
array(1) refcount(3){
["lvl1"]=>
array(1) refcount(1){
["lvl2"]=>
array(1) refcount(1){
["lvl3"]=>
array(0) refcount(2){
}
}
}
}
Pay attention to refcount: it changes, so internally PHP assigns by reference until you actually change the assigned value. You can read about this in the blog post by nikic:
The important difference to PHP 5 is that all variables were able to share the same array, even though some were PHP references and some weren’t. Only once some kind of modification is performed the array will be separated.

Add array in array of arrays

I have a $_SESSION index called "items".
Just like this:
$_SESSION['items'];
When user click to add item I check if $_SESSION['items'] exists. If exists, then insert the item, if not, create and insert. Ok.
So I coded this solution:
$newItem = array(
'id'=>$this->getId(),
'red'=>$this->getRef()
);
if(isset($_SESSION['items'])) {
array_push($_SESSION['items'],$newItem);
} else {
$_SESSION['items'] = $newItem;
}
Ok.
The problem is:
If the "else" occurs, the $newItem array is pushed into $_SESSION['items'] with this structure:
{
0: {
id: "1",
ref: "0001",
}
}
Exactly as I was expecting.
But if the "if" statement occurs, my $_SESSION['item'] looses the new indexes and I get a structure like this:
{
0: {
id: "1",
ref: "0001",
},
id: "2",
ref: "0001",
}
As you can see, the new item is not set as array...
If I add more itens, the issue affects only the last item added...
What I am doing wrong?
Change your code to the following:
if (isset($_SESSION['items'])) {
array_push($_SESSION['items'],$newItem);
} else {
$_SESSION['items'] = [];
array_push($_SESSION['items'], $newItem);
}
Now, all the $newItems will be pushed in to an actual array.
Output
array(1) {
["items"]=>
array(2) {
[0]=>
array(2) {
["id"]=>
string(2) "id"
["ref"]=>
string(3) "ref"
}
[1]=>
array(2) {
["id"]=>
string(4) "id-2"
["ref"]=>
string(5) "ref-2"
}
}
}
Live Example
Repl - Dummy data used
Your array_push here seems to be problem, because when you are pushing the array in $_SESSION[‘items’] it takes $newItem array elements and pushes them in the $_SESSION[‘items’]
If you can do as below then it should work
$newItem = array(
'id'=>$this->getId(),
'red'=>$this->getRef()
);
$_SESSION['items'][]= $newItem;

Use php to display specific "key-value" pair from the data retrieved from the mongodb

This is the php code.
<?php
// connect to mongodb
$m = new MongoClient();
// select a database
$db = $m->Example;
$collection="User";
$Query = array("Username"=>$username);
$j = $db->$collection->find($Query);
foreach ($j as $k) {
echo"<pre>";var_dump($k); echo"</pre>";
}
foreach($j as $k => $v) {
echo $k.'='.$j[$k].'<br>';
}
?>
In this, the data is retrieved in $j variable an when var_dump($k) is used the output is as follows:
array(8) {
["_id"]=>
object(MongoId)#6 (1) {
["$id"]=>
string(24) "56d1cb49097ed3241d000029"
}
["Fname"]=>
string(4) "Ritu"
["Lname"]=>
string(3) "Rad"
["Username"]=>
string(4) "riri"
["Password"]=>
string(4) "riri"
["Email"]=>
string(23) "ritikatra#gmail.com"
}
But if you try to display individual key value pair as in the next foreach loop you get the following error:
Fatal error: Cannot use object of type MongoCursor as array
How to display only a particular key and it's value?
eg: Email ritikatra#gmail.com
Result of \MongoCollection::find() (your $j) variable is an instance of \MongoCursor class which implements \Iterator - it allows you to loop over it but it doesn't have keys (i.e. doesn't implement \ArrayAccess). If you want to use your results as an array you should call
$array = iterator_to_array($j);
Now you can use $array as it'd be plain array:
echo $array[0]['Email']

Get all object from a "group" in PHP from JSON array

I'm trying to catch all objects in PHP from a JSON array, I need all the objects that will appear under ["Elements"]. So how would this be possible if I:
1.) Don't know the "name" of the object and don't know the content inside it.
2.) What I would like to achieve is to get the first objects value inside Elements, and then get the "content" inside of it, regardless of the names (there could be multiple objects)
Here is a var_dump of the JSON:
object(stdClass)#1 (1) {
["Canvas"]=>
array(1) {
[0]=>
["Elements"]=>
object(stdClass)#18 (2) {
["textHolder2"]=>
object(stdClass)#19 (1) {
["textContent"]=>
string(12) "Text to edit"
}
["textHolder1"]=>
object(stdClass)#20 (1) {
["textContent"]=>
string(12) "Text to edit"
}
}
}
}
}
Use foreach.
$json = json_decode( $input, true );
$elems = $json['canvas']['Elements'];
foreach( $elems as $key => $value ) {
echo "{$key} is an array/object:\n";
echo var_dump( $value );
}
You could use array_keys() if you need to know what keys are inside $value or you could another foreach loop, but I am assuming you will have at least some clue what keys could be in $value.

Categories