Foreach with Three Variables Add

Php foreach sorting with three variables

Code :

<?php
error_reporting('E_ALL');

$a = Array(Array("Los Angeles","City","los-angeles"),Array("San Francisco","City","san-francisco"),Array("San Diego","City","san-diego"),Array("United States","Country","united-states"));

foreach ($a as $key => $value) {
$return[$value[1]][] = array("name"=>$value[0],"slug"=>$value[2]);
}

foreach ($return as $key => $value) {
echo $key.":";
foreach($value as $newvalue){
echo '<span class="tags"><a href="'.$newvalue["slug"].'">'.$newvalue["name"].'</a> </span>';
}
echo "<br>";
}

Output:

City:<span class="tags"></span><a href="los-angeles">Los Angeles</a> <span class="tags"></span><a href="san-francisco">San Francisco</a> <span class="tags"></span><a href="san-diego">San Diego</a> <br>Country:<span class="tags"></span><a href="united-states">United States</a> <br>

Multiple index variables in PHP foreach loop

to achieve just that result you could do

foreach (array_combine($courses, $sections) as $course => $section)

but that only works for two arrays

foreach loop with 3 variables

An example of how to do it:

$a = 'file1.txt';
$b = 'file2.txt';
$c = 'file3.txt';

$a1 = explode("\r\n", file_get_contents($a));
$b1 = explode("\r\n", file_get_contents($b));
$c1 = explode("\r\n", file_get_contents($c));

for($id = 1; $id <= 3; $id++) {
$line = $id - 1;
echo "id: {$id} lang1: {$a1[$line]} lang2 {$b1[$line]} lang3: {$c1[$line]} <br>";
}

Foreach multiple variables

Maybe this will do the trick.

function get_order_item_lines($skus = '', $qty = '')
{
$xml = '';

if (!empty($skus) && is_array($skus))
{
foreach ($skus as $key => $sku)
{
$skuUpper = strtoupper($sku); // change to upper

if ($skuUpper != 'WANHAO-STICKER'
&& strpos($skuUpper, 'NWS-', 0) === false) // string not contains..
{
$xml .= "<L>".PHP_EOL;
$xml .= "<P>" . $sku . "</P>".PHP_EOL;
$xml .= "<Q>" . (empty($qty[$key]) ? "" : $qty[$key]) . "</Q>".PHP_EOL;
$xml .= "</L>".PHP_EOL;
}
}
}
else
{
$skus = explode( ",", $skus );

if ( !empty( $skus[0] ) && !empty( $qty[0] ) )
{
$skuUpper = strtoupper($skus[0]); // change to upper

if ($skuUpper != 'WANHAO-STICKER'
&& strpos($skuUpper, 'NWS-', 0) === false)
{
$xml .= "<L>".PHP_EOL;
$xml .= "<P>" . trim( $skus[0] ) . "</P>".PHP_EOL;
$xml .= "<Q>" . (empty($qty[0]) ? "" : $qty[0]) . "</Q>".PHP_EOL;
$xml .= "</L>".PHP_EOL;
}
}
}

return $xml;
}

foreach loop with 3 variables

An example of how to do it:

$a = 'file1.txt';
$b = 'file2.txt';
$c = 'file3.txt';

$a1 = explode("\r\n", file_get_contents($a));
$b1 = explode("\r\n", file_get_contents($b));
$c1 = explode("\r\n", file_get_contents($c));

for($id = 1; $id <= 3; $id++) {
$line = $id - 1;
echo "id: {$id} lang1: {$a1[$line]} lang2 {$b1[$line]} lang3: {$c1[$line]} <br>";
}

Add value into three different variables from Foreach-Object

PowerShell learned from Perl and Python, so you can do this:

$FTP_HOST, $FTP_USER, $FTP_PASS = (Get-Content .\config.cfg) -split ':'

Multiple variable pass in one Foreach loop in laravel blade

For merge and combine array , you can try :

$array = array_merge($array1,$array2);

Example in blade :

@foreach(array_merge($array1,$array2) as $item)
// ...
@endforeach

Another Example :

@foreach($array1 as $key => $row)
<li>
<b>{{ $row['id'] }}</b>
<p>{{ $array2[$key]->payment }}</p>
</li>
@endforeach

Related Links :

  • Laravel | Two arrays in foreach

  • https://codereview.stackexchange.com/questions/70419/combining-two-arrays-using-nested-foreach-loops

  • https://knackforge.com/blog/sabareesh/iterate-2-arrays-single-foreach-loop

  • Combine two arrays

  • http://php.net/manual/en/function.array-merge.php

  • https://www.w3schools.com/php/func_array_merge.asp

  • https://www.w3schools.com/php/showphp.asp?filename=demo_func_array_merge

  • https://laravel.com/docs/5.7/blade#the-loop-variable


The && operator(and) is a logical operator.

$a && $b And TRUE if both $a and $b are TRUE.

  • http://php.net/manual/en/language.operators.logical.php

  • https://laravel-news.com/blade-or-operator



Related Topics



Leave a reply



Submit