I want to make array like this for the Answer field:
$my_array = array("Melon-Daiquiri.jpg1_2.jpg","banner1.png","images.jpg");
After that I want to use Shuffle function to show this record randomly.
Here is my var_dump($array) Output. (coming from database)
Simply use array_map like as its an array of objects you can simply achieve it like as
$result = array_map(function($v) {
return $v->Answer;
},$your_array);
If your are getting your data from database you can use RAND() mysql method to shuffle your data. example
SELECT * FROM users ORDER BY rand() LIMIT 20
Related
i have some data in my notepad and i want to find out maximum values to minimum values with php My data Looks like This
a:849444988- b:10.0
a:748449844- b:15.0
a:940849847- b:20.0
a:474844994- b:55.0
and so on..
now how to arrange it with higher to lower
i want output that show uid 474844994 have 55 bal
First declare your file data as an array of lines using file().
Then perform a custom sort which leverages natural sorting on the numeric substring that trails b:.
From here, you can continue processing the data as an array, or implode it back into its original string form ...whatever your task requires.
Code: (Demo)
//$lines=file('your.txt');
$lines=[
'a:849444988- b:10.0',
'a:748449844- b:15.0',
'a:123456789- b:120.0',
'a:940849847- b:20.0',
'a:474844994- b:55.0'
];
usort($lines,function($a,$b){
return strnatcmp( substr($b,strpos($b,'b:')+2) , substr($a,strpos($a,'b:')+2) );
// $b is listed before $a in strnatcmp() to achieve DESC order
});
echo implode("\n",$lines);
Output:
a:123456789- b:120.0
a:474844994- b:55.0
a:940849847- b:20.0
a:748449844- b:15.0
a:849444988- b:10.0
First, you read your file into an array using file() this will create one array element per line. Then use arsort() to sort from high to low.
$arr = arsort(file('filepath.txt'));
print_r($arr);//output array
I have a table that is listed like this:
Table name: test
|--------------------|
|------Column 1------|
|--------------------|
|John,Raul,Matt,Tyler|
|Tim,Hannah----------|
|Zeke,Brady,Tim,Tyler|
|Elliot,Zeke---------|
|Matt,Andrew,Idda----|
...
And I need to get all of those names into a PHP array, without multiple cases of the same name.
Would I be better off getting each unique name via SQL, or should I just pull the column, and then parse through it via PHP?
The final PHP array would look like:
$test_array[0]="John"
$test_array[1]="Raul"
$test_array[2]="Matt"
$test_array[3]="Tyler"
$test_array[4]="Tim"
$test_array[5]="Hannah"
$test_array[6]="Zeke"
$test_array[7]="Brady"
$test_array[8]="Elliot"
$test_array[9]="Andrew"
$test_array[10]="Idda"
I'm not sure how I would parse each cell in the table. What would you guys do?
I agree it's best to normalize the database. However, given the situation you've described, I would perform the SQL select and use PHP to run through the results.
I'd initialize $results as an empty string:
$results = '';
Then I'd concatenate each row with $results:
$results .= $row;
Then I'd use explode() to put all of the names in an array:
$all_names = explode(',', $results);
Then I'd use array_unique() to reduces the $all_names array to unique values:
$unique_names = array_unique($all_names);
HTH.
My session variables are
$_SESSION["listofIds"]=163,164;
$_SESSION["listofVals"]=4,3;
I want to select session values like
$_SESSION["listofIds"][0]=163;
$_SESSION["listofIds"][1]=164;
$_SESSION["listofVals"][0]=4;
$_SESSION["listofVals"][1]=3;
I tried
echo $_SESSION["listofIds"][0];
then it prints '1' i.e. first character. How can I handle this?
Also how can I get 1st element of $_SESSION["listofIds"] and 1st element of $_SESSION["listofVals"] after placing in while loop for mysql query.
Your session variables seem to be strings. Use
$IdsArr = explode(",", $_SESSION["listofIds"]);
echo $IdsArr[0];
With explode splitting the string by "," to an array.
$listofIds = array();
$listofIds = explode(",",$_SESSION["listofIds"]);
now you can access the variables using listofIds[0] or listofIds[1].
First of all you should start by defining from scratch an array while placing values in the $_SESSION array. This is a better way to write code.
Anyway as all the other answers said:
$listofIds = array();
$listofIds = explode(",",$_SESSION["listofIds"]);
will convert your string into an array and you will be able to access each value in different ways. Most commons:
using a foreach loop
getting the index of the item you want to use.
If you want to use the first item in a query you should get its value using the index (array starts from 0) so your value will be $listofids[0]. The same logic will apply to the other $_SESSION value.
Another good practice for programming: when you put an array value in a query string assign it to a variable before:
$id_to_search = $listofids[0];
and then use it into the query:
$sql = "SELECT * FROM your_table WHERE id='$id'";
This will save you a lot of headache with singe and double quotes.
I got an array with ids like :
$array = array('1234','3214','8764','8467'2364','1113')
and what I do to retrieve the items data is to use the IN statement, like :
...product_id IN ('.$array .')...
The problem is that the data I get is given with an order which I don't know (e.g 8467,2364,3214,1234...).
Is there any way to get the data from mysql following the order provided in the array ?
Thanks in advance
Use order clause like,
$oderFields = implode(",",$array);
SELECT....ORDER BY field(product_id, $oderFields);
I think you can just use implode function to your array so you will have as string which you can use in your query
$array = array('1234','3214','8764','8467','2364','1113');
$string = implode(",", $array);
echo $string;
this will output
1234,3214,8764,8467,2364,1113
Now you can use this string in your query
product_id IN ('.$string.')
Which will be
product_id IN (1234,3214,8764,8467,2364,1113)
i want to know how to get all fields value search by array.
i have array that s
my table is
id content
1 zahr
2 hai
.
.
.
and so on
$a = {2,3,4,5,43,32};
i have to take the contents by this id(from array "a").
i know, i can use "for" loop for getting each element from mysql.
but i would like to use any filters or any predefined function
thanks and advance
SELECT * FROM table WHERE id IN (2,3,4,5,43,32)
the statement could be created by using implode like:
$myvar = '('.implode(',',$a).')';
and then
SELECT * FROM table WHERE id IN $myvar
use IN