Here is what my object looks like with print_r (this is an object returned by the PHP SDK for the Amazon Web Services Simple DB.
[GetAttributesResult] => CFSimpleXML Object
(
[Attribute] => Array
(
[0] => CFSimpleXML Object
(
[Name] => data_datein
[Value] => 2011-04-23
)
[1] => CFSimpleXML Object
(
[Name] => data_estatus
[Value] => 0
)
[2] => CFSimpleXML Object
(
[Name] => data_status
[Value] => 1
)
[3] => CFSimpleXML Object
(
[Name] => data_title
[Value] => Company Info
)
[4] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => firsttag
)
[5] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => secondtag
)
[6] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => thirdtag
)
[7] => CFSimpleXML Object
(
[Name] => data_files
[Value] => company_info.flv
)
[8] => CFSimpleXML Object
(
[Name] => data_id
[Value] => 8993
)
)
)
I have a function that iterates over the GetAttributesResult Object and creates an associative array that makes it easy to reference my fields by their names. One of my Names is data_tags, which is repeated an unknown number of times. I would like to return data_tags as a simple indexed array of those values. Here's my function, which doesn't work.
function attrToArray($select) {
$results = array();
$x = 0;
foreach($select->body->GetAttributesResult as $result) {
foreach ($result as $field) {
if (array_key_exists($field,$results[$x])) {
$results[$x][ (string) $field->Name ][] = (string) $field->Value;
} else {
$results[$x][ (string) $field->Name ] = (string) $field->Value;
}
}
$x++;
}
return $results;
}
I don't know if this is the most elegant solution, but I don't see why it wouldn't work. array_key_exists doesn't return true. By mistake I was able to test as in_array($field-Name,$results[$x]) and that built the array of my repeated $field->Name values... but it also converted all of the other values into single item nested array... so it would seem that it returned true more than I thought it would have. Although the hyphen in there was by mistake I meant to use -> which doesn't return true... so I'm very confused by what is going on there. Here's the print_r to show what came back.
Array ( [0] => Array (
[data_datein] => 2011-04-23
[data_estatus] => 0
[data_status] => Array ( [0] => 1 )
[data_title] => Array ( [0] => Company Info )
[data_tags] => Array (
[0] => firsttag
[1] => secondtag
[2] => thirdtag )
[data_files] => Array ( [0] => company_info.flv )
[data_id] => Array ( [0] => 8993 ) ) )
Any pointers, suggestions or instruction on how I might handle this better... and at very least if someone can figure out how I can get to the above array without the nested arrays on the other non-redundant fields. Very much appreciated!
Here is the print_r() of $result
CFSimpleXML Object
(
[Attribute] => Array
(
[0] => CFSimpleXML Object
(
[Name] => data_datein
[Value] => 2011-04-23
)
[1] => CFSimpleXML Object
(
[Name] => data_estatus
[Value] => 0
)
[2] => CFSimpleXML Object
(
[Name] => data_title
[Value] => 0001 01 Company Name
)
[3] => CFSimpleXML Object
(
[Name] => data_status
[Value] => 1
)
[4] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => good stuff
)
[5] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => save tags
)
[6] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => tagger works
)
[7] => CFSimpleXML Object
(
[Name] => data_files
[Value] => 0001_01_company_name.flv
)
[8] => CFSimpleXML Object
(
[Name] => data_id
[Value] => yFKwIxjIhH
)
)
)
and here is a print_r() of $field (iterated and separated by <hr> tags.)
CFSimpleXML Object
(
[Name] => data_datein
[Value] => 2011-04-23
)
<hr>CFSimpleXML Object
(
[Name] => data_estatus
[Value] => 0
)
<hr>CFSimpleXML Object
(
[Name] => data_title
[Value] => 0001 01 Company Name
)
<hr>CFSimpleXML Object
(
[Name] => data_status
[Value] => 1
)
<hr>CFSimpleXML Object
(
[Name] => data_tags
[Value] => good stuff
)
<hr>CFSimpleXML Object
(
[Name] => data_tags
[Value] => save tags
)
<hr>CFSimpleXML Object
(
[Name] => data_tags
[Value] => tagger works
)
<hr>CFSimpleXML Object
(
[Name] => data_files
[Value] => 0001_01_company_name.flv
)
<hr>CFSimpleXML Object
(
[Name] => data_id
[Value] => yFKwIxjIhH
)
In the AWS PHP SDK, you can use to_json(), to_stdClass() and even to_array() to get back other data types from a CFSimpleXML object. Also with SimpleXML objects, typecasting is key!
PHP has an object called ArrayObject which is more-or-less an OOP version of an array. When you call CFSimpleXML->to_array(), you get back a CFArray object, which wraps the native ArrayObject object with extra functionality.
$array = $response->body->GetAttributesResult->to_array();
list($name, $value) = $array['Attribute']->first()->map(function($node, $i) {
return (string) $node;
});
http://docs.amazonwebservices.com/AWSSDKforPHP/latest/#i=CFSimpleXML
http://docs.amazonwebservices.com/AWSSDKforPHP/latest/#i=CFArray
enter code hereDo you mean something like this:
$data_tags = array();
foreach ( $select->body->GetAttributesResult AS $attr ) {
if ( $attr->Name == 'data_tags' ) {
$data_tags[] = $attr->Value;
}
}
Otherwise, I don't know what you want =)
edit
Are you sure GetAttributesResult is right? Don't you mean http://www.php.net/manual/en/simplexmlelement.attributes.php?
I would suggest something like that.
UPDATED:
function getAttributesIntoArray( $select )
{
$results = array();
$x = 0;
foreach ( $select->body->GetAttributesResult as $result )
{
foreach ( $result as $field )
{
if ( ! isset( $results[ $x ] ) )
{
$results[ $x ] = array();
}
// Assuming, that if the $field->Value is array, then it probably have only one element
if ( $field )
{
// or if ( isset( $results[ $x ][ (string) $field->Name ] ) ) instead of array_key_exists
if ( array_key_exists( (string) $field->Name, $results[ $x ] ) )
{
$results[ $x ][ (string) $field->Name ][] = ( is_array( $field->Value ) ) ? $field->Value[0] : $field->Value;
}
else
{
$results[ $x ][ (string) $field->Name ] = ( is_array( $field->Value ) ) ? $field->Value[0] : $field->Value;
}
}
}
$x++;
}
return $results;
}
I was able to get this one to work. Hope this helps.
protected function CFResponseToArray($response)
{
try {
if ($response->isOK()) {
$responseObj = $response->body->to_array()->getArrayCopy();
//log_message('info', print_r($responseObj, true));
$result = array();
if (isset($responseObj['SelectResult']['Item'])) {
if (is_array($responseObj['SelectResult']['Item'])) {
if (isset($responseObj['SelectResult']['Item']['Name'])) {
$itemObj = array();
//log_message('info', print_r($responseObj['SelectResult'], true));
$resultItem = $responseObj['SelectResult']['Item'];
$itemObj['Id'] = $resultItem['Name'];
$attributes = $resultItem['Attribute'];
for ($i = 0; $i < count($attributes); $i++) {
$itemObj[$attributes[$i]['Name']] = $attributes[$i]['Value'];
}
$result[] = $itemObj;
} else {
//log_message('info', print_r($responseObj['SelectResult'], true));
foreach ($responseObj['SelectResult']['Item'] as $resultItem) {
$itemObj = array();
$itemObj['Id'] = $resultItem['Name'];
$attributes = $resultItem['Attribute'];
for ($i = 0; $i < count($attributes); $i++) {
$itemObj[$attributes[$i]['Name']] = is_array($attributes[$i]['Value']) ? "" : $attributes[$i]['Value'];
}
$result[] = $itemObj;
}
if (isset($responseObj['SelectResult']['NextToken'])) {
$this->nextToken = $responseObj['SelectResult']['NextToken'];
} else {
$this->nextToken = '';
}
}
}
}
return $result;
}
} catch (exception $ex) {
log_message('error', $ex->getMessage());
}
}
function attrToArray($select) {
$results = array();
$x = 0;
foreach ( $select->body->GetAttributesResult as $result ) {
foreach ( $result as $field ) {
$check = (string) $field->Name;
if (isset($field) && array_key_exists($check, $results[$x] ) ) {
if ( ! is_array( $results[ $x ][$check] ) ) {
$val = (string) $results[ $x ][$check];
$results[ $x ][ $check ] = array();
$results[ $x ][ $check ][] = $val;
}
$results[ $x ][ $check ][] = (string) $field->Value;
} else {
$results[ $x ][ $check ] = (string) $field->Value;
}
}
$x++;
}
return $results;
}
Related
I have images saved in db and on server.
I have to get images from db and compare them with those on server. If match, then show images (make new array with matched image names and fetch them in browser)
Example:
$db_image_array
Array
(
[0] => Array
(
[id] => 14
[eid] => 27
[stype_id] => 1 // folder1
[ihash] => oOYvcSEGeUjDpL2a.jpg
)
[1] => Array
(
[id] => 19
[eid] => 27
[stype_id] => 3 // folder3
[ihash] => XYmpP49Z5ye2OTL8.jpg
)
[2] => Array
(
[id] => 20
[eid] => 27
[stype_id] => 1 // folder1
[ihash] => uqF6whY6F6zNmdC7.jpg
)
[3] => Array
(
[id] => 21
[eid] => 27
[stype_id] => 1 // folder1
[ihash] => KXuYIidPpHrkZc96.jpg
)
)
$serverFolderArray
Array
(
[folder1] => Array
(
[0] => SplFileInfo Object
(
[pathName:SplFileInfo:private] => /var/www/vhosts/img/1629973f75566aed/folder1/uqF6whY6F6zNmdC7.jpg
[fileName:SplFileInfo:private] => uqF6whY6F6zNmdC7.jpg
)
[1] => SplFileInfo Object
(
[pathName:SplFileInfo:private] => /var/www/vhosts/img/1629973f75566aed/folder1/oOYvcSEGeUjDpL2a.jpg
[fileName:SplFileInfo:private] => oOYvcSEGeUjDpL2a.jpg
)
[2] => SplFileInfo Object
(
[pathName:SplFileInfo:private] => /var/www/vhosts/img/1629973f75566aed/folder1/KXuYIidPpHrkZc96.jpg
[fileName:SplFileInfo:private] => KXuYIidPpHrkZc96.jpg
)
)
[folder2] => Array
(
)
[folder3] => Array
(
[0] => SplFileInfo Object
(
[pathName:SplFileInfo:private] => /var/www/vhosts/img/1629973f75566aed/folder3/XYmpP49Z5ye2OTL8.jpg
[fileName:SplFileInfo:private] => XYmpP49Z5ye2OTL8.jpg
)
)
[folder4] => Array
(
)
)
My first try was this code:
if ($db_image_array) {
foreach ($serverFolderArray as $arrayName => $serverImageArray) {
if ( ! empty($serverImageArray)) {
for ($y = 0; $y < count($db_image_array); $y++) {
for ($x = 0; $x < count($serverImageArray); $x++) {
if ($serverImageArray[$x]->getFilename() === $db_image_array[$y]['ihash']) {
$arr[$x] = $db_image_array[$y]['ihash'];
$chunked_array[$arrayName] = array_chunk($arr, 4);
}
}
}
}
}
}
there was also a second try with foreach loop:
if ( ! empty($serverImageArray)) {
for ($x = 0; $x < count($serverImageArray); $x++) {
foreach ($db_image_array as $key => $val) {
if ($serverImageArray[$x]->getFilename() === $val['ihash']) {
$arr[$x] = $val['ihash'];
$chunked_array[$arrayName] = array_chunk($arr, 4);
break;
}
}
}
}
both ways give me wrond image sort chunked_array:
Array
(
[folder1] => Array
(
[0] => Array
(
[0] => oOYvcSEGeUjDpL2a.jpg
[1] => uqF6whY6F6zNmdC7.jpg
[2] => KXuYIidPpHrkZc96.jpg
)
)
[folder3] => Array
(
[0] => Array
(
[0] => oOYvcSEGeUjDpL2a.jpg // this one has not to be here
[1] => XYmpP49Z5ye2OTL8.jpg
[2] => KXuYIidPpHrkZc96.jpg // and this one also doesn't have to be in this array
)
)
)
How do you see this situation? Where is a logic mistake?
So, was struggling with this bug for hours, but 30 min after creating this topic found a logic bug, I have to clear $arr each time new array name comes in:
$arr=[]; // <---- !!!
if ( ! empty($serverImageArray)) {
for ($x = 0; $x < count($serverImageArray); $x++) {
foreach ($db_image_array as $key => $val) {
if ($serverImageArray[$x]->getFilename() === $val['ihash']) {
$arr[$x] = $val['ihash'];
$chunked_array[$arrayName] = array_chunk($arr, 4);
}
}
}
}
I have an array, when i tried to print_r was just like this :
Array ( [user_id] => Erick ) Array ( [user_id] => Baldi) Array([user_id]=> Bintang ) Array ( [user_id] => Bagas ) Array ( [user_id] => Baim )
My Expected output just like :
Array (
Array (
[user_id] => Erick
)
Array (
[user_id] => Baldi
)
Array (
[user_id] => Bintang
)
Array (
[user_id] => Bagas
)
Array (
[user_id] => Baim
)
)
Anyone here have an idea ? I'm stuck with this.
This my php code :
public function get_userid() {
// $action = $this->input->post('action');
$customerField = $this->input->post('customer');
$projectField = $this->input->post('project');
$user_roleField = $this->input->post('role');
for ($i=0; $i<count($customerField); $i++) {
for ($j=0; $j<count($projectField); $j++) {
for ($k=0; $k<count($user_roleField); $k++) {
array_push($test, $this->get_array_push($customerField[$i], $projectField[$j], $user_roleField[$k]));
}
}
}
}
public function get_array_push($customer, $project, $user_role) {
$query = $this->db->query("SELECT user_id
FROM `ixt_user_project_list`
LEFT JOIN ixt_user_type ON ixt_user_project_list.user_type = ixt_user_type.user_type
WHERE ixt_user_project_list.user_cust_id ='".$customer."'
AND ixt_user_project_list.user_project_id ='".$project."'
AND ixt_user_type.user_owner ='".$user_role."'")->result_array();
//Filter null array
foreach ($query as $key => $value) {
print_r($value);
}
}
And this is my initial query value :
Array ( [0] => Array ( [user_id] => Erick ) ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( [0] => Array ( [user_id] => Baldi ) [1] => Array ( [user_id] => Bintang ) ) Array ( [0] => Array ( [user_id] => Bagas ) [1] => Array ( [user_id] => Baim ) )
As #scaisEdge wrote - its happening because you use the print_r inside the foreach loop.
If you want to filter null result you can use array-filter as:
$arr = array(array("user_id" => "aaa"), null, array(), array("user_id" => null), array("otherField" => "bbb"));
$filterdResult = array_filter($arr, function($elem) {
return ($elem && array_key_exists("user_id", $elem) && !is_null($elem["user_id"]));
});
Now you can just do print_r($filterdResult );
This will output:
Array
(
[0] => Array
(
[user_id] => aaa
)
)
Edited: After setting your new input as:
$query = array(array(array("user_id" => "aaa"), null, array(), array("user_id" => null)), array(array("user_id" => "ccc"), null, array(),array("user_id" =>"ddd")));
You can use the following:
$res = array();
//Filter null array
foreach ($query as $key => $value) {
array_push($res, array_filter($value, function($elem) {
return ($elem && array_key_exists("user_id", $elem) && !is_null($elem["user_id"])); }));
}
print_r($res);
Which output:
Array
(
[0] => Array
(
[0] => Array
(
[user_id] => aaa
)
)
[1] => Array
(
[0] => Array
(
[user_id] => ccc
)
[3] => Array
(
[user_id] => ddd
)
)
)
From here flatten is available if needed.
#David Winder I tried to combine as your suggesstion with the null array filter.
$res = array();
//Filter null array
foreach ($query as $key => $value) {
if (!is_null($value)) {
$res[] = $value;
//print_r($res);
}
}
But when i print_r($res) inside the foreach loop the result became duplicate array inside all of the array value.
I have two arrays with different structures. Array 1 and Array 2 that I will name from MyList and MyFiles. I would get to return only MyList values that do not have in MyFiles. But the two arrays have different structures and I'm having trouble trying to compare
MyList
Array
(
[info] => Array
(
[0] => Array
(
[player] => Messi
[week] => Array
(
[id] => 252
[videos] => Array
(
[0] => Array
(
[id] => 2929850
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929848
[link] => best.mp4
)
[2] => Array
(
[id] => 2929847
[link] => dribbling.mp4
)
)
)
)
[1] => Array
(
[player] => CR7
[week] => Array
(
[id] => 251
[videos] => Array
(
[0] => Array
(
[id] => 2929796
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929795
[link] => best.mp4
)
)
)
)
[2] => Array
(
[player] => Neymar
[week] => Array
(
[id] => 253
[videos] => Array
(
[0] => Array
(
[id] => 2929794
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929793
[link] => best.mp4
)
)
)
)
)
)
MyFiles Array
Array
(
[252] => Array
(
[0] => Array
(
[id] => 2929850
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929848
[link] => best.mp4
)
)
[251] => Array
(
[0] => Array
(
[id] => 2929796
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929795
[link] => best.mp4
)
)
)
the comparison must be made by id of the week and id of the video
I tried this but it did not work out:
$new = array();
foreach ($list['info'] as $source) {
foreach ($source["week"]['videos'] as $keys => $videos) {
foreach ($file as $key => $upload) {
if ($source["week"]["id"] == $key ) {
for($i=0; $i<count($source["week"]["videos"]); $i++){
if($videos["id"] == $upload[$i]["id"]){
unset($videos);
}else{
$new[] = $videos;
}
}
} else {
$new[] = $videos;
}
}
}
}
The expected return would be something like this.
Array
(
[info] => Array
(
[0] => Array
(
[player] => Messi
[week] => Array
(
[id] => 252
[videos] => Array
(
[2] => Array
(
[id] => 2929847
[link] => dribbling.mp4
)
)
)
)
[2] => Array
(
[player] => Neymar
[week] => Array
(
[id] => 253
[videos] => Array
(
[0] => Array
(
[id] => 2929794
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929793
[link] => best.mp4
)
)
)
)
)
)
I have hidden the array in a usable format for my sake in the future should this answer be incorrect and need changing.
$desired = array();
$desired['info'][0]['player'] = 'Messi';
$desired['info'][0]['week']['id'] = 252;
$desired['info'][0]['week']['videos'][2]['id'] = 2929847;
$desired['info'][0]['week']['videos'][2]['link'] = 'dribbling.mp4';
$desired['info'][2]['player'] = 'Neymar';
$desired['info'][2]['week']['id'] = 253;
$desired['info'][2]['week']['videos'][0]['id'] = 2929794;
$desired['info'][2]['week']['videos'][0]['link'] = 'goals.mp4';
$desired['info'][2]['week']['videos'][1]['id'] = 2929793;
$desired['info'][2]['week']['videos'][1]['link'] = 'best.mp4';
$list = array();
$list["info"][0]["player"] = "Messi";
$list["info"][0]["week"]["id"] = "252";
$list["info"][0]["week"]["videos"][0]["id"] = 2929850;
$list["info"][0]["week"]["videos"][0]["link"] = "goals.mp4";
$list["info"][0]["week"]["videos"][1]["id"] = 2929848;
$list["info"][0]["week"]["videos"][1]["link"] = "best.mp4";
$list["info"][0]["week"]["videos"][2]["id"] = 2929847;
$list["info"][0]["week"]["videos"][2]["link"] = "dribbling.mp4";
$list["info"][1]["player"] = "CR7";
$list["info"][1]["week"]["id"] = "251";
$list["info"][1]["week"]["videos"][0]["id"] = 2929796;
$list["info"][1]["week"]["videos"][0]["link"] = "goals.mp4";
$list["info"][1]["week"]["videos"][1]["id"] = 2929795;
$list["info"][1]["week"]["videos"][1]["link"] = "best.mp4";
$list["info"][2]["player"] = "Neymar";
$list["info"][2]["week"]["id"] = "253";
$list["info"][2]["week"]["videos"][0]["id"] = 2929794;
$list["info"][2]["week"]["videos"][0]["link"] = "goals.mp4";
$list["info"][2]["week"]["videos"][1]["id"] = 2929793;
$list["info"][2]["week"]["videos"][1]["link"] = "best.mp4";
$file = array();
$file[252][0]['id'] = 2929850;
$file[252][0]['link'] = 'goals.mp4';
$file[252][1]['id'] = 2929848;
$file[252][1]['link'] = 'best.mp4';
$file[251][0]['id'] = 2929796;
$file[251][0]['link'] = 'goals.mp4';
$file[251][1]['id'] = 2929795;
$file[251][1]['link'] = 'best.mp4';
Edit
function array_diff_assoc_recursive($array1, $array2) {
$difference=array();
foreach ($array1 as $key => $value) {
if (is_array($value)) {
if( !isset($array2[$key]) || !is_array($array2[$key])) {
$difference[$key] = $value;
} else {
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if (!empty($new_diff))
$difference[$key] = $new_diff;
}
} else if (!array_key_exists($key,$array2) || $array2[$key] !== $value) {
$difference[$key] = $value;
}
}
return $difference;
}
$new = array('info' => array());
foreach ($list['info'] as $key => $item) {
$a = $item['week']['videos'];
//$b = $file[$item['week']['id']] ?? []; // This is PHP7+
$b = isset($file[$item['week']['id']]) ? $file[$item['week']['id']] : [];
$c = array_diff_assoc_recursive($a, $b);
if (!empty($c)) {
$new['info'][$key] = $item;
$new['info'][$key]['week']['videos'] = $c;
}
}
You will need a function that will check the difference between the videos arrays.
What I do is simply iterate over the list array and then check the difference between that item and the file array. The difference is then stored in $c.
If there is a difference then if statement is fired which will store that player in the $new array and then replace the videos array with the difference array.
This is similar to what you were doing when you were unsetting variables.
I'm hoping there is a better way of returning the values from each of cy_GB['value] and en_GB['value] from the array below:
MultilingualSelectAttributeTypeOptionList Object (
[options:MultilingualSelectAttributeTypeOptionList:private] => Array
(
[0] => MultilingualSelectAttributeTypeOption Object
(
[error] =>
[id] => 7
[values] => Array
(
[cy_GB] => Array
(
[id] => 13
[value] => Audio described
)
[en_GB] => Array
(
[id] => 14
[value] => Audio described
)
)
[th] => TextHelper Object
(
)
)
[1] => MultilingualSelectAttributeTypeOption Object
(
[error] =>
[id] => 3
[values] => Array
(
[cy_GB] => Array
(
[id] => 5
[value] => BSL signed
)
[en_GB] => Array
(
[id] => 6
[value] => BSL signed
)
)
[th] => TextHelper Object
(
)
)
)
[error] =>
)
This is what I've tried. I should also use more meaningful names.:
foreach ($selectedOptions as $row) {
foreach ($row as $key) {
foreach ($key as $k => $v) {
if($k == 'cy_GB') {
echo $v['value'];
}
if($k == 'en_GB') {
echo $v['value'];
}
}
}
}
I know this kind of thing has been asked many times before so I apologise for that. any help would be most appreciated.
Something like this could work:
function findKeyRec($obj, $search) {
if( !is_array( $obj ) && !$obj instanceof Traversable ) return;
foreach($obj as $key => $value) {
if($key == $search) {
echo $value['value'];
} else {
findKeyRec($value, $search);
}
}
}
findKeyRec($ar, 'cy_GB');
findKeyRec($ar, 'en_GB');
It's not shorter, but in my opinion more elegant, and it should work with any object/array structure.
Untested.
I have an object array that looks like this:
Array
(
[0] =>Object
(
[ClassScheduleID] => 2263
[Name] => Workout 1
[Location] => Object
(
[BusinessID] => 1
)
)
[1] =>Object
(
[ClassScheduleID] => 2263
[Name] => Workout 1
[Location] => Object
(
[BusinessID] => 13
)
)
[2] =>Object
(
[ClassScheduleID] => 2264
[Name] => Workout 2
[Location] => Object
(
[BusinessID] => 22
)
)
I am looking to identify that the ClassScheduleID of 2263 is a duplicate, and remove the duplicate entry's entire object from the array. So that I get:
Array
(
[0] =>Object
(
[ClassScheduleID] => 2263
[Name] => Workout 1
[Location] => Object
(
[BusinessID] => 1
)
)
[1] =>Object
(
[ClassScheduleID] => 2264
[Name] => Workout 2
[Location] => Object
(
[BusinessID] => 22
)
)
I tried the solution proposed here
How to remove duplicate values from a multi-dimensional array in PHP
but the count() remained the same
$count = 0;
foreach($arrayObj as $key => $value) {
if ($value['ClassScheduleID'] == 2263) {
$count++;
}
if ($count > 1){
unset($arrayObj[$key]);
$count--;
}
}
It works: http://ideone.com/fork/zrdDtu
Edit: Modified to delete any duplicates:
foreach($arrayObj as $key => $value) {
$count = 0;
foreach($arrayObj as $nkey => $nvalue) {
if ($value['ClassScheduleID'] == $nvalue['ClassScheduleID']) {
$count++;
}
if ($count > 1){
unset($arrayObj[$key]);
$count--;
}
}
}
var_dump($arrayObj);
See it here: http://ideone.com/fork/85RCst
function get_unique_array($array)
{
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($result as $key => $value)
{
if ( is_array($value) )
{
$result[$key] = get_unique_array($value);
}
}
return $result;
}
Demo: http://3v4l.org/WOTHi#v430