Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
<?php
$val=array(4,10,15);
$val2=array('ravi,suresh,kumar');
foreach ($val2 as $title) {
foreach ($val as $title1) {
echo $title;
echo $title1;
}
}
?>
o/p:
ravi,suresh,kumar4ravi,suresh,kumar10ravi,suresh,kumar15
but i requeied
ravi 4
suresh10
kumar15
Use one loop.
for($i = 0; $i < count($val); $i++) {
print $val[$i] . " " . $val2[$i];
}
The issue with your nested for loops is that it iterates over the first element of val1, then all the elements of val 2. Then the 2nd element of val1, and all the elements of val2 and so on.
since you want to go through both at the same time, use a standard for loop.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
how do i loop this kind of array?
$arr = array (
"aa"=>array("apple","orange"),
"bb"=>array("373","22"),
"cc"=>array("t0","h0"),
"dd"=>array("1","0")
);
I want to loop through the column of each item.
e.g.: i want to display ('apple','373','t0','1') at the first loop and ('orange','22','h0','0') at the last loop. thanks
We are assuming that all the arrays inside the main array are the same size in this example.
$arr = array (
"aa"=>array("apple","orange"),
"bb"=>array("373","22"),
"cc"=>array("t0","h0"),
"dd"=>array("1","0")
);
for($i = 0; $i<sizeof($arr["aa"]); $i++)
{
foreach($arr as $key=>$item)
{
echo($item[$i]);
}
echo ' - ';
}
Output: (obviously you can do any necessary formatting you need such as new lines or commas)
apple373t01 - orange22h00 -
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am using json_encode on foreach its return multiple values i want only one value after foreach completed
foreach($tree as $file) {
$arr = array('success'=>'ok');
echo json_encode($arr);
}
{"success":"ok"}{"success":"ok"}{"success":"ok"}{"success":"ok"}{"success":"ok"}{"success":"ok"}
Expected Output
{"success":"ok"}
Do like following:
foreach($tree as $file) {
$arr = array('success'=>'ok');
}
echo json_encode($arr);
Output will be as expected: {"success":"ok"}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How i can combine ad array of values into an array with double combination string without duplications ?
For example, if i have something like:
array('one','two','tre','four','five');
I want to obtain an array of combinations like this ('one / two') just one time and not also ('two / one').
In this way i wanto to get something similar to:
array('one/two', 'one/tre', 'one/four', 'one/five', 'two/tree','two/four' .......
Suggestions ?
You can do it with this code. It won't show two/one, three/two, etc (that's how I understood it):
<?php
$array = array('one','two','tre','four','five');
$newArray = [];
foreach ($array as $el) {
foreach ($array as $el2) {
if ($el === $el2) continue;
$newArray[] = $el."/".$el2;
}
array_shift($array); // remove the element we just went through
}
print_r($newArray);
Demo
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have two array i want to match a key of first array with another array and if the values of both key matches then add the value of second array to array 1
Array 1
[{"currency":1,"amount":23},{"currency":1,"amount":30},{"currency":2,"amount":40},]
Array 2
[{"currency_id": 1,"currency_symbol":$},{"currency_id":2,"currency_symbol":€}]
Desired output is:
[{"currency":$,"amount":23},{"currency":$,"amount":30},{"currency":€,"amount":40}]
The Code i am using is:
foreach($a1 as $key) {
foreach($a2 as $cKey){
if($a1['currency']==$a2['currency_id']){
$a1['currency_symbol'] = $a2['currency_symbol'];
echo $a1['currency_symbol'];
}
}
}
foreach($arr1 as $k=>$key) {
foreach($arr2 as $cKey){
if($key['currency']==$cKey['currency_id']){
$arr3[$k]['currency'] = $cKey['currency_symbol'];
$arr3[$k]['amount'] = $key['amount'];
}
}
}
var_export($arr3);
You only need to assign variable and print outside loop.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Kindly help me with my code:
$a[1]='cloropil';
$a[2]='rodopayta';
$r=2;
$c=1;
$f= while($c<=$r){ echo $a[$c];;
$e = $c++ };
while($c<=$r){
echo $f;
$r++;
}
while($c<=$r){
echo $e;
$r++;
}
I want its output to be like this:
$a[1]='cloropil';
$a[2]='rodopayta';
$r=2;
$c=1;
while($c<=$r){
echo $a[$c];
while($c<=$r){
echo $a[$c];
$c++;
}
$c++;
}
How ever the out put is blank.
it should even display at least "cloropil".
Is this even possible?
I want to display something like this:
cloropilcloropil
cloropilrodopyta
rodopytacloropil
rodopytarodopyta
thank you very much in advance.
I think this may be what you are looking for?:
<?php
$a[]='cloropil';
$a[]='rodopayta';
// You first iterate over each
for($i = 0; $i < count($a); $i++)
{
// As you iterate through the first round, you iterate
// again to match pairs but include the first iteration
// value with this second
foreach($a as $val)
echo $a[$i].'=>'.$val.'<br />';
}
?>
Gives you:
cloropil=>cloropil
cloropil=>rodopayta
rodopayta=>cloropil
rodopayta=>rodopayta