i want to add value on specific point in array php - php

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');
}

Related

how to get some of values from multidimensional array

Using the array below, I want to get just some value of an element containing a certain value for option_name. For example, I want to get the element with option_name => custom_radio_gender then the result will show the value of option_value for that element (i.e. Radio 1).
Array
(
[0] => Array
(
[ID] => 15
[user_id] => 3
[option_name] => custom_monStart
[option_value] => a:3:{i:0;s:8:"05:30 am";i:1;s:8:"07:30 am";i:2;s:8:"09:30 am";}
[autoload] => yes
)
[1] => Array
(
[ID] => 13
[user_id] => 3
[option_name] => custom_radio_gender
[option_value] => Radio 1
[autoload] => yes
)
[2] => Array
(
[ID] => 14
[user_id] => 3
[option_name] => custom_time2
[option_value] =>
[autoload] => yes
)
)
It's a basic PHP question,which has nothing relationship with javascript.What you need is just a function like:
function getValue($array, $key){
foreach($array as $content){
if(array_key_exists("option_name", $content) && $content["option_name"] == $key){
return $content["option_value"];
}
}
}

Array parsing using array value

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;

php delete specific value from array

I have an array $products that looks like this
Array
(
[services] => Array
(
[0] => Array
(
[id] => 1
[icon] => bus.png
[name] => Web Development
[cost] => 500
)
[1] => Array
(
[id] => 4
[icon] => icon.png
[name] => Icon design
[cost] => 300
)
)
)
I am trying to delete the part of array that matches [id] => 1 and for this I am using the following code
$key = array_search('1', $products);
unset($products['services'][$key]);
However it is not working and I am not getting any error either.
What am i doing wrong?
This should work for you:
$key = array_search('1', $products["services"]);
//^^^^^^^^^^^^ See here i search in this array
unset($products['services'][$key]);
print_r($products);
Output:
Array ( [services] => Array ( [1] => Array ( [id] => 4 [icon] => icon.png [name] => Icon design [cost] => 300 ) ) )
And if you want to reindex the array, so that it starts again with 0 you can do this:
$products["services"] = array_values($products["services"]);
Then you get the output:
Array ( [services] => Array ( [0] => Array ( [id] => 4 [icon] => icon.png [name] => Icon design [cost] => 300 ) ) )
//^^^ See here starts again with 0
This will loop through $products['services'] and delete the array whose 'id' key has value 1. array_values just re-indexes the array from 0 again.
foreach($products['services'] as $key => $service)
{
if($product['id'] == 1)
{
unset($products['services'][$key]);
array_values($products['services']);
break;
}
}

Merge array by value of a value cell (no doubles)

I was wondering if it's possible to merge an array by a value of a cell (no doubles) right of the php box.
To clarify this is my current array :
Array
(
[0] => Array
(
[Score] => 90
[Name] => David,
)
[1] => Array
(
[Score] => 90
[Name] => Eric,
)
[2] => Array
(
[Score] => 77
[Name] => Eva,
)
[3] => Array
(
[Score] => 77
[Name] => Gila,
)
)
So we have:
2 persons (David and Eric) that have the same score (90)
2 persons (Eva and Gila) that have the same score (77)
So by the end of the day (in that case) I want to get only 2 rows (merged by score) and the names will be added to the same line that merged by score.
This is what I'm expecting to get :
Array
(
[0] => Array
(
[Score] => 90
[Name] => David,Eric,
)
[1] => Array
(
[Score] => 77
[Name] => Eva,Gila,
)
)
Here is my php code :
<?php
header("Content-Type:text/plain");
$myArray=array();
$myArray[]=array('Score'=>'90','Name'=>'David,');
$myArray[]=array('Score'=>'90','Name'=>'Eric,');
$myArray[]=array('Score'=>'77','Name'=>'Eva,');
$myArray[]=array('Score'=>'77','Name'=>'Gila,');
print_r($myArray);
Thanks!

serialise array will inputs of same name

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...

Categories