How do you echo only the first line of print print_r?
More info:
I have this PHP code:
preg_match_all('/MbrDtlMain.php\?([^ ]+)>/i', $string, $matches);
foreach(end($matches) as $key=> $value){
print print_r($value, 1).'<br>';
}
That results in:
12567682
12764252
12493678
14739908
(or other numbers depending on user input)
I tried:
preg_match_all('/MbrDtlMain.php\?([^ ]+)>/i', $string, $matches);
foreach(end($matches) as $key=> $value){
$id = print_r($value, 1).'<br>';
}
echo $id
But it results in 1 random number from the list. In other words, the result only shows when using print like ' print print_r($value, 1).'<br>';'. The problem is that I only want the first, inorder, result to be shown. As if:
$firstlineofnumbers = '12567682';
echo $firstlineofnumbers;
Hope this makes sense. Thanks (:
If I understood what you're trying to do, just adding a break; statement after outputting the first value should be enough:
foreach(end($matches) as $key=> $value){
print print_r($value, true).'<br>'; // print_r() expects true, not 1
break;
}
If the keys in $matches are always numeric keys, this code should be enough:
echo $matches[0];
Otherwise, try this code:
$keys = array_keys($matches);
echo $matches[array_shift($keys)];
$keys will contain all keys of $matches.
array_shift will return the first value of $keys (the first key).
So the last line will display the corresponding value.
There is no need to loop through the entire array if you only need to display the first element.
preg_match_all('/MbrDtlMain.php\?([^ ]+)>/i', $string, $matches);
$i=0;
foreach(end($matches) as $key=> $value){
$i++;
if ($i == 1) {
echo $value."<br />";
}
}
This starts with the variable $i which increases by 1 for each match. If $i == 1, then it will echo the $value.
Related
I want to echo one element of the array like sum1. But I only get one letter like s. Please solve this problem.
$nums = array("sum1", 100, 200);
foreach ($nums as $value) {
echo $value[0];
echo $value[1];
}
If you want to just echo 1 item from that array, you should to it like this:
echo $nums[0];
If you want to loop through all of them, and show each, do it like so:
$nums = array("sum1", 100, 200);
foreach ($nums as $value) {
echo $value."<br>";
}
What you did wrong
You had already looped through the array, so you had a string. You can select the first letter from a string like in this example:
$string = "A string";
echo $string[0];
Will return A, as it's the first index of that string. That is essentially what you did in your loop.
You made your String an array, and it showed the index's you selected to be shown. You can read this where the questions asks how to do this. I hope this gives some more clearity.
If you want every element of array,
then,
For your array,
$nums = array("sum1", 100, 200);
$nums[0] will be sum1
$nums[1] will be 100
$nums[2] will be 200,
Now your loop,
foreach ($nums as $value) {
// here echo $value values are like 'sum1', 100, 200 will be printed.
// by default string will be considered as array,
// if you print $value[0], $value[1], $value[2], $value[3] for sum1, it will return, s, u, m, 1 respectively.
// and integers will be considered as pure value, which you will get in $value only, not in $value[0], ....
}
I hope I explained your concern.
Thanks.
I've an array got with preg_match_all.
This array contain 4 values and each value is a number.
I want to display ONLY the value that have 10 or more character.
Example:
Array_Value1 = 1234567890
Array_Value2 = 01234
Array_Value3 = 449125
Array_Value4 = 991234581210
I want to show with an echo only Array_Value1 and Array_Value4 because formed of 10 or more characters!
How can I do it?
I tried with count_char or str_len but I see a message that say "Array to String conversion".
Anyone can help me?
first of all you have to put all your values inside an array:
$array = array(1234567890, 01234, 449125, 991234581210);
after it you can use a simple foreach:
foreach($array as $value) {
if(strlen((string)$value) >= 10)
echo $value;
}
you should add a separator after each echo, or you will have an output like
1234567890991234581210
foreach ($values as $value)
{
if (strlen((string)$value) >= 10)
echo $value;
}
Try this-
foreach($my_arr as $val)
{
if(strlen((string)$val) >=10)
// more than or equal to 10
else
// less than 10
}
Try this:
<?php
$values = array(123123123, 53453453452345, 123, 9979797979797979);
$string = implode(',',$values);
preg_match_all('/(\d{10,})/', $string, $matches);
print_r($matches[1]);
?>
Maybe it's more time-consuming... hehe
I have a row returned from DB and i need to change the specific markup characters used in cols to the right characters for the display:
$row = mysqli_fetch_array($res, MYSQLI_ASSOC); //the result of query is 1 row
$markup = array("#", "#", "&", "$"); // this is our markup
$meaning = array("↑", "↓", "(dil.)", "(conc.)"); // this is our meaning of the markup for the display
foreach ($row as $k => $v) { //for each element of the array we change one char for the other
$v = str_replace ($markup, $meaning, $v);
echo "<br>$v";
}
UPD now its getting more weird. I did a mistake by tracing if the str_replace before the change was made to array, i fixed it ant tracing shows that it works! But there is a specialised output of this array down below and it still appears with unreplaced characters!
$reagent_no = "reaction_l" . $i; // determining the column number, irrelevant to the issue
echo "<td>\n<h3>{$row[$reagent_no]}</h3></td>"; //output of the supposedly
//changed characters, but it still outputs the markup, as if i didnt do anything to the array previously
You are first printing the variable and then using the str_replace() function. Change it to:-
foreach ($row as $k => $v) { //for each element of the array we change one char for the other
$v = str_replace ($markup, $meaning, $v);
echo "<br>$v";
}
The problem is that the str_replace is executed after the echo.
Try:
foreach ($row as $k => $v) { //for each element of the array we change one char for the other
$v = str_replace ($markup, $meaning, $v);
echo "<br>$v";
}
Heres what i mean:
foreach ($array as $a) {
echo $a.',<br/>';
}
current output would be:
a,
a,
a,
a,
i want the output to be like this:
a,
a,
a,
a
(all 'a' separated with comma and when it comes to the last loop it doesnt write a comma)
Try this:
echo implode(",<br/>", $array);
If the length of array is too large or you have multidimensional array use the below code
<?php $len=count($array);
foreach($array as $a){
echo $a;
if( $len > 1) echo ',';
$len--;
} ?>
If you'd also like to convert any newlines in the array to <br />, which might be ideal if you're outputting:
echo nl2br(implode(',' . PHP_EOL, $array));
PHP has the implode function for that:
implode(",<br>", $array);
Nobody said you can make it this way:
foreach($array as $element) {
$separator = ($element != end($array)) ? ",<br />" : '';
// or $separator = ($element == end($array)) ? '' : ",<br />";
echo $element.$separator;
}
I suppose this is going to output exactly what you wish.
You should use implode except for one situation.
If the output is huge, and you don't want to keep it in memory before sending to the output (e.g. itemwise processing) then you should do something like:
$remain=count($array);
foreach ($array as $a) {
echo $a;
if($remain-->0) echo ',';
echo '<br/>';
}
i have 2 queries, and i get back some data by doing a whyle loop.
from the first query if i do print_r($final_key);i get:
hey1
hey2
hey3
from the second one, if i do print_r($final_key2); i get:
hey1 test
hey2 test1
hey3
what i am trying to do is to compare the 2 arrays and check for words that match
and i can't do it directly from the database
any ideas?
thanks
edit:
here is my query 1:
while ($keywords = mysql_fetch_array($keys1, MYSQL_ASSOC)){
foreach ($keywords as $key) {
$plus = '+';
$pos = strripos($key, $plus);
if ($pos === false) { } else {
$clean_plus = preg_replace("/[\+]/", '', $key);
$final_key = str_replace("'", "", $clean_plus);
print_r($final_key);
echo '<br>';
}
}
}
and the second one:
<?php
while ($keywords = mysql_fetch_array($keys)){
if($keywords['kword'] != ''){
echo $keywords['kword'];
} } ?>
i am tryingt o match $final_key against $keywords['kword'];
Use array_intersect:
array_intersect($array1, $array2, ...);
from the array_intersect documentation:
array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.
Use array_intersect which returns an array containing values in both arrays.
If you want the keys to match, use array_intersect_assoc.
Loop over one array, and on each iteration, loop the other.
<?php
foreach($array as $item){
foreach($array2 as $item2){
if($item == $item2){
$array3[] = $item;
}
}
}
?>
This will create an array of matches, which you can then do with as you will.