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"];
}
}
}
Related
I have an array from an inner join statement from 2 Mysql tables... I need them in a better working format for front end display:
mysql query:
SELECT weddings.id, weddings.wImage, images.iLink FROM images INNER JOIN weddings ON weddings.id = images.weddingId
OUTCOME:
Array
(
[0] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => image2.jpg
)
[1] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => image3.jpg
)
[2] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => image4.jpg
)
[3] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => image5.jpg
)
[4] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => image6.jpg
)
[5] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => image7.jpg
)
[6] => Array
(
[id] => 11
[wImage] => image12.jpg
[iLink] => image8.jpg
)
[7] => Array
(
[id] => 11
[wImage] => image12.jpg
[iLink] => image9.jpg
)
[8] => Array
(
[id] => 11
[wImage] => image12.jpg
[iLink] => image10.jpg
)
)
So I need to work this array further to get this expected OUTCOME:
Array
(
[0] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => Array
(
[image] => image2.jpg
[image] => image3.jpg
[image] => image4.jpg
)
)
[1] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => Array
(
[image] => image5.jpg
[image] => image6.jpg
[image] => image7.jpg
)
)
[2] => Array
(
[id] => 10
[wImage] => image12.jpg
[iLink] => Array
(
[image] => image8.jpg
[image] => image9.jpg
[image] => image10.jpg
)
)
)
There are many ways, but assuming your id is unique (as I guess), you can use this id as an associative key in the output, so you can easily determine whether it exists already. In the last step you reindex the result array. See code comments for further explanation.
// $input_array is the outcome from sql
// $output_array will store the desired outcome
$output_array=[];
// loop through every item in input array
foreach ($input_array as $input_item) {
// if key with "id" already exists in output array
if (array_key_exists($input_item["id"], $output_array)) {
// append image to already existing "iLink" array
array_push($output_array[$input_item["id"]]["iLink"],
["image" => $input_item["iLink"]]
);
}
// if key with id not exists yet in output array
else {
// add current input item to output array,
// with "id" value as key
$output_array[$input_item["id"]] = [
"id" => $input_item["id"],
"wImage" => $input_item["wImage"],
// array with first image, can be appended later
"iLink" =>["image" => $input_item["iLink"]]
];
}
}
// now we have the result, but output array keys are not 0,1,2
// but the id values (3, 10 etc.), so we have to reindex
$output_array = array_values($output_array);
// now it is ready
var_dump($output_array);
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 $test array
Array
(
[0] => Array
(
[quantity] => 3
[stock_id] => _PHONE
)
[1] => Array
(
[quantity] => 3
[stock_id] => 102
)
[2] => Array
(
[quantity] => 4
[stock_id] => _PHONE
)
[3] => Array
(
[quantity] => 3
[stock_id] => 102
)
[4] => Array
(
[quantity] => 4
[stock_id] => _PHONE
)
[5] => Array
(
[quantity] => 6
[stock_id] => _PHONE
)
[6] => Array
(
[quantity] => 2
[stock_id] => 102
)
)
and to sum same stock_id keys to one, i use below functions:
function sum($array, $key){
isset($array[$key['stock_id']]) ? $array[$key['stock_id']]['quantity'] += $key['quantity'] : $array[$key['stock_id']] = $key;
return $array;
};
//merge same stock_id and sum the quantity same stock id
$sum_same_stock_id = array_reduce($test, "sum");
and the result went well like below:
$sum_same_stock_id:
Array
(
[_PHONE] => Array
(
[quantity] => 17
[stock_id] => _PHONE
)
[102] => Array
(
[quantity] => 8
[stock_id] => 102
)
)
So the question here is, I wanted to pass an dynamic keys values not just fixed values stock_id & quantity in sum function above. Have tried variety ways but still can't figured out the way. And can we put those functions into class as well?
Any advise is appreciated!
Maybe you can use the "use" function for the callback?
See https://www.php.net/manual/en/functions.anonymous.php
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;
}
}
I've an associative array called $data as follows:
Array
(
[op] => edit
[pt_id] => 4
[form_submitted] => yes
[pt_doc_title] => Array
(
[1] => Test Document
[2] => New Joining
[3] => Hallo Jolly
)
[pt_doc_id] => Array
(
[0] => 6
[1] => 7
)
[submit] => Update
)
In order to keep all the package type documents data together I've manipulated the above array as follows:
foreach ($data['pt_doc_title'] as $key => $title) {
$id = isset($data['pt_doc_id'][$key-1]) ? $data['pt_doc_id'][$key-1] : null;
$data['pt_documents_data'][] = array(
'pt_doc_title' => $title,
'pt_doc_id' => $id
);
}
unset($data['pt_doc_title'], $data['pt_doc_id']);
After manipulation I'm getting following array $data as follows:
Array
(
[op] => edit
[pt_id] => 4
[form_submitted] => yes
[submit] => Update
[pt_documents_data] => Array
(
[0] => Array
(
[pt_doc_title] => Test Document
[pt_doc_id] => 6
)
[1] => Array
(
[pt_doc_title] => New Joining
[pt_doc_id] => 7
)
[2] => Array
(
[pt_doc_title] => Hallo Jolly
[pt_doc_id] =>
)
)
)
My issue is I'm haivng another array called $_FILES as follows and I want to merge one of it's key(name) into above array in a same manner.
Array
(
[document_file_name_1] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[document_file_name_2] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[document_file_name_3] => Array
(
[name] => FAQ.doc
[type] => application/msword
[tmp_name] => /tmp/phpFiBYKB
[error] => 0
[size] => 35840
)
)
That is if there exists a value under [name] then the final array should be as follows. As there is a value present only in last array element of array $_FILES
Array
(
[op] => edit
[pt_id] => 4
[form_submitted] => yes
[submit] => Update
[pt_documents_data] => Array
(
[0] => Array
(
[pt_doc_title] => Test Document
[pt_doc_id] => 6
[pt_doc_file_iname] =>
)
[1] => Array
(
[pt_doc_title] => New Joining
[pt_doc_id] => 7
[pt_doc_file_iname] =>
)
[2] => Array
(
[pt_doc_title] => Hallo Jolly
[pt_doc_id] =>
[pt_doc_file_iname] => FAQ.doc
)
)
)
Can anyone please help me in creation of such final array?
Simplest way would be to walk the $_FILES array and extract the id from the last segment of the field name... then use that -1 as the basis for adding the file to your result array.
Something like this should get you there (untested):
foreach($_FILES as $k => $file){
$index_to_update = trim(substr($k, strrpos($k, "_")+1))-1;
$res["pt_document_data"][$index_to_update]["pt_doc_file_iname"] = isset($file["name"])?$file["name"]:"";
}
That is assuming $res is the array that is the parent of the pt_document_data element.