Cannot save data in more than one array in Laravel - php

I am trying to save data in Laravel which has multiple arrays.
The array looks like this:
Array
(
[0] => Array
(
[client_personnel_name] => Ron
[client_id] => 52
[client_personnel_email] => abc#gmail.com
)
[1] => Array
(
[client_personnel_name] => John
[client_id] => 52
[client_personnel_email] => abc#gmail.com
)
)
When I save this data:
$personnel = ClientsPersonnel::create($client_personnel);
$personnel->save();
On debugging what data is being created to insert.
This is what I get in the attributes where the sent data is stored
[attributes:protected] => Array
(
[updated_at] => 2015-04-23 06:53:05
[created_at] => 2015-04-23 06:53:05
[id] => 2
)
How can I save the data which has multiple arrays?

You can use DB::insert(), like this:
DB::table('client_personnel')->insert(array($client_personnel));
As an alternative, you could do this using loop like.
foreach ($personnels as $personnelAttributes) {
$personnel = new ClientsPersonnel($personnelAttributes);
$personnel->save();
}
Regards,

Laravel Eloquent does not have bulk update nor insert function. You need to create new ClientsPersonnel for each subarray and save it individually.

Just loop the arrays and insert seperately:
foreach ($client_personnel as $client) {
ClientsPersonnel::create($client);
}

Related

Extract data from php array

I have the following array in my Moodle plugin:
Array (
[0] => stdClass Object (
[id] => 32
[sessionid] => 5
[sessiontimezone] => CET
[timestart] => 1464071400
[timefinish] => 1464102000 )
[1] => stdClass Object (
[id] => 33
[sessionid] => 5
[sessiontimezone] => CET
[timestart] => 1465281000
[timefinish] => 1465311600 )
)
How to get the data. Right now, when I make:
$pluginsessiondates = $DB->get_record('plugin_sessions', array('id'=>$sessionid));
I get only data from the frist array [0]
How to get the data from every array key and then the single values? Thanks in advance.
The Moodle DB functions are for getting data out of the database, rather than from an array somewhere inside your plugin.
If you have an array somewhere, then you can get fields from it by writing:
echo $myarray[0]->id;
echo $myarray[1]->id;
etc.
If you are not trying to get data out of an existing array and want, instead, to get it out of the database, then $DB->get_record() will, as its name implies, get you only a single record, whereas $DB->get_records() will get you all the matching records:
$sessions = $DB->get_records('plugin_sessions', array('sessionid' => $sessionid));
foreach ($sessions as $session) {
echo $session->id;
}

Can you retrieve nested array by name in php

I have an array like this:
(
[data] => Array
(
[account_id] => 1
[description] => my asset
[value] => Estimate
[value_amount] => 85000
[type] => Vehicle
[owner] => Array
(
[app_id] => 123
[percent] => 100
)
)
)
Clearly I can loop through the array and pull out the nested owner array that way, but is there something similar to array_column that will get the entire owner nested array without having to loop ?
Use the indexes, no function necessary.
$owner = $array['data']['owner']
or..
$percent = $array['data']['owner']['percent']
In php it's call associative array which mean index will not numeric it can be anythink.
So you can retrieve data like
<?php echo $array['data']['owner']['app_id']; ?>

Saving Array of Array in Mysql using Active Record Code Igniter

I am trying to save a Form Data in Mysql. But nested array is causing problem.
Array ( [name] => Custom Project
[number] => 08883
[key] => Array ( [0] => Server Cost
[1] => Domain Cost
[2] => Design Charges,
)
[value] => Array ( [0] => 098
[1] => 765
[2] => 787
)
)
I am using $this->db->insert('invoice',$array) and it is inserting data fine without the nested array.
So is there a way I can separate this nested array from above array and then save this nested array into another table having only two columns key and value
You could just use array_combine to create the key pair value:
function save_value($array)
{
$key_value = array_combine($array['key'], $array['value']);
$this->db->insert('table_name', $key_value);
}
I used the very simple approach, I pushed one array to another because the result was to be processed in Codeigniter insert method.
//Item description
$description=$_POST['key'];
//item amount
$amount=$_POST['value'];
$big_array=array();
for($i=0; $i<sizeof($description); $i++){
$small_array=array('description'=>$description[$i],'amount'=>$amount[$i]);
array_push($big_array,$small_array);
}
//adding data into model
$this->my_Model->InsertData($big_array);

php Post arrays in Session array

Im trying to put multiple posts into one session. Heres the line of code that i use to do it.
$_SESSION['answers'][] = array_push($_SESSION['answers'], $_POST);
As i do so, the following array comes out once i try to add the second array to the session:
Array
(
[0] => Array
(
[checkbox] => Optie 2
[category] => Dieren en natuur
[subcategory] => Wilde dieren
[vraagid] => 116
[type] => 3afbeeldingen
[media] => image
[submit] =>
)
[1] => Array
(
[checkbox] => Optie 1
[category] => Dieren en natuur
[subcategory] => Wilde dieren
[vraagid] => 117
[type] => 3afbeeldingen
[media] => image
[submit] =>
)
[2] => 2
)
I am talking about the last array,
[2] => 2.
This is automattically added. Now when i try to get another array in to the session, it gives me the same issue, it adds the correct array, with checkbox and other vars, but also makes a
[4] => 4
Is this an issue with the array_push function? Because the array that i push is correct, i already checked that.
Either add the value to your array:
$_SESSION['answers'][] = $_POST;
or use array_push
array_push($_SESSION['answers'], $_POST);
You try to do to much at once :)
Won't it work, when you just do:
array_push($_SESSION['answers'], $_POST);
or
$_SESSION['answers'][] = $_POST;
array_push() is basically the same as $array[] = .... array_push() returns the new number of elements in the array, so basically you are adding the new element to your array and then you are adding the amount of elements in the array to the array again (hence the 2).

PHP create array from database using size of another array as counter for for loop

I have the following code. The first part retrieves a multidimensional array from the database.
$model = $product->getModelsBikes();
echo "<pre>".print_r($model, true)."</pre>";
Its structure is like this.
Array
(
[0] => Array
(
[manufacture] => data
[name] => data
[code] => data
[cc] => data
[bike_type] => data
[title] => data
[about] => data
)
[1] =>; Array
(
[manufacture] => data
[name] => data
[code] => data
[cc] => data
[bike_type] => data
[title] => data
[about] => data
)
....
Second part
$count = 0;
for ($i = 0; $i < count($model); $i++) {
$modelSpecs = $product->getModelSpecs($model[$i]['code']);
$count++;
}
With this code i am trying to create another array of content based on the product code. however its just overriding the data in the array with each pass until only the last set is stored in the array because its a for loop.
This has got my stumped and is probably obvious but how would i iterate through and create another array exactly like the first but with the new data.
Just do this. This will make $modelSpecs an array and add the getModelSpecs return to the end of the array each iteration
$modelSpecs[] = $product->getModelSpecs($model[$i]['code']);
you should also probably define the array before the loop like this
$modelSpecs=array();

Categories