2 variable foreach loop in Velocity Template Language - foreach

Is there any way to use 2 variables in foreach loop ?
#foreach( $name in $names && $method in $methods )
$name : $method
#end
Because I want to print the result side by side

Just loop over an index.
Assuming both collections have the same size:
#set($size = $names.size - 1)
#foreach( $i in [0..$size])
$names[$i] : $methods[$i]
#end
(edited for correctness)

I found the solution, just put in the Map
Map<String, String> maps = new HashMap<>();
ctx.put("rows", maps);
and iterate over key in maps
#foreach( $key in $rows.keySet() )
Key: $key Value: $rows.get($key)
#end

Related

How do I Store an Array Variable Inside of Another Array Variable using while loop and for each?

How do I Store an Array Variable Inside of Another Array Variable using while loop and for each?
Like this(array inside array)
please chec Foreach for arrays inside of an array
foreach($resultArray as $row => $innerArray){
foreach($innerArray as $innerRow => $value){
echo $value . "<br/>";
}
}
also check this link : Foreach for arrays inside of an array
You are probably looking for
<?php
$format = array('Roman' => array('one'=>'I','two'=>'II'),
'Arbic' => array('one'=>'1','two'=>'2'));
while (list($key, $value) = each($format)) {
echo $value;
foreach( $key as $key1 => $value1) {
echo $key1 .'='.$value1;
}
?>
I have not tested .

How to access array from nested foreach loop in PHP?

How can I access variable which is an array in nested for each loop?
foreach($nodes as $node){
foreach ($instances as $name => $instance) {
$items = field_get_items('node', $node, $instance);
echo $items;
}
}
print_r($items);die;
This code is giving me
ArrayArray
And when I do this instead:
foreach($nodes as $node){
foreach ($instances as $name => $instance) {
$items = field_get_items('node', $node, $instance);
print_r($items);die;
}
}
It gives:
Array(
[0]
[value]
<p>hbjhbfg</p>
)
How can I access $items outside of foreach loop?
You can simply loop through the elements of $items in the same way you're looping through $nodes.
foreach($items as $item) {
foreach($item as $itemValue) {
echo $itemValue;
}
}
However, mind that this line:
$items = field_get_items('node', $node, $instance);
overwrites $items at every iteration, therefore when the outer loop ends, you have only the value from last iteration of both "for" loops.
You should append the result of field_get_items to $items, otherwise the first piece of code is pretty inefficient.

Zend Framework 2 PDO "not like" operation

I need to make a "not like" operation in a where. I know that i can do this:
$predicate->like($a,$b);
But i can't find a way to perform a "not like" and other negated like "not in". Is there any way or i will have to make string where?
Thanks.
As you mention there is no notLike method on where object. But there is literal method where you can pass anything you want:
$sql = new Sql($adapter);
$select = $sql->select();
$select->from('foo')
->where->literal('NOT LIKE ?', array('bar'));
echo $select->getSqlString();
The output wille be:
SELECT "foo".* FROM "foo" WHERE NOT LIKE 'bar'
For reference to those who want to add the Object method of notLike() instead of using literal()
$predicate->notLike( $a, $b );
Create ZF2/Db/Sql/Predicate/NotLike.php
namespace Zend\Db\Sql\Predicate;
class NotLike extends Like {
protected $specification = '%1$s NOT LIKE %2$s',
}
In ZF2/Db/Sql/Predicate/Predicate.php add
public function notLike( $identifier, $like ) {
$this->addPredicate( new NotLike( $identifier, $like ), ($this->nextPredicateCombineOperator) ? : $this->defaultCombination );
$this->nextPredicateCombineOperator = null;
return $this;
}
Test
$select = new Select;
$select->from( 'dbTable' )
->columns( array() )
->where->notLike( 'column', '%test%' );
echo $select->getSqlString();

In Symfony, How to iterate over the data returned by doSelect?

In Propel ( using in Symfony ), I am using the following:
$data = ObjectPeer::doSelect($myCriteria); // $myCriteria is Criteria Object
How can I iterate over the $data?
I guess:
foreach ($data as $object)

Why does my attempt to trim strings in a List<string> not appear to work?

I tried the following code in LINQPad and got the results given below:
List<string> listFromSplit = new List<string>("a, b".Split(",".ToCharArray())).Dump();
listFromSplit.ForEach(delegate(string s)
{
s.Trim();
});
listFromSplit.Dump();
"a" and " b"
so the letter b didn't get the white-space removed as I was expecting...?
Anyone have any ideas
[NOTE: the .Dump() method is an extension menthod in LINQPad that prints out the contents of any object in a nice intelligently formatted way]
you're just creating a trimmed string, not assigning anything to it.
var s = " asd ";
s.Trim();
won't update s, while..
var s = " asd ";
s = s.Trim();
will..
var listFromSplit = "a, b".Split(',').Select(s=>s.Trim());
would, i suppose, be how i'd go about it.
The String.Trim() method returns a string representing the updated string. It does not update the string object itself, but rather creates a new one.
You could do this:
s = s.Trim();
However you cannot update a collection while enumerating through it so you'd want to either fill a new List while enumerating over the existing one or populate the List manually using the string array returned by String.Split.
Filling a new list:
List<string> temp = new List<string>("a, b".Split(",".ToCharArray()));
List<string> listFromSplit = new List<string>();
temp.ForEach(delegate(string s)
{
listFromSplit.Add(s.Trim());
});
listFromSplit.Dump();
Populating Manually:
string[] temp = "a, b".Split(",".ToCharArray());
List<string> listFromSplit = new List<string>();
foreach (string s in temp)
{
listFromSplit.Add(s.Trim());
};
listFromSplit.Dump();
Further to the answer posted by Adrian Kuhn you could do the following:
var result = listFromSplit.Select(s => s.Trim());
The string instances are immutable. Anything that seems to modify one, creates a new instance instead.
You are not assigning the trimmed result to anything. This is a classic error, I've only just got out of the habit of making this mistake with string.Replace :)
I have no IDE up and running, but this should get the job done (unless I am wrong):
var result = from each in listFromSplit select each.Trim();
Split on both spaces and commas and remove any empty entries. All nice and trimmed. Assumes that your strings don't contain spaces, though.
List<string> listFromSplit =
new List<string>( "a , b ".Split( new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries ));
The linq options others have provided should work well. As another option, here is an extension method using a for loop:
public static void TrimCollection(this IList<string> stringCollection) {
for (int i = 0; i <= stringCollection.Count() - 1; i++)
stringCollection[i] = stringCollection[i].Trim();
}

Resources