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
Related
This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 4 months ago.
There is an array with the following format.
I have used array_chunk function to format like the following array.
Array (
[0] => Array
(
[0] => Array
(
[SGST (2.5%)] => 2.5000
)
[1] => Array
(
[CGST (2.5%)] => 2.5000
)
)
[1] => Array
(
[0] => Array
(
[CGST (6%)] => 6.0000
)
[1] => Array
(
[SGST (6%)] => 6.0000
)
)
)
All I need my array to be displayed in the following format
Array
(
[0] => Array
(
[SGST (2.5%)] => 2.5000
[CGST (2.5%)] => 2.5000
)
[1] => Array
(
[CGST (6%)] => 6.0000
[SGST (6%)] => 6.0000
)
)
Help to create such format.Thanks
try combination of array_map, array_reduce with array_merge
To know more about used function check function doc on php.net
<?php
$array = array (
'0' => array
(
'0' => array
(
'SGST (2.5%)' => '2.5000'
),
'1' => array
(
'CGST (2.5%)' => '2.5000'
)
),
'1' => array
(
'0' => array
(
'CGST (6%)' => '6.0000'
),
'1' => array
(
'SGST (6%)' => '6.0000'
)
)
);
echo '<pre>';
$processed = array_map(function($a) { return array_reduce($a, 'array_merge', array()); }, $array);
print_r($processed);
Output
Array
(
[0] => Array
(
[SGST (2.5%)] => 2.5000
[CGST (2.5%)] => 2.5000
)
[1] => Array
(
[CGST (6%)] => 6.0000
[SGST (6%)] => 6.0000
)
)
I have following array as response from db. I am trying to convert this database response into multidimensional array as per my requirement.
Array
(
[0] => Array
(
[0] => Array
(
[_id] => C10359
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10428
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
[1] => Array
(
[0] => Array
(
[_id] => C10350
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10430
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
)
Now I need to convert above array in following way.
Array
(
[0] => Array
(
[C10359] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10428] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
[1] => Array
(
[C10350] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10430] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
following is way i am trying
array_map(function($arr) {
return $arr[0] ;
},$panel_result);
But it is not working.
Kindly suggest how can I convert in required formate.
This should do the trick :
$arr = array(
array(
array(
'_id' => 'C10359',
'AE' => array
(
89785,
89786,
89857,
89859,
),
),
array(
'_id' => 'C10428',
'AE' => array
(
50191,
50203,
50230,
50244,
),
),
),
);
$output = array();
foreach ($arr as $levelK => $level) {
if(!isset($output[$levelK])){
$output[$levelK] = array();
}
foreach ($level as $subLevel) {
$id = $subLevel['_id'];
if (!isset($output[$levelK][$id])) {
$output[$levelK][$id] = array();
}
foreach ($subLevel['AE'] as $val) {
$output[$levelK][$id][] = $val;
}
}
}
Hope this helps.
Use array_column() and pass third param as the index key.
$reqArray = array();
foreach ($yourArray as $key => $innerArray) {
$reqArray[] = array_column($innerArray, 'AE', '_id');
}
OR
Use array map()
$reqArray = array_map(function($a){
return array_column($a, 'AE', '_id');
},$arr);
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'];
I've an array:
Array
(
[_edit_lock] => Array
(
[0] => 1434971582:11
)
[_edit_last] => Array
(
[0] => 11
)
[_wp_page_template] => Array
(
[0] => page-templates/langenfeldDreiSpalterMitSiderbarsRL.php
)
[_wpas_done_all] => Array
(
[0] => 1
)
[hefo_before] => Array
(
[0] => 0
)
[hefo_after] => Array
(
[0] => 0
)
[sharing_disabled] => Array
(
[0] => 1
)
[spacious_page_layout] => Array
(
[0] => left_sidebar
)
[_thumbnail_id] => Array
(
[0] => 2641
)
[ort] => Array
(
[0] => langenfeld
)
)
I want to save the "ort" in a variable.
[ort] => Array
(
[0] => langenfeld
)
My code give me the values of the array but how can I save the values?
My code:
foreach ($gpc as $k){
foreach ($k as $v){
//echo $v;
}
}
I thought something like that:
$ort = $v['ort'];
But that's not working for me. Can someone help?
This code is working properly
$arr = Array
(
'_edit_lock' => Array
(
'0' => "1434971582:11",
),
'_edit_last' => Array
(
'0' => "11",
),
'_wp_page_template' => Array
(
'0' => "page-templates/langenfeldDreiSpalterMitSiderbarsRL.php",
),
'_wpas_done_all' => Array
(
'0' => "1",
),
'hefo_before' => Array
(
'0' => "0",
),
'hefo_after' => Array
(
'0' => "0",
),
'sharing_disabled' => Array
(
'0' => "1",
),
'spacious_page_layout' => Array
(
'0' => "left_sidebar",
),
'_thumbnail_id' => Array
(
'0' => "2641",
),
'ort' => Array
(
'0' => "langenfeld",
),
);
$ort = $arr["ort"];
print_r($ort);
// output
Array
(
[0] => langenfeld
)
if you directly want langenfeld
$ort = $arr['ort'][0];
//this will output - langenfeld
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.