+ 1
Return array index in PHP WITHOUT ANY FUNCTIONS
How can I write a code with nested loop to find a element within an array and return its index
20 Answers
+ 1
Ash
I edited the code to tell the break statement to exit inner and outer loop altogether. Just using `break;` only exits from inner loop. With `break 2;` we exits inner loop and outer loop with one break statement.
+ 1
Ipang yeah in 2d array ,i was just confused about that. can you help me with that ?
+ 1
i first tried this with one dem array to find
$a = [2,4,5,37,9,7];
for ($y = 0; $y < count($a) ; $y++){
if ($a[$y]== 9) {
echo $a;
}
}
+ 1
im trying this method with 2d array 2x2 with two loops but cannot seem to fit the if statement to compare the element. break the loop .
here is my code on which im working rn
r = [
[ 2,4] ,
[4,5]
];
for (e = 0 ; e < 2;e++){
for (t = 0;t < 2 ; t++){
if( r [t][e] == 5){
break;
}
}
}
i did not use dollar sign .
+ 1
Ipang the apache server shows time execution error when im trying to run it
+ 1
nope my bad ,it was minor error .Thnx man you helped a lot .im still new to these things
+ 1
Alright 😁 no problem at all 👍
Keep it up man!
+ 1
damn thnx man, actually ,i was incrementing $r in second loop as i have to iterate the $t in second loop . 😂
+ 1
Ipang need your help man .i use the same code for 3x2 array it shows for 3 row and 2 column array . sorry for bothering you man . can you make some adjustment to fix it . 🙏
+ 1
i did not do any modification . i thought i would use it with more arrays
+ 1
yup it works fine ,Thnx ipang ,but what if we tons of data dont know the row or column number and want to find an element in that array ? how is it gonna be possible
+ 1
Ipang thnx bro .for helping me out
0
Are you searching the value in a 2 dimensional array? why you think there's a need for nested loop? for a 1 dimensional array we don't need nested loop : )
0
Ash
Let me see your code first. I don't even know how far you have walked through this thing. I can't suggest a thing until I understand where you are facing difficulty.
0
Well, that is very close.
In that example with 1D array all you need is <$y>, as that is the index. You can `echo $y;`, or you can return $y if you make it into a function 👍
0
Ash
I'm afraid without $ (dollar sign) the code won't run. But anyways, here's your code with a little adjustment I did.
$r = [ [ 2,4] , [4,5] ];
for ($e = 0 ; $e < 2; $e++) // $e -> row
{
for ($t = 0; $t < 2; $t++) // $t -> column
{
if( $r [$e][$t] == 5)
{
echo "Found 5 at row[$e] column[$t]";
break 2;
// break from 2 loops
//inner & outer loops altogether
}
}
}
(Edited)
0
Show me the latest modification Ash
0
Okay, well, 3x2 means 3 rows of 2 columns each row right? correct me if I misunderstood.
We only need to adjust the row-based loop (outer loop) to run for 3 iterations rather than 2 iterations. Check if this is what you're after.
$r = [ [2,4] , [4,5] , [6,9] ];
for ($e = 0 ; $e < 3; $e++) // $e -> row
{
for ($t = 0; $t < 2; $t++) // $t -> column
{
if( $r [$e][$t] == 6)
{
echo "Found 6 at row[$e] column[$t]";
break 2;
}
}
}
0
Ash
In your original question above you mentioned "without any functions". Does that requirement still in relation to this?
I think PHP has built-in function for working with arrays. But if using built-in function is not a feasible option, then we'd have to manually calculate the number of rows and columns, before we go search the rows and columns.
Again, I'm not sure about efficiency when writing our own array search function, if we were dealing with thousands of values.
0
Ash
Check this out man.
In this code we get number of rows first. Then for each row we get number of columns. Having both number of rows and columns we can do the loops just fine.
But mind you this is not something you wanna use when there are loads of data, just not sure about efficiency and performance. Might need to search another way when there's tonnes of data I guess.
<?php
$arr = [
[1,9,3,7,5,7,5],
[6,70,82,97,140],
[29,38,42,56]
];
$num = 42; // number to find
$rows = count($arr); // number of rows
for($row = 0; $row < $rows; $row++)
{
// number of columns for row $row
$columns = count($arr[$row]);
for($col = 0; $col < $columns; $col++)
{
if($arr[$row][$col] == $num)
{
$res = [$row, $col];
break 2;
}
}
}
if(isset($res)) // [row, column]
echo "$num found at row $res[0] column $res[1]";
else
echo "$num not found.";
?>