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.
Related
I Have an array of Objects (result of mysql Query)
array(28) {
[0]=>
array(2) {
["member_id"]=>
string(5) "40105"
["last_login"]=>
string(19) "2014-02-18 06:04:06"
}
[1]=>
array(2) {
["member_id"]=>
string(5) "51758"
["last_login"]=>
string(19) "2014-04-21 09:29:11"
}
[2]=>
array(2) {
["member_id"]=>
string(5) "52682"
["last_login"]=>
string(19) "2014-04-21 08:27:00"
}
What is the best datatype to sort it in redis ? I need to search and add an object into this result?
shall I use SET or LIST ?
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
I have implemented a Single Sign on function using Janrain. It outputs this data (amongst other data). Is it possible to break this down and extract only the displayName for example so that I may place it in a variable?
auth_info:
array(3) {
["stat"]=>
string(2) "ok"
["profile"]=>
array(10) {
["providerName"]=>
string(8) "Facebook"
["identifier"]=>
string(48) "removed"
["preferredUsername"]=>
string(10) "OllieJones"
["displayName"]=>
string(11) "Ollie Jones"
["name"]=>
array(3) {
["formatted"]=>
string(11) "Ollie Jones"
["givenName"]=>
string(5) "Ollie"
["familyName"]=>
string(5) "Jones"
}
Here is the script that creates it and where I would like to define the variable https://github.com/janrain/Janrain-Sample-Code/blob/master/php/rpx-token-url.php
Many Thanks
Try $auth_info['profile']['displayName']
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
I have a SESSION that looks like this:
array(1) { [1]=> array(11) { ["aantal"]=> int(1) ["id"]=> string(2) "29" ["filmtitel"]=> string(16) "2_fast_2_furious" ["film_id"]=> string(1) "1" ["zaal_id"]=> string(1) "1" ["zaaltitel"]=> string(6) "zaal 1" ["tijdstip"]=> string(8) "17:30:00" ["stoeltjes"]=> array(3) { [0]=> string(2) "19" [1]=> string(2) "20" [2]=> string(2) "21" } ["dag"]=> string(8) "woensdag" ["verwijder"]=> int(1) ["vertoningId"]=> string(2) "31" } }
so there is an array and within that array is another array called "stoeltjes" with 3 items.
What i would like to know is how i can direct the content of "stoeltjes" to jquery so i can assign it to a flashvar and send it to as3.
Anyone who can help?
Take a look at json_encode if you have php 5.2 or higher. The resulting variable from that function can be set as a javascript variable.
I think it's something like this:
echo "
<script>
var stoeltjes = eval(".json_encode($_SESSION['stoeltjes']).");
</script>
";
Now 'stoeltjes' is a array with the same values but then in javascript.