foreach to a link - php

I have a problem.
I have written a web site where there are two PHP files where 1st contains variables and one array and next one contains next array with variables. Variables of second file are url adress. And now I need to know, if I can make something like:
with foreach
foreach($array01 as $word) {
$a = 0;
echo '<a href="'. $array_url[$a] .'><li>'.$word.'</li></a>';
$a = $a + 1;``
}
Output:
<li>var01</li>
<li>var02</li>
with foreach:
foreach ($array01 as $word) {
$a = 0;
echo '<li>'.$word.'</li>';
$a = $a + 1;
}
I know it does not works with this example but maybe there is a way to make it correct.

The problem in your code is that you're doing $a = 0; at the beginning of each loop. That undoes the $a = $a + 1; that you do at the end, so you're using $array_url[0] every time. You can fix that by putting $a = 0; before the loop instead of inside it.
But instead, you can tell foreach to give you the array indexes as well as the values. Then you can use that index to access the other array.
foreach ($array01 as $i => $word) {
echo '<a href="'. $array_url[$i] .'><li>'.$word.'</li></a>';
}

your question is unclear, you must specify your arrays
BUT
i think you should use for instead of foreach
for($a=0; $a < count($array01); $a++)
{
echo '<a href="'. $array_url[$a] .'><li>'.$array01[$a].'</li></a>';
}

What your asking is unclear but as I've understood from your question, you want to take an array of urls from a PHP file into another PHP file and output them as link in the HTML format.
If that's the case then follow this:
Your 2nd PHP file should look like this as far as I can guess::
<?php
//some codes..
$arrayofvariables=["word" => "url_address",....]; //your array of url address
//some codes..
?>
And in your 1st file do something like this::
<?php
include 'pathtoyour2ndfile'; // include your 2nd file with url array
foreach($arrayofurladdress as $word => $url_address){
echo "<li>$word</li>";
}
I could not figure what is the use of the variables and array in the first file. So, If this isn't what you're looking for please try to clarify your question by improving your English or upload all the content of your PHP files in the question.
Thanks. And A Happy New Year 2016. Hope my answer helps you. If it does don't forget to rate it and mark as answer.

Try This
$a = 0;
foreach ($array01 as $word) {
echo '<li>'.$word.'</li>';
$a++;
}
if your $array_url is like
$array_url = [ 'var_url01' , 'var_url02', 'var_url03'] ;
and your $array01 is like
$array01 = [ 'var01' , 'var02' , 'var03'];
then you can use the foreach loop as
foreach ($variable as $key => $value) {
echo '<li>'.$word.'</li>';
}

Related

Put each value of an array in a new variable in PHP

I have an array composed of files of a folder.
When I use the following snippet :
foreach($myarray as $key => $value)
{
echo $value. "<br>";
}
I have the following output :
vendor/templates/File1.docx
vendor/templates/File2.docx
vendor/templates/File3.docx
My question is : how to make it to put each value of my array in a new variable ? How to make it automatically if I have e.g 100 files in my folder ?
Actually I'd like to have (if my array is only composed of 3 items) :
$a = 'vendor/templates/File1.docx'
$b = 'vendor/templates/File2.docx'
$c = 'vendor/templates/File3.docx'
I guess I should use a loop but after many tests, I'm still getting stuck..
Have you any ideas ?
Thanks !!
Here is the solution:
<?php
$myarray = ['vendor/templates/File1.docx', 'vendor/templates/File2.docx', 'vendor/templates/File3.docx'];
foreach($myarray as $key => $value)
{
$varname = "var".$key;
$$varname = $value;
}
echo $var0."\n";
echo $var1."\n";
echo $var2."\n";
->
vendor/templates/File1.docx
vendor/templates/File2.docx
vendor/templates/File3.docx
May be following function will work for your problem.
extract($array);
http://php.net/extract

Get value from multi-dimensional array using variable

I want to get keys and values from a multi-dimensional array dynamically, to better explain what I'm trying to achieve please see the code below.
$i = 0;
foreach ($faq as $f) {
$q = 'faq'.$i;
$a = 'faq'.$i.'_answer';
echo $faq['faq1'][$i];
echo $faq['faq1_answer'][$i];
$i++;
}
The literal text above faq1 and faq1_answer needs to be replaced by the variable $q and $a respectively for me to be able to get the keys and values dynamically, but I cannot figure out how to add the variable.
The keys will always be the same, except for the number, which will change from 1 to 99. So with the code above, I can get the value of faq1 but I also need to grab the value of faq2 etc, hence why the variables above would work as I need.
tl;dr faq1 needs to be able to change to faq2 on the next iteration, hence the reason for me using $i.
Maybe like this?
$i = 0;
foreach ($faq as $f) {
$q = 'faq'.$i;
$a = 'faq'.$i.'_answer';
echo $f[$a];
echo $f[$a];
$i++;
}

Find if an array value shows up in a string. (Kind of a reverse in_array?)

On a site I'm working on there's a subheadline that only should be shown if the information isn't already shown in the main headline. The main headline is an arbitrary string, while the subheadline is created programatically. The subheadline is generated from the contents of a multi-dimensional array (the contents of which are also used for other parts of the page.)
I used the PHP example of foreach to drill down through the array (only half-understanding how it was working), and then tried strpos to see if the values in the array were in the headline string.
Unfortunately, it doesn't work. There is a high chance that I made a stupid mistake in how I thought it's supposed to work. Or that the variable that tells the site to hide the subhead ("hider") is constantly reset to "no" as a result of the other values in the array.
foreach ($arr_info as $i1 => $n1) {
foreach ($n1 as $i2 => $n2) {
foreach ($n2 as $i3 => $n3) {
$pos = strpos($headline, $n3);
if ($pos === false) {
$hider="no";
} else {
$hider="yes";
}
}
}
Any ideas? Greatly appreciate the help.
Add this:
$hider="yes";
break;
Hope it helps
I think a cleaner approach would be to construct a regex out of the values and see if it matches your string:
$values = array();
array_walk_recursive($arr_info, function($k){$GLOBALS['values'][] = preg_quote($k);});
$hider = preg_match('/(' . implode($values, '|') . ')/', $headline) ? 'yes' : 'no';

php arrays creating dynamically giving incorrect values

I want to create a array with values:
3,2,1.... and I want to use array_push and a forloop.
I have written the following code is not working..
============
<?PHP
$temp0=3;
$temp1=2;
$temp2=1;
$temp3=1;
$temp4=1;
$temp5=1;
$arraytemp=array();
for($i=0;$i<4;$i++)
{
$r="temp";
$dd=$r.$i;
array_push($arraytemp,$dd);
}
echo $arraytemp[3];
?>
can you please let me know what I am missing
This is how should you assign $dd
for($i=0;$i<4;$i++)
{
$dd=${"temp".$i};
array_push($arraytemp,$dd);
}
your $dd has the name of your var as a string. you want to use this for this technique:
array_push($arraytemp,$$dd);
Pay attention to the double $$ :)
What happens here is the following: the $dd gets replaced by the string it contains. so your call
array_push($arraytemp,$dd);
will do this:
array_push($arraytemp,'temp0');
But you want this:
array_push($arraytemp,$temp0);
so you need to show you want an acutal $var with that name, so you add the $. It's just the way the syntax works, neccessairy to distinguish between a normal string and a string that's supposed to be a variable
confusing what do you want to achieve here, do you want to:
create array with value: temp0, temp1, temp2 ...
for($i=0;$i<4;$i++){
array_push($array,"temp{$i}");
}
echo $array[3];
create array with value: 0, 1, 2, 3 ..
for($i=0;$i<4;$i++){
array_push($array,$i);
}
echo $array[3];
create array with value based on your defined variable above ($temp0, $temp1 ...)
$temp0=3;
$temp1=2;
$temp2=1;
$temp3=1;
$array = array();
for($i=0;$i<4;$i++){
$val = "temp{$i}";
array_push($array,$$val);
}
echo $array[3];
Easiest way, going by what you're requesting, although you didn't specify how many numbers you wanted to add. so for loop won't work that way. you're best off with a while loop.
$foo = array();
$i = 1;
while (some end condition) {
array_push($foo, $i);
$i++;
}
print_r($foo);

is there another samples of php foreach?

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'];
}
?>

Categories