I'm trying to figure out how to properly structure request data into an array in my controller. I have 3 values coming from my http request and I'm trying to get two of them into a specific array structure. My request values are dumping so they are definitely there.
Here's my controller where I"m putting them into an array:
$subItems = array("title" => $request->itemTitle, "description" => $request->itemDesc);
But it's failing because the function I'm calling with the array $subItems is expecting an array structure where it can get the paramaters by the index of title and description like this:
[ $subtask['title'], $subtask['description']]
How do I need to change my controller/array code to accomplish this?
You can do it like below
$subItems = [];
$subItems['title'] = $request->itemTitle;
$subItems['description'] = $request->itemDesc;
You can use like this,
$subItems['title'] = $request->itemTitle;
After you cant get values using its key.
In your question you add values to $subItems but you getting values from $subtask Please check that one also.
Related
I have one constant file which contains all the status details ,i want to fetch the values from the constant file and store it inside an array in the form of indexed array, can you give me some idea how to do this one..
BooksConstants.php
class BooksConstants{
const PAID = 'settled';
const BOOK_FAILED_STATUSES = [
self::cancelled_by_customer,
self::FAILED,
self::FAILED_BY_GATEWAY,
self::INVALID_OTP
];
const BOOK_SUCCESS_STATUSES = [
self::PAID,
self::SUCCESS,
self::ON_THE_WAY,
self::PROGRESS
];
}
Controller.php
$array=[];
array_push($array,BooksConstants::BOOK_SUCCESS_STATUSES);
array_push($array,BooksConstants::BOOK_FAILED_STATUSES);
it's storing 0th index with all data for BOOK_SUCCESS_STATUES array and 1st index is storing for BOOK_FAILED_STATUES but my requirement is
$array=['failed','settled','failed by gateway'....);
Using array_push you will actually push an array to the 0th-index and after that push another array to the 1st-index.
As stated in the comment, array_merge() can be used here instead, as it will just place the content of the arrays besides each other in the returned array.
$array = array_merge(BooksConstants::BOOK_SUCCESS_STATUSES, BooksConstants::BOOK_FAILED_STATUSES);
I want to use laravels FormRequest to validate before updating some fields. This works fine if i just use:
User::find($application->userid)->fill($request->only('first_name'...
but the request also contains sub array ($request->programmeData).
array:2 [▼
"programme_id" => 5
"programme_title" => "some programme title"
]
if i try access that the same way i get 'Call to a member function only() on array':
Course::find($application->userid)->fill($request->programmeData->only('programme_id...
I've tried a handful of things, but not sure best way to go with this?
Update
I'm now using a foreach loop to save two items in the array. the example below saves the second value for both user_ids. any reason this isn't saving the first value for the first user_id?
foreach ($request->programmeData['userProgrammes'] as $key=>$userProgrammes) {
Course::where('application_id', $application->id)->get()[$key]->fill(Arr::only($request->programmeData['userProgrammes'][$key], ['programme_id']))->save();
}
but nothing updates. Any ideas on this one?
You can use Array::only() helper for this:
foreach ($request->programmeData['userProgrammes'] as $key=>$userProgrammes) {
Course::where('application_id', $application->id)->first()->fill([
$key => Arr::only($request->programmeData['userProgrammes'][$key], ['programme_id'])
])->save();
// or
$course = Course::where('application_id', $application->id)->first()
$course->$key = Arr::only($request->programmeData['userProgrammes'][$key], ['programme_id']);
$course->save();
}
//Arr::only($request->programmeData, ['programme_id', ...]);
I'm trying how to figure something out for my website.
This is not my code just an example to make it easier to understand what i mean.
Lets say i have an array like this:
$services = array(
Marketing => marketing:342343423423,
Sales => sales:779876786,
)
And i have a form on my website. I can get the posted values with a POST request.
The POST request can for example look like this
$_POST['service_request']
Now what i want to know is how to do the next:
if $_POST['service_request'] matches one of the array keys inside $services then print the relevant value of this array key.
So lets say an user fills my form, and his service request is marketing then i want to check if this service request exists inside the $service variable and if it exist print the value.
use key_exists function of php.
if(key_exists($_POST['service_request'],$services)){
//exists, perform rest of the logic here.
}
Update: it's an alias of array_key_exists so both are basically same.
Edit:
Code below can give an exception if the key isn't set. You should use the key_exists, given in the other answer. Or change it to:
if ( isset($services[$_POST['service_request']]) ) {
echo $services[$_POST['service_request']];
}
This code will print the value from the array with the given key:
$value = $services[$_POST['service_request']];
echo (isset($value) ? $value : '');
On my models I try to write a php model that will get me a associative array from a database. But I don't quite know how to approach this.
So after I execute this SQL query:
SELECT balance_events.weight,balance_events.added_date,
balance_entries.mid FROM balance_events, balance_entries
WHERE balance_entries.added_date BETWEEN '2016-08-02' AND '2016-08-03'
AND balance_entries.ptid =12
AND balance_entries.beid = balance_events.id
I will get this table:
And from that table I want to extract a asociative array that it will look like this:
count = ['13'=>1, '6'=>4, '16'=>3, '4'=>3]
where 'mid'=>number of how many times that mid can be found in the table.
ex. mid '13'=>1 cause you can found it only once.
I think that I will have to use SQL COUNT function, but how I can aggregate all of this in a PHP model in codeigniter? I know how to configure controller and view, but I don't know how to actually do the actual php model that will get me the desired array.
Try this query may help you ,
$result = $this->db->select('balance_events.weight,balance_events.added_date,COUNT(balance_entries.mid) as mid_count')
->from('balance_events, balance_entries')
->where('balance_entries.added_date BETWEEN "2016-08-02" AND "2016-08-03" ')
->where('balance_entries.ptid','12')
->where('balance_entries.beid','balance_events.id')
->group_by('balance_entries.mid')
->get();
return $result->result_array();
I'm not sure how you would create this in SQL but since you tagged php, I wrote a function that would do just this.
<?php
$query = array(array("mid"=>13), array("mid"=>2), array("mid"=>13), array("mid" =>6), array("mid" => 13), array("mid" => 6));
function createMidArray($queryResult){
$returnArray = array();
foreach ($queryResult as $qr){
$returnArray[$qr['mid']]++;
}
return $returnArray;
}
print_r(createMidArray($query));
?>
The output of this was Array ( [13] => 3 [2] => 1 [6] => 2 ) which matches up to my inputted $query (which is a 2D array). I'm expecting the output of your query is stored in a similar array, but with more data and keys
I am using Doctrine in my PHP app to return a result set using the following code
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$query = $dm->createQueryBuilder('SomeBundle:Listing')
->select('title')
->field('userId')->equals(1);
$listings = $query->getQuery()->execute();
$listings_array = $listings->toArray(); <--- WHY NOT RETURNING AN ARRAY?????
$data = array('success'=>true,'listings' => $listings_array, 'displaymessage' => $classifieds->count(). " Listings Found");
What gets out out is the following:
{"success":true,"listings":{"50831582253b4acf09000000":{"id":"50831582253b4acf09000000","title":"fddfds","assets":[],"discussions":[]}},"displaymessage":"1 Listings Found"}
I am wanting an array and not a dictionary.
Any help?
I havent messed with the ODM much but i suspect Doctrine always uses the key for the record as the key in the array when calling toArray on a collection, it makes it easier for most of the cases when you would want to do this, especially since there is no distinction in php between a dict/hash and an array.
Call array_values on it if you want a numerically indexed array.
$data = array(
'success'=>true,
'listings' => array_values($listings_array),
'displaymessage' => $classifieds->count(). " Listings Found"
);