The code is:
<?php
$notes = array("Main/folder/02/12.jpg = twelve",
"Main/folder/02/16.jpg = sixteen"
);
$imglist = array( "12.jpg",
"13.jpg",
"14.jpg",
"15.jpg",
"16.jpg"
);
for ($i=0;$i<(count($imglist));$i++){
if(in_array($imglist[$i], $notes)){
echo $imglist[$i];
//Get key($notes) and //Cant figure out how to implement this key();
echo $notes[$key];
} //if ENDS
} //for ENDS
?>
What I need is that for $i=0 and $i=4, if() should return TRUE & do the echoes.
The pseudo-code/logic is:
- Iterate through all the $imglist array one by one.
- if the complete $imglist[$i] string is found anywhere in $notes array, get the key/index of that value in $notes, and echo $notes[$key] and $imglist[$i]. OR Echo $notes[$key].
For example the desired output of this for loop should be:
12.jpg Main/folder/02/12.jpg = twelve
16.jpg Main/folder/02/16.jpg = sixteen
The problem is that now this all code is just outputting none. Means that if condition is not returning TRUE ever. Whereas in theory it should return TRUE when $i is 0 & 4. Is my if() condition wrong? Is php not taking $imglist[$i] as a string? Should I use strpos?
in_array()uses a loose comparison to search for elements and will not yield the results you’re looking for.Instead, you need to use your own logic (using
strpos()as you guessed) to implement the functionality you desire, like so:This will output: