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
I'm making simple test script with PHP+mysql.
The script gives the question and SELECT with 4 options (answers).
1 of them is correct, 3 others are incorrect, retrieved from mysql table.
I have 4 variables and all works fine, but I need to retrieve these variables in random order.
$variable1='1';
$variable2='2';
$variable3='3';
$variable4='4';
Just need some direction what to learn, what method to use.
Many thanks in advance
SELECT data from MySQL
Store in an associative array
shuffle the array
http://codepad.org/6R9LsGIy
<?php
$arrAnswers = array("a", "b", "c", "d");
shuffle($arrAnswers);
print_r($arrAnswers);
/* Example Ouput:
Array
(
[0] => b
[1] => d
[2] => a
[3] => c
) */
As you've said you have these variables;
$variable1='1';
$variable2='2';
$variable3='3';
$variable4='4';
$arrAnswers = array($variable1,$variable2,$variable3,$variable4);
There are more elegant ways, but with the information given, this is the best I can do :)
Related
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 10 months ago.
Improve this question
$matchThese = array('product_id' => 8789, 'MONTH(date_created)' => '04');
$result = ExpiryPrepayment::firstOrNew($matchThese);
$result->payment_status = 1;
$result->save();
***====>'MONTH(date_created)' => '04'***
Help me get condition only month or year in column date_created
Why are you sending an array at a firstornew? This is basically a string that you are building an array with in the eloquent. Inserting an array will not work. Really the product_id should be a unique so you only need that value in your where.
So it would be something like this...
firstOrNew(['product_id' => $product_id]);
$product_id being the variable of the unique id.
Then if you have tons of products to add just loop over the array of product ids and it will either make a new row at your save or update the product when it finds it first.
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 4 years ago.
Improve this question
$rating = $rowsRaing;
Array
(
[0] => stdClass Object
(
[ratting] => 2.5
)
)
I want rating value.
I want user average number of ratting help me
Thanks
Your query should be
$rowsRaing = $wpdb->get_results("select avg(ratting) as rating from tablename where id='".$_REQUEST['id']."'");
$rat=$rowsRaing;
$RatingVal = $rat[0]->rating;
By using this variable you get the avg number of ratting
"$RatingVal"
It's var of object inside array, so:
$yourRatingValue = $rat[0]->rating;
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 6 years ago.
Improve this question
If array with 2 keys:
Array(
[0] => content
[1] => content
)
It is a task
if array 3 keys
Array(
[0] => content
[1] => content
[2] => content
)
It is other task
How count how many keys ([0], [1]..) there are in the array?
I already have all the structure of the code but just need to know how to know the amount of keys within the variable that contains the array
$len = count($myArray);
I think this is what you're asking?
As per your comment if you are using array_count_values() it will return the array that contains no of count of same values.
$yourArr = array("content","content");
print_r( array_count_values($yourArr) );
Result:
array("content"=>2);
From W3School:
Returns an associative array, where the keys are the original array's values, and the values are the number of occurrences
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Lets suppose that I have a string such as:
'the exact number of files is 223,345,129'
I am looking for a piece of code that within a sequential number like (223,345,129) wherever it found a delimiter(a comma like ",") it breaks the number into 3 elements of 223 345 129 and store these 3 segments inside an array?
Thanks for answers in advance
You may do it easily with the in-built PHP function explode:
$arr = explode(",", "223,345,129");
print_r($arr);
The output will be:
Array
(
[0] => 223
[1] => 345
[2] => 129
)
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 do a print_r I get this array:
Array (
[0] => Array (
[id] => 5280
)
)
How can I access the id value with PHP?
You can access it like this
$id_value = $your_array[0]['id'];
Try
$mapped = array_map("current",$array);
echo $mapped[0];
This is useful if you have multiple arrays on the root array
eg: Array(Array(),Array()..)