I am calling a PHP file using http.post, passing a json object in the process.
I have managed to retrieve the object from within the PHP and have attached the dump below. All I now need is to retrieve 'name', 'email' and 'message' strings from the array but am finding this difficult as not used to PHP.
Connected successfully<pre>string(2467) "Array
(
[name] => Array
(
[$viewValue] => testing one two
[$modelValue] => testing one two
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => fullName
[$options] =>
)
[email] => Array
(
[$viewValue] => test#onetwo.com
[$modelValue] => test#onetwo.com
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => email
[$options] =>
)
[message] => Array
(
[$viewValue] => testing testing
[$modelValue] => testing testing
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => message
[$options] =>
)
)
"
<br /><br />Array
(
[name] => Array
(
[$viewValue] => testing one two
[$modelValue] => testing one two
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => fullName
[$options] =>
)
[email] => Array
(
[$viewValue] => test#onetwo.com
[$modelValue] => test#onetwo.com
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => email
[$options] =>
)
[message] => Array
(
[$viewValue] => testing testing
[$modelValue] => testing testing
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => message
[$options] =>
)
)
</pre>
The PHP code which retrieves the object in the first place is as follows:
$data = json_decode(file_get_contents('php://input'), TRUE);
$text = print_r($data,true);
echo "<pre>";
var_dump($text);
echo "<br /><br />";
print_r($text);
echo "</pre>";
How can I access the 'name', 'email' and 'message' strings please?
First of all your array isnt a valid one... it should be something like this:
BTW remove the $ from inside the arrays.
$newarr = Array(
"name" => Array(
"first_name" => "Alex",
"last_name" => "Gonzalez"
),
"email" => Array(),
"other_sub_array" => Array()
);
Now to get let say the first name, since it is a subarray (so an array inside another array).
echo $newarr['name']['first_name'];
// Result: Alex
Hope this helps.
UPDATE
I didn't mean not valid, it is a bad practice to use an array like that.
Your array is valid unlike the other answer supposes. I assume that you are using some framework that generates the relatively strange json you're reading in. But to the point you can get them via:
//note I used single quotes so that the dollar sign isn't evaluated to a php variable
$data['name']['$modelValue'];
$data['message']['$modelValue'];
$data['email']['$modelValue'];
Related
This question already has answers here:
PHP Getting Values From Nested Array
(2 answers)
Closed last year.
I have an array which looks like this:
Array
(
[response] => Array
(
[dataInfo] => Array
(
[totalRecordCount] => 362
[foundCount] => 5
[returnedCount] => 5
)
[data] => Array
(
[0] => Array
(
[fieldData] => Array
(
[groupAssetID] => 1020
[groupAssetName] => Standard Equipment
)
[portalData] => Array
(
)
[recordId] => 823
[modId] => 1
)
[1] => Array
(
[fieldData] => Array
(
[groupAssetID] => 1001
[groupAssetName] => Tools
)
[portalData] => Array
(
)
[recordId] => 829
[modId] => 1
)
[2] => Array
(
[fieldData] => Array
(
[groupAssetID] => 1005
[groupAssetName] => Spare Parts
)
[portalData] => Array
(
)
[recordId] => 830
[modId] => 1
)
)
)
[messages] => Array
(
[0] => Array
(
[code] => 0
[message] => OK
)
)
)
I'm trying to loop over this and extract the values for groupAssetID and groupAssetName in the data array but haven't been able to get this to work so far. I've tried:
foreach ( $records as $record) {
echo $field . ": " . $value . "<br />\r\n" ;
}
and
foreach ( $records->fieldData as $field=>$value) {
echo $field . ": " . $value . "<br />\r\n" ;
}
but none of these seem to work and I can't work out the correct syntax here.
I managed it to run with this syntax:
$records = array
(
'response' => array
(
'dataInfo' => array
(
'totalRecordCount' => 362,
'foundCount' => 5,
'returnedCount' => 5
),
'data' => array
(
0 => array
(
'fieldData' => array
(
'groupAssetID' => 1020,
'groupAssetName' => 'Standard Equipment'
),
'portalData' => array
(
),
'recordId' => 823,
'modId' => 1
),
1 => array
(
'fieldData' => array
(
'groupAssetID' => 1001,
'groupAssetName' => 'Tools'
),
'portalData' => array
(
),
'recordId' => 829,
'modId' => 1
),
2 => array
(
'fieldData' => array
(
'groupAssetID' => 1005,
'groupAssetName' => 'Spare Parts'
),
'portalData' => array
(
),
'recordId' => 830,
'modId' => 1
)
)
),
'messages' => array
(
0 => array
(
'code' => 0,
'message' => 'OK'
)
)
);
foreach ($records['response']['data'] as $key => $value) {
echo $value['fieldData']['groupAssetID'] . ": " . $value['fieldData']['groupAssetName'] . "<br />\r\n" ;
}
Output:
1020: Standard Equipment
1001: Tools
1005: Spare Parts
Is this what you need?
I'm using print_r(array_unique($array, SORT_REGULAR)); on the array below but it does not work.
I'm trying to filter out the redundant data.
Notice that [Order] and its key value pairs are all the same. But [Transaction] and its key value pairs are unique.
I need to get the [Order] element data and combine it with the 3 different [Transaction] elements.
My array
Array
(
[0] => Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[TransPO] => TR11211
[TransactionPrice] => 91.17
)
)
[1] => Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[TransPO] => TR11212
[TransactionPrice] => 180.41
)
)
[2] => Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[TransPO] => TR11213
[TransactionPrice] => 209.99
)
)
)
The final array I need will look something like this.
Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[0] => Array
(
[TransPO] => TR11211
[TransactionPrice] => 91.17
)
[1] => Array
(
[TransPO] => TR11212
[TransactionPrice] => 180.41
)
[2] => Array
(
[TransPO] => TR11213
[TransactionPrice] => 209.99
)
)
)
I can flatten the original array and then use array_unique, but wanted to see if there is a better way to accomplish what I need.
my code:
$myarray = array(
0 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11211", "TransactionPrice" => 91.17)
),
1 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11212", "TransactionPrice" => 180.41)
),
2 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11213", "TransactionPrice" => 209.99)
)
);
print_r(array_unique($myarray, SORT_REGULAR));
If you want to determine how many unique values of the Order element there are in your array, you need to apply array_unique only to the Order elements, which you can do using array_column:
$unique_orders = count(array_unique(array_column($myarray, 'Order'), SORT_REGULAR));
You can process your array using a list of keys which have non-unique values to generate an array, while other keys will have just a single value:
$non_unique_keys = ['Transaction'];
$output = array();
foreach (array_keys($myarray[0]) as $key) {
if (in_array($key, $non_unique_keys)) {
$output[$key] = array_column($myarray, $key);
}
else {
$output[$key] = $myarray[0][$key];
}
}
print_r($output);
Example Output:
Array (
[Order] => Array (
[PO] => TR11214
[OrderID] => 242856952012
)
[Sales Tax] => Array (
[PO] => TR11214
[SalesTaxAmount] => 0
)
[Transaction] => Array (
[0] => Array (
[TransPO] => TR11211
[TransactionPrice] => 91.17
)
[1] => Array (
[TransPO] => TR11212
[TransactionPrice] => 180.41
)
[2] => Array (
[TransPO] => TR11213
[TransactionPrice] => 209.99
)
)
)
Demo on 3v4l.org
array_unique() is intended for single dimensional arrays. If you want to use it on a multi-dimentional array, you should consider using usort() instead. Then you'll need to iterate through the array in reverse manually, searching for duplicates and removing them.
This question already has answers here:
PHP: Check for duplicate values in a multidimensional array
(6 answers)
Closed 7 years ago.
I have an array, last two elements are identical, i just want to check duplicate exist or not.
Array
(
[0] => Array
(
[crop] => CI-000001
[type] => PT-000001
)
[1] => Array
(
[crop] => CI-000001
[type] => PT-000003
)
[2] => Array
(
[crop] => CI-000005
[type] => PT-000014
)
[3] => Array
(
[crop] => CI-000005
[type] => PT-000014
)
)
You need to use array_unique function of PHP as
$ara = Array ( Array ( 'crop' => 'CI-000001', 'type' => 'PT-000001' ), Array
(
'crop' => 'CI-000001',
'type' => 'PT-000003'
), Array
(
'crop' => 'CI-000005',
'type' => 'PT-000014'
), Array
(
'crop' => 'CI-000005',
'type' => 'PT-000014'
)
);
echo "<pre>";
print_r(array_unique($ara,SORT_REGULAR));
echo "</pre>";
Output:
Array
(
[0] => Array
(
[crop] => CI-000001
[type] => PT-000001
)
[1] => Array
(
[crop] => CI-000001
[type] => PT-000003
)
[2] => Array
(
[crop] => CI-000005
[type] => PT-000014
)
)
Try the following code:
$hashes=array();
foreach ($myarray as $key=>$item) {
$hash=sha1(var_export($item, true));
if (isset($hashes($hash)) echo "$key is a duplicate of ".$hashes[$hash];
else $hashes[$hash]=$key;
}
try like this
<?php
$array = array(array('crop' => 'CI-000001','type' => 'PT-000001'), array('crop' => 'CI-000001','type' => 'PT-000003'),array('crop' => 'CI-000005','type' => 'PT-000014'),array('crop' => 'CI-000005','type' => 'PT-000014'));
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
echo "After Remove Duplicate:".'<pre>';
print_r( $array );
echo '</pre>';
?>
Output:-
After Remove Duplicate:
Array
(
[0] => Array
(
[crop] => CI-000001
[type] => PT-000001
)
[1] => Array
(
[crop] => CI-000001
[type] => PT-000003
)
[2] => Array
(
[crop] => CI-000005
[type] => PT-000014
)
)
Demo
I've an array titled $val. It's a blank array. I want it in following manner i.e. after print_r($val); it should print the array $val in following manner:
I've following variables too :
$_POST['fileName'] = 'Sample_1.docx';
$_POST['fileLink'] = 'https://www.filepicker.io/api/file/HEYQ6lPNRg2lzjUNhUGX';
Array
(
[status_info] =>
[user_status] => Share what's going on...
[group_id] =>
[action] => upload_photo_via_share
[is_activity_feed] => 1
[vshare] => Array
(
[Sample_1.docx] => Array
(
[0] => https://www.filepicker.io/api/file/HEYQ6lPNRg2lzjUNhUGX
)
)
[link] => Array
(
[url] => http://
)
[poll_question] =>
[answer] => Array
(
[0] => Array
(
[answer] =>
)
[1] => Array
(
[answer] =>
)
)
[is_profile] => no
[iframe] => 1
[method] => simple
[document_title] => document
[category] => Array
(
[0] =>
)
[code_snippet] =>
)
I want to add following structure to the variable $val. This thing should be done using PHP code only.
How should I add following structure of key-values to the array $val in PHP?
Thanks in advance.
The PHP source of this array should be like this:
$val = array(
'status_info' => '',
'user_status' => 'Share what's going on...',
...
'vshare' => array(
'Sample_1.docx' => array (
'https://www.filepicker.io/api/file/HEYQ6lPNRg2lzjUNhUGX'
)
),
'link' => array(
'url' => 'http://'
),
...
);
I want to get the value of 'GUID' with the value of 'SamAccountName'. i.e. I only have the value pf 'SamAccountName' and I would like to get the value of 'GUID' for that part of the array.
Array
(
[0] => Array
(
[DistinguishedName] => CN=johnn#playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
[GUID] => 26d7c204-7db7-4601-8cd2-0dd0d3b37d97
[OriginatingServer] => dcprov024-CA-1.exch024.domain.local
[Name] => johnn#playgroundla
[HostingObjectType] => Array
(
[HostingObjectTypes] => Array
(
[0] => ActiveSync
[1] => MSExchange2007Mailbox
[2] => ActiveDirectoryUser
)
)
[HostingOwners] => Array
(
[HostingObjectOwners] => Array
(
[0] => MSExchange2007Mailboxes
[1] => ActiveDirectoryUsers
)
)
[Attributes] => Array
(
[Hidden] =>
[ReadOnly] =>
[SpecialAccess] =>
[Items] => Array
(
)
)
[DisplayName] => John Nolan
[SamAccountName] => johnn_playgroundla
[FullSamAccountName] => EXCH024\johnn_playgroundla
[UserPrincipalName] => johnn#playgroundla.com
[AccountExpires] =>
[Enabled] =>
[EnabledFeatures] => Array
(
[string] => Array
(
[0] => ActiveSync
[1] => MSExchangeMailboxes
[2] => ActiveDirectoryUsers
)
)
[LastLogonTimestamp] =>
)
[1] => Array
(
[DistinguishedName] => CN=csliney#playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
[GUID] => 71224be8-1b8b-46e7-97ef-2cd873bf9b7f
[OriginatingServer] => dcprov024-CA-1.exch024.domain.local
[Name] => csliney#playgroundla
[HostingObjectType] => Array
(
[HostingObjectTypes] => Array
(
[0] => ActiveSync
[1] => MSExchange2007Mailbox
[2] => ActiveDirectoryUser
)
)
[HostingOwners] => Array
(
[HostingObjectOwners] => Array
(
[0] => MSExchange2007Mailboxes
[1] => ActiveDirectoryUsers
)
)
[Attributes] => Array
(
[Hidden] =>
[ReadOnly] =>
[SpecialAccess] =>
[Items] => Array
(
)
)
[DisplayName] => Christopher Sliney
[SamAccountName] => csliney_playgroundla
[FullSamAccountName] => EXCH024\csliney_playgroundla
[UserPrincipalName] => csliney#playgroundla.com
[AccountExpires] =>
[Enabled] =>
[EnabledFeatures] => Array
(
[string] => Array
(
[0] => ActiveSync
[1] => MSExchangeMailboxes
[2] => ActiveDirectoryUsers
)
)
[LastLogonTimestamp] =>
)
[2] => Array
(
[DistinguishedName] => CN=lee#playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
[GUID] => b428b57f-4cd4-4243-a76a-f25f5ff3be97
[OriginatingServer] => dcprov024-CA-1.exch024.domain.local
[Name] => lee#playgroundla
[HostingObjectType] => Array
(
[HostingObjectTypes] => Array
(
[0] => MSExchange2007Mailbox
[1] => ActiveDirectoryUser
)
)
[HostingOwners] => Array
(
[HostingObjectOwners] => Array
(
[0] => MSExchange2007Mailboxes
[1] => ActiveDirectoryUsers
)
)
[Attributes] => Array
(
[Hidden] =>
[ReadOnly] =>
[SpecialAccess] =>
[Items] => Array
(
)
)
[DisplayName] => Lee Roderick
[SamAccountName] => lee_playgroundla
[FullSamAccountName] => EXCH024\lee_playgroundla
[UserPrincipalName] => lee#playgroundla.com
[AccountExpires] =>
[Enabled] =>
[EnabledFeatures] => Array
(
[string] => Array
(
[0] => MSExchangeMailboxes
[1] => ActiveDirectoryUsers
)
)
[LastLogonTimestamp] =>
)
[3] => Array
(
[DistinguishedName] => CN=theresa#playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
[GUID] => 4b2aee17-9e88-4de9-b95b-63a9877835a6
[OriginatingServer] => dcprov024-CA-1.exch024.domain.local
[Name] => theresa#playgroundla
[HostingObjectType] => Array
(
[HostingObjectTypes] => Array
(
[0] => ActiveSync
[1] => MSExchange2007Mailbox
[2] => ActiveDirectoryUser
)
)
[HostingOwners] => Array
(
[HostingObjectOwners] => Array
(
[0] => MSExchange2007Mailboxes
[1] => ActiveDirectoryUsers
)
)
[Attributes] => Array
(
[Hidden] =>
[ReadOnly] =>
[SpecialAccess] =>
[Items] => Array
(
)
)
[DisplayName] => Theresa Baker
[SamAccountName] => theresa_playgroundla
[FullSamAccountName] => EXCH024\theresa_playgroundla
[UserPrincipalName] => theresa#playgroundla.com
[AccountExpires] =>
[Enabled] =>
[EnabledFeatures] => Array
(
[string] => Array
(
[0] => ActiveSync
[1] => MSExchangeMailboxes
[2] => ActiveDirectoryUsers
)
)
[LastLogonTimestamp] =>
)
)
This was originally a stdClass object but I used json_decode(json_encode($obj), true) to convert to an associative array.
Sounds like you want to get the GUID portion for the value of 'SamAccountName'. Use a foreach loop:
function getGUID($san, $array) {
foreach($array as $a) {
if($a['SamAccountName'] == $san) {
return $a['GUID'];
}
}
return false;
}
$guid = getGUID("SamAccountNameHere", $yourArray);
You can use a simple loop to fetch it
$id = 0;
foreach($data as $item) {
if (isset($item['SamAccountName']) && 'accountName' == $item['SamAccountName']) {
$id = $item['GUID'];
break;
}
}
var_dump($id);
is this what you are looking for?
function findBySam($arrayList, $sam) {
foreach($arrayList as $array) {
if($array['SamAccountName'] == $sam) {
return $array;
}
}
return false;
}
Here is an example of a function that you could use. This assumes that there will be only one object with the SamAccountName that you supply in the array (it just uses the first one that it finds). It returns the GUID of the matching array and false if it cannot find an array with a matching SamAccountName.
function getGuidForSamAccountName($arr, $name) {
foreach ($arr as $elem) {
if ($elem['SamAccountName'] === $name) {
return $elem['GUID'];
}
}
return false; //No match found
}
You can use array_filter function of php:
http://php.net/manual/en/function.array-filter.php
example:
$GUID = "sample";
array_filter($array, "findElement");
function findElement($el) {
return $el["GUID"] == $_GLOBAL["GUID"];
}
Not a very elegant solution... but it should work.