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
Related
This question already has answers here:
how get each single column data from php multidimensional array?
(2 answers)
Closed 2 years ago.
I have the following data structure:
$campaigns =
Array
(
[0] => Array
(
[subject] => cca-cpg
)
[1] => Array
(
[subject] => cleanup-cpg
)
[2] => Array
(
[subject] => gas-cpg
)
[3] => Array
(
[subject] => pollinators-cpg
)
)
what I would like to end up with is:
$campaigns = ['cca-cpg','clean_up-cpg','gas-cpg','pollinators-cpg'];
this will work:
$newCampaigns=[];
for($i=0;$i<count($campaigns);$i++){
array_push($newCampaigns,$campaigns[$i]['subject'];
}
but I was wondering if there's a better way to do this. The data is coming directly from a mysql database
There's array_column() function for you:
$newCampaigns = array_column($campaigns, 'subject');
Source: https://www.php.net/manual/en/function.array-column.php
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
I have an array variable $array like this
$array = Array
(
[results] => stdClass Object
(
[successc] => stdClass Object
(
[926] => stdClass Object
(
[transaction_id] => xx
[transaction_code] => xx
[status] => xx
[amount] => 5
)
)
[success] => Array
(
[0] => Successful transaction
)
)
)
I want to access the transaction_id element. The 926 is a variable value. It could very well be 927 or 928. It comes from another object $cc. Would it be correct to access the transaction_id using the following code?
$x = $cc->id;
$transaction_id = $array['results']->successc->{$x}->transaction_id;
Your approach is not bad, but the code structure looks more like an array.
To convert to a full array, you can encode to json en decode to array.
$array = json_decode(json_encode($array), true);
When TRUE, returned objects will be converted into associative arrays.
So you can access each level of $array as array element.
This question already has answers here:
php stdClass to array
(11 answers)
Closed 6 years ago.
stdClass Object
(
[CountyId] => 3
[Name] => Alba
[Abbreviation] => AB
)
stdClass Object
(
[CountyId] => 4
[Name] => Arad
[Abbreviation] => AR
)
stdClass Object
(
[CountyId] => 5
[Name] => Arges
[Abbreviation] => AG
)
I want to convert this collection of stdClass Object into an array that contains only the CountyId, such as
[CountyId[0] => 3, CountyId[1] => 4, CountyId[2] => 5,...].
Anyone can help me ?
Try this:
$array = (array)$stdClassObject; // type casting
Update:
// convert your object into array using type casting
$array = (array) $stdClassObject;
// use array_column for specific index
$CountyIdArr = array_column($array, 'CountyId');
// note that, you can not use same index name for all values, you need to use as
$CountyIdArr['CountryID'] = $CountyIdArr;
echo "<pre>";
print_r($CountyIdArr);
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;
}
I have a SOAP Response from a Web Service and have extracted the XML data as a SimpleXMLElement. I have then iterated through this object to extract the various fields I need and save them into an array, which becomes an array of SimpleXMLElement objects.
I am now trying to export this data into a MySQL Database which, according to my research, means turning the array into a String and then using mysql_query("INSERT INTO (whatever) VALUES (whatever)");. I have tried implode and serialize but neither work and I get the error:
Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed'
This is what the array I have created from the SimpleXMLELement looks like:
Array
(
[0] => Array
(
[uid] => SimpleXMLElement Object
(
[0] => WOS:000238186400009
)
[journal] => SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => source
)
)
[publication] => SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => item
)
[0] => Abundance of hedgehogs (Erinaceus europaeus) in relation to the density and distribution of badgers (Meles meles)
)
[year] => 2006
[author1] => SimpleXMLElement Object
(
[0] => Young, RP
)
[address] => SimpleXMLElement Object
(
[0] => Cent Sci Lab, Sand Hutton, Yorks, England
)
[author2] => SimpleXMLElement Object
(
[0] => Davison, J
)
[author3] => SimpleXMLElement Object
(
[0] => Trewby, ID
)
[citations] => SimpleXMLElement Object
(
[#attributes] => Array
(
[local_count] => 15
[coll_id] => WOS
)
)
) ... etc ...
)
Can anyone help me with the method to get this data into my database, please? Do I need to change it into (yet) another format?
You have to iterate through your array to create a new array fulfilled with strings instead of SimpleXMLElement, such as :
<?php
// your array (already built)
$arraySimpleXml = array(
"example1" => new SimpleXMLElement("<test>value</test>"),
"example2" => new SimpleXMLElement("<test>value2</test>")
);
// output array, to store in database
$result = array();
foreach($arraySimpleXml as $key => $simpleXml) {
$result[$key] = $simpleXml->asXML();
}
// gets your result as a string => you can now insert it into mysql
$dbInsertion = serialize($result);
?>
So I worked out how to change the data into a standard array rather than an array of SimpleXMLElements so that I can successfully insert it into a MySQL database.
When iterating the SimpleXMLElement object to extract the data I needed I cast the type as String so that now my array has the format (as opposed to above):
Array
(
[0] => Array
(
[uid] => WOS:000238186400009
[journal] => JOURNAL OF ZOOLOGY
[publication] => Abundance of hedgehogs (Erinaceus europaeus) in relation to the density and distribution of badgers (Meles meles)
[year] => 2006
[author1] => Young, RP
[address] => Cent Sci Lab, Sand Hutton, Yorks, England
[author2] => Davison, J
[author3] => Trewby, ID
[citations] => 15
)
)
Thought I'd post this in case anyone has a similar problem in future. To do this, when iterating the data instead of:
$uid = $record->UID;
I did:
$uid = (string)$record->UID;
For each of the data fields I required. This ensures the data is stored as a String and so removes the SimpleXMLElement format.