Extract data from php array - php

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

Related

PHP: Removing string count of entire array from serialized array

I have a JSON array that I'm saving in a PHP variable. I'm then serializing that array using serialize($variable) and saving it to the dB with the built in Wordpress function update_post_meta().
The problem I'm having is that the entire serialized array is wrapped with a string count. Like so, currently being saved as:
s:332:"a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}}";
I need to save it without the string count for the entire array. Desired output:
a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}};
Any help with this is, as always, greatly appreciated.
It seems that your array is serialized twice and that's what gives you the addendum ... I have taken your serialize data and un serialized it twice and it came back as you wanted :
<?php
$ser = 's:332:"a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}}";';
$arr = unserialize($ser);
echo '<pre>';
print_r($arr); /* Print after one unserialize */
echo '<pre>';
print_r(unserialize($arr)); /* Print with unserialize to the once unserialized*/
Will return:
a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}}
Array
(
[0] => Array
(
[type] => weeks
[cost] => 3
[modifier] =>
[base_cost] => 2
[base_modifier] =>
[from] => 1
[to] => 4
)
[1] => Array
(
[type] => persons
[cost] => 6
[modifier] =>
[base_cost] => 5
[base_modifier] =>
[from] => 1
[to] => 2
)
)
As you can see only after two unserialize it returns back to an array... so just do one and you have what you need.

How to access stdClass object of nested array in PHP [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
I have php array like
Array
(
[0] => stdClass Object
( [type] => MILESTONE
[creator] => xyz
[tpid] => abc
[docname] => STS
[items] => stdClass Object
(
[MILESTONE_CODE] => ARPOD
[MILESTONE_TYPE] => ACT
[MILESTONE_DESCRIPTION] => Arrival at Port of Discharge
[TRIGGER_EVENT] => Y [ENVIRONMENT] => T
)
)
[1] => stdClass Object
(
[type] => MILESTONE
[creator] => xyz
[tpid] => abc
[docname] => STS
[items] => stdClass Object
(
[MILESTONE_CODE] => BKD
[MILESTONE_TYPE] => EST
[MILESTONE_DESCRIPTION] => Booking created
[TRIGGER_EVENT] => N
[ENVIRONMENT] => P
)
)
)
There are 2 php files written
File 1: In this file I am decoding my json data and got above array
$data = json_decode(file_get_contents("php://input"));
// print_r($data);exit;
foreach ($data as $value){
$TP->createTPConfig($data); // Here I am calling method for each index of array
}
File 2: In this file I have written method to insert data into oracle database.
function createTPConfig($dataToBeInsert){
// print_r($dataToBeInsert);exit; // till here I am getting above array
// written insert query here
}
Now in File 2, I want to access array values of
[type], [creator], [tpid], [docname]
and again
I want to foreach(repeat) loop for [items]
and want to access keys and values of
[MILESTONE_CODE], [MILESTONE_TYPE], [MILESTONE_DESCRIPTION] and [TRIGGER_EVENT]
and insert both keys and values into database
Can anyone help ... your help would be appreciated!
Just change
$data = json_decode(file_get_contents("php://input"));
to
$data = json_decode(file_get_contents("php://input"), true);
This will give you a full-fledged array. Obligatory man link: json decode

Cannot save data in more than one array in Laravel

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

PHP Find value in object

I have a result of an SQL query that looks like this
Array
(
[0] => stdClass Object
(
[field_number] => 1
[value] => Joe
)
[1] => stdClass Object
(
[field_number] => 2
[value] => Bloggs
)
[2] => stdClass Object
(
[field_number] => 3
[value] => 12566
)
[3] => stdClass Object
(
[field_number] => 4
[value] => 2000-07-24
)
)
It wont always return all the fields as some are not required therefore not saved to the database.
I know that first name is stored with field number 1. How can I look this up in the object.
EG
$first_name = $result => field_number == 1
I know thats not right, but Im sure there must be a simple way to get this info?
Thanks
If the values in the array are not in order (eg array[0] does not always contain field_number 1) then you will need to iterate the array:
foreach($array as $item){
if($item->field_number==1){
$first_name = $item->value;
break;
}
}
However, if this is the result of an SQL query, probably you need to rewrite the query to give you data in a more useable form

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