I looking for another samples of php foreach code that similar to the code as following:
foreach ($this->ask->post['books'] as $book) {
if ($book['qty']) {
$this->goto->add($book['book_id'], $book['qty'], (isset($book['opt'])) ? $book['opt'] : NULL);
}
}
I just want to save it as my collection, so, is there another samples of php foreach that You may know? let me know it. Thanks
Maybe this example can help you:
foreach($myArrayOfObject as $key=>$object) {
if($object->aPropertyOfMyObject) {
// do something...
}
}
You can also have a look to http://php.net/manual/en/control-structures.foreach.php
Do you specifically need foreach?
You can also use this:
<?php
$array = array(); // Imagine a filled array
array $max = count($array);
for($i=0;$i<$max;$i++) {
// Loop over the array
echo $array[$i]['name_of_key'];
}
?>
Related
I have 2 loops,
I would like that for each iteration of fighters2, $fighter1 also advance 1 element. Is is posible to do it with foreach????
foreach ($fighters1 as $fighter) {
foreach ($fighters2 as $fighter2) {
}
}
not sure if i understand the question correctly, but here is the answer to how i understood the question.
if your arrays are the same size you could do sth like this:
foreach($fighters1 as $index => $fighter) {
$fighter2 = $fighters2[$index];
//do what you need to do with $fighter and $fighter2
}
that is assuming both arrays are same size and have numeric indexes
Maybe using next() might help you.
foreach ($fighters1 as $fighter) {
$nextFighter1 = next($fighters1);
foreach ($fighters2 as $fighter2) {
//do whatever you need
}
}
next() reference
Bye!
I am trying to use item[1],item[2]... item[n] inside php functions like array_diff, max etc.
I use foreach loop like this to get the items
$n=1;
foreach ($arrays as $array) {
$item[$n] = $array;
$n++;
}
# how can I define item[1] through item[n] like:
print_r(max(item[1],item[2]...item[n]));
print_r(array_diff(item[1],item[2]...item[n]));
I searched but could not find a solution, please point me to the url if this has already been answered. Thanks.
Even though I don't see a problem, you can do this:
Hope it helps
foreach ($arrays as $k=>$array) {
$item[$k] = $array;
}
print_r(max($item));
print_r(array_diff($arrays,$item));
I'm trying to apostrophe to items in an array. and return that into another array
I know I can use foreach to display it but I need the items to be back inside an array.
from: array('blue','red','yellow');
to: array("'blue'","'red'","'yellow'");
please help
Yes.
Use this PHP function :
$array=array('blue','red','yellow');
foreach ($array as $key=>$item)
{
$new_array[$key]="'".$item."'";
}
print_r ($new_array);
With array_map :
function myfunc($n)
{
return("'".$n."'");
}
$a = array('blue','red','yellow');
$b = array_map("myfunc", $a);
print_r($b);
A slightly cleaner way:
function addQuotes($a)
{
return "'".$a."'";
}
$array = array('blue','red','yellow');
$array = array_map('addQuotes', $array);
A simple search would have landed you this very basic information though. Please give effort next time.
<?php
$interests[50] = array('fav_beverages' => "beer");
?>
now i need the index (i.e. 50 or whatever the index may be) from the value beer.
I tried array_search(), array_flip(), in_array(), extract(), list() to get the answer.
please do let me know if I have missed out any tricks for the above functions or any other function I`ve not listed. Answers will be greatly appreciated.
thanks for the replies. But for my disappointment it is still not working. btw I have a large pool of data like "beer");
$interests[50] = array('fav_cuisine' => "arabic");
$interests[50] = array('fav_food' => "hummus"); ?> . my approach was to get the other data like "arablic" and "hummus" from the user input "beer". So my only connection is via the index[50].Do let me know if my approach is wrong and I can access the data through other means.My senior just informed me that I`m not supposed to use loop.
This should work in your case.
$interests[50] = array('fav_beverages' => "beer");
function multi_array_search($needle, $interests){
foreach($interests as $interest){
if (array_search($needle, $interest)){
return array_search($interest, $interests);
break;
}
}
}
echo multi_array_search("beer", $interests);
If your array contains multiple sub-arrays and you don't know which one contains the value beer, then you can simply loop through the arrays, and then through the sub-arrays, to search for the value, and then return the index if it is found:
$needle = 'beer';
foreach ($interests as $index => $arr) {
foreach ($arr as $value) {
if ($value == $needle) {
echo $index;
break;
}
}
}
Demo
Is it possible to have an AND in a foreach loop?
For Example,
foreach ($bookmarks_latest as $bookmark AND $tags_latest as $tags)
You can always use a loop counter to access the same index in the second array as you are accessing in the foreach loop (i hope that makes sense).
For example:-
$i = 0;
foreach($bookmarks_latest as $bookmark){
$result['bookmark'] = $bookmark;
$result['tag'] = $tags_latest[$i];
$i++;
}
That should achieve what you are trying to do, otherwise use the approach sugested by dark_charlie.
In PHP 5 >= 5.3 you can use MultipleIterator.
Short answer: no. You can always put the bookmarks and tags into one array and iterate over it.
Or you could also do this:
reset($bookmarks_latest);
reset($tags_latest);
while ((list(, $bookmark) = each($bookmarks_latest)) && (list(,$tag) = each($tags_latest)) {
// Your code here that uses $bookmark and $tag
}
EDIT:
The requested example for the one-array solution:
class BookmarkWithTag {
public var $bookmark;
public var $tag;
}
// Use the class, fill instances to the array $tagsAndBookmarks
foreach ($tagsAndBookmarks as $bookmarkWithTag) {
$tag = $bookmarkWithTag->tag;
$bookmark = $bookmarkWithTag->bookmark;
}
you can't do that.
but you can
<?php
foreach($keyval as $key => $val) {
// something with $key and $val
}
the above example works really well if you have a hash type array but if you have nested values in the array I recommend you:
or option 2
<?php
foreach ($keyval as $kv) {
list($val1, $val2, $valn) = $kv;
}
No, but there are many ways to do this, e.g:
reset($tags_latest);
foreach ($bookmarks_latest as $bookmark){
$tags = current($tags_latest); next($tags_latest);
// here you can use $bookmark and $tags
}
No. No, it is not.
You'll have to manually write out a loop that uses indexes or internal pointers to traverse both arrays at the same time.
Yes, for completeness:
foreach (array_combine($bookmarks_latest, $tags_latest) as $bookm=>$tag)
That would be the native way to get what you want. But it only works if both input arrays have the exact same length, obviously.
(Using a separate iteration key is the more common approach however.)