How to get certain part of a string value in php? [closed] - php

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 6 years ago.
Improve this question
I get one value like "13and45" and would like to know if there is a way so I can store these values separately like
$string="13and45";
$value1=13;
$value2=45;
Do you guys know how I can manipulate the string in order to get something like what I have above?

PHP - Explode () function
This will return an array with the two values.
Example:
$array = explode ("and", $string);
Returned array with strings:
$array[0] = 13
$array[1] = 45

You can use something like intval(substr($string, 0, 2) to get the first integer and intval(substr($string, 5, 2) for the second. intval will get the integer value from a string, and substr will get the portion of the string given by ($string, index, length).

Related

How to sum two rows input file numbers to one row output file (PHP) [closed]

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 9 months ago.
Improve this question
Input data:
In the input file INPUT.TXT there are two non-negative integers are given by two rows and the numbers are less than 10 powered by 100.
Output data:
in the OUTPUT.TXT file is needed to return sum of the numbers to one row, without initial zeros
Example:
#
input.txt
output.txt
1
3
7
4
Check it , It will help you
$data = explode("\n", file_get_contents('INPUT.TXT'));
$sum = intval($data[0])+intval($data[1]);
echo $sum;
file_put_contents('OUTPUT.TXT', $sum);

Dissect, sort and compare arrays [closed]

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 4 years ago.
Improve this question
I have the two following arrays:
a:4:{i:0;s:2:"98";i:1;s:2:"98";i:2;s:2:"89";i:3;s:2:"99";}
a:4:{i:0;s:11:"Musculation";i:1;s:3:"Gym";i:2;s:22:"Production in HTML/CSS";i:3;s:9:"Endurance";}
Each array has 4 values that are correlated. I want to display only three values from second array that have the highest correlating number values from first array:-
Endurance - 99
Musculation - 98
Gym - 98
How do I achieve this?
Use array_multisort to sort the text according to the corresponding numbers
array_multisort($numbers, SORT_DESC, SORT_NUMERIC, $text);
Take the first three values.
$result = array_slice($text, 0, 3);
If you want to show the numbers with the text, the keys will still match up, so you can iterate the text array and use its key to get the right value from the number array.
foreach ($text as $key => $title) {
echo "$title: $numbers[$key]\n";
}

Which string(in query) are empty? [closed]

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
So how to detect which string are empty in the query? I mean:
I have few WHERE clause in one query
$query = SELECT var FROM table WHERE var = '$y';
and how can I detect which "$y" has no result?
I know I can use if($y), but how can I detect which was empty?
Declare an array of your Y variables:
var $myYs =array($y1, $y2, ... $yn);
Then make a loop to count the query results of each of your Y. Then check if the count of that particular query was equal to 0, still inside the loop.
foreach ($myYs as $checkThsYnow){
$query = SELECT COUNT(var) FROM table WHERE var = '$checkThsYnow';
if ($query =0) {echo $checkThsYnow." is empty"}
}
Based on your comments additionally you need to check all combinations of your Ys.. as it is possible, that every Y brings back a result, but Y1 and Y2 AND together resulting the 0 results. Its a mathematical combination issue. Need to check all combinations..Loop in a loop in a loop..etc.

Convert array to associative array with specific key and value [closed]

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
I need to convert an array into associative array as first element should act as a key and second element should act as its value ?please tell me how i can do that
If you have only two elements (first and second as you mentioned), then you can do simply like this
$assoc = array($simple[0] => $simple[1]);
If you wanted to convert pairs of values like [1,2,3,4] to [1=>2,3=>4], then use this code snippet
$assoc = array();
for ($i = 0; $i < count($simple); $i=$i+2) {
$assoc[$simple[$i]] = $simple[$i+1];
}

'IF' statement doesn't work right? [closed]

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 8 years ago.
Improve this question
When I print out the integers (1 or 0), all the iterations (there are 3 of them in this case) print out the correct number. First and the second one return 1, the last one returns zero. BUT, 'if' statement seems to display everything in it anyway. What am I doing wrong?
All the code below is inside a bigger 'for' loop.
$yn = 0;
if(!in_array($pos, $blocks)){
$blocks[$x] = $pos;
$x++;
$yn = 1;
}
print_r($blocks);
print "YN: ".$yn; # this prints out 1, 1 and 0 on the last iteration
if(yn){
# show some stuff (is displayed in all three iterations, but it shouldn't be on the last)
}
Try:
if($yn){
PHP is interpreting yn as a string, rather than as the variable you should be using.

Categories