i've got 3 inputs, 2 of which i want in the same array (name="notes['a']" and name="notes['b']") but when i use jquerys' serializeArray() it puts them all at the same 'level'. But i want to use php to serialise the posted notes array into the database using the method thats been working for inputs that aren't part of an array (ie name="basic"
foreach($_POST['data'] as $var => $value) {
if(!strstr(strtolower($value['name']),'added')) $q1 .= "".$value['name']."='".$value['value']."',";
}
echo $q = "UPDATE ".$_POST['table']." SET ".rtrim($q1,',')." WHERE ".$_POST['idField']."=".$_POST['id']." ";
*the array $_POST['data'] is getting posted as;*
[data] => Array
(
[0] => Array
(
[name] => notes[\'a\']
[value] => dan#jynk.net
)
[1] => Array
(
[name] => notes[\'b\']
[value] => Daniel Crabbe
)
[2] => Array
(
[name] => test
[value] => Daniel Crabbe
)
)
but i guess it should be along these lines?
[data] => Array
(
[0] => Array{
Array(
[name] => notes[\'a\']
[value] => dan#jynk.net
)
Array
(
[name] => notes[\'b\']
[value] => Daniel Crabbe
)
)
[1] => Array
(
[name] => test
[value] => Daniel Crabbe
)
)
how can i get jquery to respect the arrays in the input array? any help welcome...
UPdates
no got it like this but want everything on level [0] for easy access...
Array
(
[0] => Array
(
[name] => cm_email_to
[value] => dan#jynk.net
)
[1] => Array
(
[name] => cm_name_to
[value] => Daniel Crabbe
)
[2] => Array
(
[name] => cm_moveToList
[value] =>
)
)
*inputs*
<input id="cm_email_to" name="cm_email_to" value="dan#jynk.net" class="toPostCM" />
<input id="cm_name_to" name="cm_name_to" value="Daniel Crabbe" class="toPostCM" />
*jquery*
var dataCMSettings = $(".toPostCM").serializeArray();
Try taking the array indicies out of the input name:
<input name="notes[]" value="val1" />
<input name="notes[]" value="val2" />
Remove the quotes from your array indices in your HTML, so array['a'] becomes array[a].
ok - this does exactly what i need...
var params = {};
$('.toPostCM').each(function(index,value) {
params[value.name] = value.value;
});
console.log(params);
*gives me*
Array
(
[cm_email_to] => dan#jynk.net
[cm_name_to] => Daniel Crabbe
[cm_moveToList] =>
)
thanks all...
Related
I want to determine what is the type of filed in my array from below code.
foreach($question->questionsOptions as $option){
$servey_detail_arr['option'][$option->question_id][] = array('value'=>$option->label,'id'=>$option->option_id,'option'=>$option->label);
}
The above code create below array. Now i want to insert type of filed in my array. i have written in below array in comment where i want the value.
Array
(
[option] => Array
(
[96] => Array
(
//i want add here a value means type radio
[0] => Array
(
[value] => karachi
[id] => 49
[option] => karachi
)
[1] => Array
(
[value] => islamabad
[id] => 50
[option] => islamabad
)
)
[97] => Array
(
**i want add here a value means type datepicker**
[0] => Array
(
[value] => date
[id] => 53
[option] => date
)
)
[100] => Array
(
//i want add here a value means type checbox
[0] => Array
(
[value] => checkbox1
[id] => 55
[option] => checkbox1
)
[1] => Array
(
[value] => checkbox2
[id] => 56
[option] => checkbox2
)
)
)
)
Any other solution will be appreciate. Thanks
Try adding the below condition inside foreach loop
if (array_key_exists('96', $servey_detail_arr['option'][$option->question_id])) {
$servey_detail_arr['option'][$option->question_id][] = array('type'=>'radio');
}
if (array_key_exists('97', $servey_detail_arr['option'][$option->question_id])) {
$servey_detail_arr['option'][$option->question_id][] = array('type'=>'datepicker');
}
if (array_key_exists('98', $servey_detail_arr['option'][$option->question_id])) {
$servey_detail_arr['option'][$option->question_id][] = array('type'=>'checkbox');
}
i have below array,and i have amenities id = 50,i need to show amenities name like 'Express check-out' using amenities id = 50 from this array using php.
Array
(
[amenities] => Array
(
[0] => Array
(
[id] => 0
[name] => Cash machine
[key] => CASHMACHINE
)
[1] => Array
(
[id] => 42
[name] => Express check-in
[key] => EXPRESSCHECKINSERVICE
)
[2] => Array
(
[id] => 50
[name] => Express check-out
[key] => EXPRESSCHECKOUTSERVICE
)
[5] => Array
(
[id] => 3
[name] => Wi-Fi
[key] => WIFISERVICE
)
)
)
There are many ways that your problem can be solved. Easy way can be as follow
function getAmenities($array,$id){
foreach($array['amenities'] as $tmp_arr)
if($tmp_arr['id']==$id)
return $tmp_arr['name'];
}
echo getAmenities($array,50);
I have not checked result but should work fine. Please let me know if this works for you
How do u create this Array?
Can't u just use the id as the key when u create it, like:
$key = $array2['id'];
$array['amenities'][$key] = $array2;
Here's my issue:
I have an object filled with arrays that look like this.
[376339] => Array
(
[0] => 1f422730-f54b-4e4d-9289-10258ce74446
[1] => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
I need to search another object to find objects with the matching IDs, like below. Is there a way of doing this without a foreach? There are SEVERAL and I would like to not have to loop over the entire object every time.
stdClass Object
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
stdClass Object
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
I need it to end up looking like this.
[376339] => Array
(
[0] => Array
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
[1] => Array
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
)
I'm not sure if this makes any sense, that's why I had my two inital outputs I need to have merged into one somehow. This is all coming from one huge json object and I'm just using json_decode($jsonStuff) to decode it.
Would this be easier if I added true in the decode function? If I could just search for it like I could in python, that would be neat. But as it is, I'm at a loss as to how to get the output I need.
Note: Input json CANNOT be changed, I have no affiliation with the people that created it.
First loop over your input array and create an array with the key as the id
$input = json_decode($json_input);
$output = array();
foreach($input as $obj){
$output[$obj->id] = $obj;
}
then you can build your other array by searching the id on the array key
$massive_search_array = array(376339 => array
(
0 => 1f422730-f54b-4e4d-9289-10258ce74446,
1 => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
);
$final_output = array();
foreach($massive_search_array as $index => $searches){
foreach($searches as $search){
if(isset($output[$search])){
$final_output[$index][] = $output[$search];
}
}
}
I'm trying to echo an array value from an array that was created using the following code.
The data for the array is pulled from a MySQL table.
$names = $db->fetchAll("select `name` from `classes`");
This is what is stored in the $names variable
Array ( [0] => Array ( [name] => Web Design ) [1] => Array ( [name] =>
Art History ) [2] => Array ( [name] => Gym ) [3] => Array ( [name] =>
English ) [4] => Array ( [name] => Biology ) [5] => Array ( [name] =>
3D Animation ) [6] => Array ( [name] => Tech Disc ) [7] => Array (
[name] => Math ) [8] => Array ( [name] => Dance ) [9] => Array (
[name] => Video Production ) [10] => Array ( [name] => Home Ec ) [11]
=> Array ( [name] => Government ) [12] => Array ( [name] => Physics ) )
I'm attempting to echo a [name] value OR all [name] values, but I can't figure it out. I've tried the following....
<?php echo $names['name'];?>
returns nothing
<?php echo $names['0'];?> //AND\\ <?php echo $names[0];?>
Both return the string Array
Can someone please help me echo a single value from the array?
Example: Web Design or Art History
Also can someone please help me echo all the values from the array?
Example: Web Design Art History Gym English Biology ......
You should try <?php echo $names[0]['name'];?>, <?php echo $names[1]['name'];?>, etc....
This is because your query function returns an array with all results in it.
In that array, each row returned is an array again. And each field is a key in that array.
Echoing a single value:
echo $names[0]['name'];
Echoing all values:
foreach ($names as $name) {
echo $name['name'].' ';
}
I dynamically create this associative file array through a PHP script, and now I am wondering how do I select all values inside the root array?
I'll explain here in code.
This is the array that is currently getting generated everytime:
Array
(
[HTML] => Array
(
[0] => Index.php
)
[Javascript] => Array
(
[0] => Javascript.js
[1] => Jquery.js
)
[0] => New Text Document.txt
[Scripts] => Array
(
[0] => Get_Server_files.Script.php
)
[Style] => Array
(
[0] => General.css
[1] => Menu.css
[2] => Style.css
)
[images] => Array
(
[WelcomeImages] => Array
(
[0] => WelcomeImage0.png
[1] => WelcomeImage1.png
[2] => WelcomeImage2.png
[3] => WelcomeImage3.png
[4] => WelcomeImage4.png
[5] => WelcomeImage5.png
[6] => WelcomeImage6.png
)
[0] => bg.jpg
)
)
From here, how do I select every first value? As in selecting
HTML
Javascript
Scripts
Style
Images.
And not anything that is inside these arrays.
I have searched but everytime it's not quite what I would like.
You can use array_keys() to get the names and then use array_filter() to remove the 0 values:
$names = array_filter(array_keys($data));
print_r($names);
Output:
Array
(
[0] => HTML
[1] => Javascript
[3] => Scripts
[4] => Style
[5] => images
)
Demo.
Use array_keys()
$keys = array_keys($array);