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 have an array of ids. Right now it has 125 ids in it. I only pasted a few. I would like to select a random id from it and use it in a query. I was using rand_array, but I had a simple array like this: array(1,2,3); with this new array I don't know how to grab a random id.
array(125) {
[0]=>
object(stdClass)#21 (1) {
["id"]=>
string(1) "5"
}
[1]=>
object(stdClass)#22 (1) {
["id"]=>
string(1) "6"
}
[2]=>
object(stdClass)#23 (1) {
["id"]=>
string(1) "7"
}
[3]=>
object(stdClass)#24 (1) {
["id"]=>
string(1) "8"
}
[4]=>
object(stdClass)#25 (1) {
["id"]=>
string(1) "9"
}
[5]=>
object(stdClass)#26 (1) {
["id"]=>
string(2) "10"
}
[6]=>
object(stdClass)#27 (1) {
["id"]=>
string(2) "11"
}
[7]=>
object(stdClass)#28 (1) {
["id"]=>
string(2) "12"
}
}
As #Jack commented under the question, this is probably the easiest way to do what you're trying to do:
echo $array[array_rand($array)]->id;
If you want the IDs for a different purpose, you could extract the IDs from the objects in the array and apply array_rand() on that array:
$idsArr = array(); // Initialize an empty array
foreach ($array as $obj) {
$idsArr[] = $obj->id; // Push each ID into the array
}
$random_id = $array[array_rand($idsArr)]; // Get a random ID from the array
Your new array is two dimensional - you need to specify both ID's to grab an element. So try:
$rand = rand(0,count($array));
$item=$my_array[$rand]['id'];
However I couldn't understand the structure of your array so if you print_r it I can refine this
cheers
Related
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 am creating an Array from a form via serializeArray() in jQuery:
var form = $(this).closest('form');
var formData = form.serializeArray();
If I output this with alert(formData.toSource()); I get the result:
[{name:"form[username]", value:"1"}, {name:"form[email]", value:"1#12.sw"}, {name:"form[is_active]", value:"1"}, {name:"form[plainPassword][first]", value:""}, {name:"form[plainPassword][second]", value:""}, {name:"form[id]", value:"9"}, {name:"form[_token]", value:"Mk"}]
If I capture the data via Ajax to php with $data = $request->request->get('data');I get the following Array as a result:
array(7) {
[0]=>
array(2) {
["name"]=>
string(14) "form[username]"
["value"]=>
string(1) "1"
}
[1]=>
array(2) {
["name"]=>
string(11) "form[email]"
["value"]=>
string(7) "1#12.sw"
}
[2]=>
array(2) {
["name"]=>
string(15) "form[is_active]"
["value"]=>
string(1) "1"
}
[3]=>
array(2) {
["name"]=>
string(26) "form[plainPassword][first]"
["value"]=>
string(0) ""
}
[4]=>
array(2) {
["name"]=>
string(27) "form[plainPassword][second]"
["value"]=>
string(0) ""
}
[5]=>
array(2) {
["name"]=>
string(8) "form[id]"
["value"]=>
string(1) "9"
}
[6]=>
array(2) {
["name"]=>
string(12) "form[_token]"
["value"]=>
string(43) "Mk"
}
}
The array that I would actually need is something like this:
array(2) {
["form[username]"]=>
string(14) "1"
["form[email]"]=>
string(1) "1#12.sw"
["form[is_active]"]=>
string(1) "1"
["form[plainPassword][first]"]=>
string(0) ""
["form[plainPassword][second]"]=>
string(0) ""
["form[id]"]=>
string(1) "9"
["form[id]"]=>
string(2) "Mk"
}
So is it possible to actually serialize the Array differently? What is the best way to achieve the array I would need?
foreach($data as $i) { $newData[$i['name']] = $i['value']; }
$newData is now like you wanted it to be.
As the comments suggest, there is no built-in way of doing so. You either have to loop through the array and build the object yourself, or more common, just use .serialize() and handle the parameter interpretation in php directly.
This question already has answers here:
Get cart item name, quantity all details woocommerce
(6 answers)
Closed 4 years ago.
I have The WC()->cart object that has this structure:
object(WC_Cart)#803 (13) {
["cart_contents"]=>
array(3) {
["e369853df766fa44e1ed0ff613f563bd"]=>
array(11) {
["key"]=>
string(32) "e369853df766fa44e1ed0ff613f563bd"
["product_id"]=>
int(34)
["variation_id"]=>
int(0)
["variation"]=>
array(0) {
}
["quantity"]=>
int(1)
["line_tax_data"]=>
array(2) {
["subtotal"]=>
array(1) {
[1]=>
float(31.5)
}
["total"]=>
array(1) {
[1]=>
float(31.5)
}
}
["line_subtotal"]=>
float(150)
["line_subtotal_tax"]=>
float(31.5)
["line_total"]=>
float(150)
["line_tax"]=>
float(31.5)
["data"]=>
object(WC_Product_Simple)#1251 (12) {
After WC()->cart->cart_contents I don't know how to get 'line_subtotal' value.
May I need to convert this object to something else before trying to get any value?
Try this.
foreach(WC()->cart->cart_contents as $cart_content) {
echo $cart_content['line_subtotal']
}
using foreach since the value of "cart_contents" is array.
This question already has answers here:
PHP foreach change original array values [duplicate]
(5 answers)
Closed 5 years ago.
I tried to add a key and its in a 2D array using a foreach. The problem is that this key isn't "saved". As soon as I try to look the first array, the key I added disappeared.
There is the code :
$Etapes=$this::getEtapes();
foreach($Etapes as $Etape){
$req = $this::getSuiviEtapes();
$Etape['Nom_Suivi'] = $req[0]['Nom_Suivi'];
if($Etape['ID_Etat_Etape']=="22")
{
var_dump($Etape);
var_dump($Etapes);
$this->Etapes=$Etapes;
var_dump($this->Etapes);
}
}
And there is the return
array(3) {
["ID_Etat_Etape"]=>
string(2) "22"
["Nom"]=>
string(36) "Comparatif"
["Nom_Suivi"]=>
string(8) "En_cours"
}
array(2) {
[0]=>
array(2) {
["ID_Etat_Etape"]=>
string(2) "21"
["Nom"]=>
string(12) "Etude"
}
[1]=>
array(2) {
["ID_Etat_Etape"]=>
string(2) "22"
["Nom"]=>
string(36) "Comparatif"
}
}
array(2) {
[0]=>
array(2) {
["ID_Etat_Etape"]=>
string(2) "21"
["Nom"]=>
string(12) "Etude"
}
[1]=>
array(2) {
["ID_Etat_Etape"]=>
string(2) "22"
["Nom"]=>
string(36) "Comparatif"
}
}
as you can see, the "Nom_Suivi" key do not appear in the second and third array.
I don't know if my issue is clear enough. Ask if it isn't.
Thank you for helping.
If you want to modify array you are iterating over with foreach, you either have to use reference - foreach($Etapes as &$Etape) or (prefered way) change the value by using the original array variable and key:
foreach($Etapes as $key => $Etape){
...
$Etapes[$key]['Nom_Suivi'] = $req[0]['Nom_Suivi'];
...
}
Try using
foreach ($fields as $key => $field)
Use the key
Check out this Duplicate of this question
I hope I can make this question clear enough.
I'm looking to put a list of arrays inside one master array, dynamically, so that it looks like this:
masterarray {
array1
{ [0]=>VAL1 [1]=>VAL2 }
array2
{ [0]=>VAL1 [1]=>VAL2 }
array3
{ [0]=>VAL1 [1]=>VAL2 }
}
I've tried, but I could only get it to look like this:
array(1) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
array(2) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [1]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
array(3) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [1]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [2]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
And that's definitely not what I'm aiming for. Nothing seems contained. I need the format specified above.
I'm using the explode function on a string pulled from a file to make this table of arrays (I think you call it that)
Here is the code I'm using that's not working.
$variabledebugging = file("FILE.TXT");//LOOK IN THIS FILE FOR THE NUMBER AND SET IT TO A VAR.
$i=0;
foreach($variabledebugging as $placeholder){
$variabledebuggingtbl[] = explode("\t",$variabledebugging[$i]);
var_dump($variabledebuggingtbl);
$i++;
}
I've tried a couple of different variations, but that's the one I'm using now.
To be clear, that file being pulled (each line as a value in an array) has 2 things written to each line, separated by a tab character, so that's the system I'm going on.
Thank you! I'm sure this is a simple task, I just can't think it through.
Oh and while I'm at is there a way to make debugging more readable?
You ARE getting the right result. The reason it seems wrong is that you are running var_dump inside the loop. And why don't you use the $placeholder variable?
$variabledebugging = file("FILE.TXT");
foreach($variabledebugging as $placeholder){
$variabledebuggingtbl[] = explode("\t", $placeholder);
}
var_dump($variabledebuggingtbl);
I'm not sure what you mean by "making debugging more readable", but if you want some linebreaks and indentation you should just look in the generated HTML code. var_dump do add spacing to make it readable but it is ignored by the web browser. If you don't want to read the HTML source, just add your var_dump to a <pre> element.
This question already has answers here:
Make var_dump look pretty [duplicate]
(16 answers)
Closed 1 year ago.
I've seen some online pretty print modules for code. Anyone know of one that will format a multi-dimensional array into readable display?
Example, translate this:
array(83) { [0]=> array(2) {
["name"]=> string(11) "CE2 Options"
["type"]=> string(5) "title" } [1]=>
array(1) { ["type"]=> string(4) "open"
} [2]=> array(5) { ["name"]=>
string(8) "Template" ["desc"]=>
string(638) "test description"
["id"]=> string(9) "my_theme"
["type"]=> string(14) "selectTemplate"
["options"]=> array(13) {
Into this...
array(83) {
[0]=> array(2) { ["name"]=> string(11) "My Options" ["type"]=> string(5) "title" }
[1]=> array(1) { ["type"]=> string(4) "open" }
[2]=> array(5) {
["name"]=> string(8) "Template"
["desc"]=> string(638) "Test description"
["id"]=> string(9) "my_theme"
["type"]=> string(14) "selectTemplate"
["options"]=> array(13) {
[0]=> string(10) "test"
If you are dumping it to HTML document use the
<pre></pre>
it does exactly that.
If you want a nicer output than var_dump , then check out the alternatives listed here:
A more pretty/informative Var_dump alternative in PHP?
Particularily http://krumo.sourceforge.net/ provides a much more accessible DHTML view for variable dumps. (It requires an extra include() though.)
And if you actually want to keep the generated output as static html, you might have to write a smallish wrapper script.
the pretty version is just what you get when you have XDebug installed and html_errors is set to On. Then you use var_dump($array). And make sure you set children and depth to what you need. there you go