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.
Related
I want to perform a multiple where clause using foreach statement inside lambda list expression. It is possible to do that? Please help me. Thanks. Here's my code:
//The cat values are "Toys,Accessories,Shirts"
var listCategories = cat.Split(',').ToList();
var GetProd = db.vwProducts.Where(x => foreach(var category in listCategories) { x.CategoryName.ToLower().Contains(category) && } x.Status == 1).ToList();
I already solved my question. You do not use the foreach to loop through list.I use the List statement to loop over the query. Here's my code.
//cat values "Toy,Shirt,Accessories"
List<string> categories = cat.Split(',').ToList();
var GetProd = db.vwProducts.Where(x => categories.Contains(x.CategoryName) && x.Status == 1).OrderBy(x => x.Name).ToList();
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 .
I want to change a row field in my ResultSet before returning it to my Controller.
$resultSet->buffer();
foreach ($resultSet as $row) {
$row->foo = $newvalue;
}
return $resultSet;
Problem is, when I use the buffer() function I can indeed loop over my ResultSet and make some changes on my rows, but once the loop ends all changes are gone.
I tried to set up a reference on $row :
foreach ($resultSet as &$row)
But then caught the following exception :
Fatal error: An iterator cannot be used with foreach by reference
I also tried to change resultSet to array but the same problem occurs.
Have I missed something ?
I don't think it is possible via the usual ResultSet usage. The array solution might work only if you are going to use the array in loops (foreach() in this case).
From any Table class -
$arr_resultSet = array();
foreach ($resultSet as $row) {
$row->foo = $newvalue;
//Object is assigned instead of converting it to an array.
$arr_resultSet[] = $row;
}
return $arr_resultSet;
Usage of this array in controller or view file -
//Here you can access that $row object as if the $resultSet was never converted to array.
foreach($arr_resultSet as $row) {
echo $row->foo;
}
No need of buffer(). I hope it works for now. Will definitely search for a proper solution.
I have the following in php:
$follow=explode(" ",$_SESSION['Following']); //create array from the string stored in session variable
foreach($follow as $val) {
$show = $val;
//my query
$result=mysqli_query($dbc,$query);
WHILE ($rows = mysqli_fetch_assoc($result)) {
//$array[]= $rows; // tried this
//$array=json_encode($rows); //tried this
//array_push($array,$rows); // tried this
}
$json_array=json_encode($array);
echo $json_array;
If I take a single pass through the foreach loop the json object looks like this:
[{key:value}....], which can be parsed in my javascript.
However, with multiple passes in the foreach I am getting multiple arrays
within the object ,like this: [{key:value}][{key:value}]..... which results in the following
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data, which I guess are the []'s inside the object. How can I create the json object in the foreach loop to fix this?
Fixed it. I was echoing $json_array inside the foreach loop.
I'm doing some LINQ to look for keywords typed in a textbox. Each keyword ends with ";" and i need to look for itens that contain at least one of those keys.
I thought that i would be able to achieve this with this loop
IEnumerable<ItemFAQ> allResults = null;
foreach (var item in termosPesquisaIsolados)
{
IEnumerable<ItemFAQ> tempResults =
_dbHelpDesk.ItemFAQ
.Where(x => x.DescricaoResposta.Contains(item) || x.TituloTopico.Contains(item))
.ToList()
.Select(x => new ItemFAQ
{
IdPergunta = x.IdPergunta,
TituloTopico = x.TituloTopico,
DescricaoResposta = x.DescricaoResposta.Substring(0, 100)
});
if (allResults != null)
allResults = allResults.Union(tempResults);
else
allResults = tempResults;
}
At first iteration tempResult returns in a test 3 elements then it passes then to allResult, everything ok with that.
The second iteration, tempResult returns 2 ocurrences... then according to code allResult should receive the Union of AllResult and the TempResults, but no matter what i do, when i use the Union clause the result in an Empty Set.
So 3 + 2 = 0 at the end after using Union.
Do you guys can see the mistake at this peace of code or know some issues that could lead to this error ?
Thanks for your time.
try replacing this line:
allResults = allResults.Union(tempResults);
with:
allResults = allResults.Union(tempResults).ToList();