Remove Child Array If Value of A Key is Duplicate - php

I want to remove a child array from a multi-dimensional array in case a duplicate value found for a particular key. The answer(s) here didn't work at all. The answer here works, however, for large amount of arrays, that gets pretty slower. Looking for a cleaner and faster solution.
Example PHP Array
$args = array();
$args[] = array(
'section' => array(
'id' => 'section1',
'name' => 'Section 1',
),
'name' => 'Shortcode Name',
'action' => 'shortcodeaction',
'icon' => 'codeicon',
'image' => 'codeimage',
);
$args[] = array(
'section' => array(
'id' => 'section2',
'name' => 'Section 2',
),
'name' => 'Shortcode2 Name',
'action' => 'shortcodeaction2',
'icon' => 'codeicon2',
'image' => 'codeimage2',
);
$args[] = array(
'section' => array(
'id' => 'section3',
'name' => 'Section 3',
),
'name' => 'Shortcode3 Name',
'action' => 'shortcodeaction3',
'icon' => 'codeicon3',
'image' => 'codeimage3',
);
$args[] = array(
'section' => array(
'id' => 'section1',
'name' => 'Section 4',
),
'name' => 'Shortcode4 Name',
'action' => 'shortcodeaction4',
'icon' => 'codeicon4',
'image' => 'codeimage4',
);
$args[] = array(
'section' => array(
'id' => 'section5',
'name' => 'Section 5',
),
'name' => 'Shortcode5 Name',
'action' => 'shortcodeaction5',
'icon' => 'codeicon5',
'image' => 'codeimage5',
);
$sections = array();
foreach ( $args as $arg ) {
$sections[] = $arg['section'];
}
And, the print_r($sections) result.
Array
(
[0] => Array
(
[id] => section1
[name] => Section 1
)
[1] => Array
(
[id] => section2
[name] => Section 2
)
[2] => Array
(
[id] => section3
[name] => Section 3
)
[3] => Array
(
[id] => section1
[name] => Section 4
)
[4] => Array
(
[id] => section5
[name] => Section 5
)
)
Both Array[0] and Array[3] has the same value for the key id, therefor the entire Array[3] has to be removed in my case to avoid duplicates.
This is working for me though, but it gets really slow when there are 100s or more arrays.
$knownIds = array();
foreach( $sections AS $key=>$item ) {
if( array_key_exists($item['id'], $knownIds) === true ) {
unset( $sections[$key] );
} else {
$knownIds[$item['id']] = $key;
}
}
$sections = array_values($sections);
Tried several answers here in StackOverflow (including this), but none of them helped in my case.
Thanks

You can modify the whole using array_column and array_filter -
//get all the sections value
$section = array_column($args, 'section');
//store ids in temp array
$idArray = array_unique(array_column($section, 'id'));
//filter the array having unique id
$uniqueSections = array_filter($section, function ($key, $value) use ($idArray) {
return in_array($value, array_keys($idArray));
}, ARRAY_FILTER_USE_BOTH);
var_dump($uniqueSections);
For PHP <5.5
$section = array_map(function($args) {
return $args['section'];
}, $args);
$idArray = array_unique(array_map(function($section){return $section['id'];}, $section));

Related

Extract a complete branch from a Parent-Child data structure in PHP using recursion

I have an object from a sql-query. Every entry has the keys (id, type, title, parent_id).
My example data:
Content of the sql-object $list (var_export()):
$array = array(0 => (object) array( 'id' => 1, 'type' => 'label', 'title' => 'Product Categories', 'parent_id' => 0, ),
1 => (object) array( 'id' => 2, 'type' => 'label', 'title' => 'Shoes', 'parent_id' => 1, ),
2 => (object) array( 'id' => 3, 'type' => 'label', 'title' => 'T-Shirts', 'parent_id' => 1, ),
3 => (object) array( 'id' => 4, 'type' => 'label', 'title' => 'With Print', 'parent_id' => 2, ),
4 => (object) array( 'id' => 5, 'type' => 'label', 'title' => 'Without Print', 'parent_id' => 2, ),
5 => (object) array( 'id' => 6, 'type' => 'label', 'title' => 'Brands', 'parent_id' => 2, ),
6 => (object) array( 'id' => 7, 'type' => 'label', 'title' => 'Blue', 'parent_id' => 3, ),
7 => (object) array( 'id' => 8, 'type' => 'label', 'title' => 'Red', 'parent_id' => 3, ));
What i expect:
the function, should find the dependencies of the entries starting with a given id. Here for example the ID 7:
Array
(
[0] => stdClass Object
(
[id] => 7
[type] => "label"
[title] => "Blue"
[parent_id] => 3
)
[1] => stdClass Object
(
[id] => 3
[type] => "label"
[title] => "T-Shirts"
[parent_id] => 1
)
[2] => stdClass Object
(
[id] => 1
[type] => "label"
[title] => "Product Categories"
[parent_id] => 0
)
)
What i get:
I just get an array with the first entry, with the id I started with.
As example with starting ID 7:
array ( 0 => (object) array( 'id' => 7, 'type' => 'label', 'title' => 'Blue', 'parent_id' => 3, ), )
My current Function:
The function needs to search for the item with the given id, stores the information into a new array and then start a new search but with the parent_id as new search id. This should loop as long as there are dependencies, if there are no dependencies the loop should stop and returning the created array.
function getParentSelect($list, $parent) {
$next_id = true;
$result = array();
foreach($list as $k => $s) {
echo $s->id;
if ($s->id == $parent) {
$result[] = $s;
$next_id = $s->parent_id;
break;
}
else {
$next_id = false;
}
}
if ($next_id != false) {
$result = array_merge($result, getParentSelect($list, $next_id));
}
return $result;
}
Recursion is always somewhat difficult to understand. I think you got the main idea, but the execution was flawed. This is what I can up with:
function getParentSelect($list, $select_id) {
$result = [];
foreach($list as $s) {
if ($s->id == $select_id) {
$result = array_merge([$s], getParentSelect($list, $s->parent_id));
}
}
return $result;
}
The assumption here is that all the parent id's are valid.
How does the code work?
The function itself searched the whole list for items with the id that was selected. If it finds one it will add it to the results, but it also looks for any parents of that item. This is where the function recurses. This means that the function can also look for parents of parents, and so on. array_merge() is used to combine the items and all parents together to form the results.

Create an array from an array based on value and include matches within as an array

I'm wanting to group my data by a specific value (parent name) and then merge all the items that share the same parent name under a "items" array
However it's overwriting the items array, not adding to it, so for example "items" in the output should have multiple items not just one.
Any ideas?
$result = array();
foreach ($page->products_codes as $option) {
$result[$option->parent->name]["title"] = $option->parent->title;
$result[$option->parent->name]["items"] = $option->title;
}
Outputs as:
array (
'fixture' =>
array (
'title' => 'Fixture',
'items' => 'Pinhole90 Fixed with LED51',
),
'finish' =>
array (
'title' => 'Finish',
'items' => 'RAL',
),
'ip-rating' =>
array (
'title' => 'IP Rating',
'items' => 'IP54',
),
'emergency' =>
array (
'title' => 'Emergency',
'items' => 'Maintained 3hr Self Test',
),
'installation' =>
array (
'title' => 'Installation',
'items' => 'Plaster-kit for seamless flush appearance',
),
'led' =>
array (
'title' => 'LED',
'items' => 'LED50 ONE',
),
'cct' =>
array (
'title' => 'CCT',
'items' => '90 CRI 4000K',
),
'beam-angle' =>
array (
'title' => 'Beam Angle',
'items' => '38°',
),
'protocol' =>
array (
'title' => 'Protocol',
'items' => 'Bluetooth',
),
'louvre-lens' =>
array (
'title' => 'Louvre/Lens',
'items' => 'Heavy Spread Lens',
),
)
Any thoughts?
Based on the preferred data structure you specified:
$result = array();
foreach ($page->products_codes as $option) {
$result[$option->parent->name]["title"] = $option->parent->title;
$result[$option->parent->name]["items"][] = $option;
}
$result = array_values($result);
Here's a working example: https://3v4l.org/u9XBk

Replace key in array, with keeping order intact

I would like to replace keys in arrays, because I will move them on two indexes up.
Problem that I am facing is that those are containing same names which will not be ok, if i want to move them up.
This is how array looks like.
$list = array(
'ind' => array(
'messagetype' => 'Alert',
'visibility' => 'Public',
'info' => array(
0 => array(
'urgency' => 'Urgent',
'params' => array(
0 => array(
'Name' => 'display',
'value' => '3; top',
),
1 => array(
'Name' => 'level',
'value' => '1; blue',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GSSD154',
),
),
),
),
1 => array(
'messagetype' => 'Information',
'visibility' => 'Private',
'info' => array(
0 => array(
'urgency' => 'Minor',
'params' => array(
0 => array(
'Name' => 'display',
'value' => '1; left',
),
1 => array(
'Name' => 'level',
'value' => '1; red',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GBECS23',
),
),
),
),
),
),
),
),
);
and this is how I would like the output to be with changing keys in Name0, Name1, which are inside params.
$list = array(
'ind' => array(
'messagetype' => 'Alert',
'visibility' => 'Public',
'info' => array(
0 => array(
'urgency' => 'Urgent',
'params' => array(
0 => array(
'Name0' => 'display',
'value0' => '3; top',
),
1 => array(
'Name1' => 'level',
'value1' => '1; blue',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GSSD154',
),
),
),
),
1 => array(
'messagetype' => 'Information',
'visibility' => 'Private',
'info' => array(
0 => array(
'urgency' => 'Minor',
'params' => array(
0 => array(
'Name0' => 'display',
'value0' => '1; left',
),
1 => array(
'Name1' => 'level',
'value1' => '1; red',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GBECS23',
),
),
),
),
),
),
),
),
);
I have tried with a lots of examples over this website, but could not find one to achieve this.
Code that I used from
How to replace key in multidimensional array and maintain order
function replaceKey($subject, $newKey, $oldKey) {
// if the value is not an array, then you have reached the deepest
// point of the branch, so return the value
if (!is_array($subject)) {
return $subject;
}
$newArray = array(); // empty array to hold copy of subject
foreach ($subject as $key => $value) {
// replace the key with the new key only if it is the old key
$key = ($key === $oldKey) ? $newKey : $key;
// add the value with the recursive call
$newArray[$key] = replaceKey($value, $newKey, $oldKey);
}
return $newArray;
}
$s = replaceKey($list, 'Name0', 'Name');
print "<PRE>";
print_r($s);
at the moment I get this output:
[0] => Array
(
[Name0] => display
[value] => 1; left
)
[1] => Array
(
[Name0] => level
[value] => 1; red
)
any help would be appreciated. regards
A very strange question, but why not?
The following function returns nothing (a procedure) and changes the array in-place using references but feel free to rewrite it as a "real" function (without references and with a return statement somewhere).
The idea consists to search for arrays, with numeric keys and at least 2 items, in which each item has the Name and value keys. In other words, this approach doesn't care about paths where the targets are supposed to be:
function replaceKeys(&$arr) {
foreach ($arr as &$v) {
if ( !is_array($v) )
continue;
$keys = array_keys($v);
if ( count($keys) < 2 ||
$keys !== array_flip($keys) ||
array_keys(array_merge(...$v)) !== ['Name', 'value'] ) {
replaceKeys($v);
continue;
}
foreach ($v as $k => &$item) {
$item = array_combine(["Name$k", "value$k"], $item);
}
}
}
replaceKeys($list);
print_r($list);
demo

How to fetch data into array 1 from array 2, based on particular key value?

I have two arrays.
$primary = array(
[0] => array(
'name' => 'PPT Shop',
'place_id' => '1000',
'category' => '220,221',
'address' =>
),
[1] => array(
'name' => 'Meat Shop',
'place_id' => '1001',
'category' => '220,221'
'address' =>
),
[2] => array(
'name' => 'Bikini Shop',
'place_id' => '1010',
'category' => '100,102'
'address' =>
),
[3] => array(
'name' => 'Knife Shop',
'place_id' => '1012',
'category' => '1,3'
'address' =>
)
)
$moredata = array(
[0] => array(
'id' => '1000',
'category' => '900,901'
'address' => '35 Lawrence Park',
'phone' => '9000000099'
),
[1] => array(
'id' => '1001',
'category' => '909,300'
'address' => '39 Park Avenue',
),
[2] => array(
'id' => '1010',
'category' => '50,45'
'address' => '35 Trump Park',
'phone' => '8900000099'
)
)
I want to compare each data of $moredata with each data of $primary, and check if the place_id from $primary exists in $moredata. If it matches, then the corresponding records of that particular key will be updated. For example,
$newPrimary = array(
[0] => array(
'name' => 'PPT Shop',
'place_id' => '1000',
'category' => '220,221,900,901',
'address' => '35 Lawrence Park',
'phone' => '9000000099'
),
[1] => array(
'name' => 'Meat Shop',
'place_id' => '220,221,1001',
'category' => '220,221,909,300',
'address' => '39 Park Avenue',
),
[2] => array(
'name' => 'Bikini Shop',
'place_id' => '1010',
'category' => '100,102,50,45'
'address' => '35 Trump Park',
'phone' => '8900000099'
),
[3] => array(
'name' => 'Knife Shop',
'place_id' => '1012',
'category' => '1,3'
'address' =>
)
)
place_id(1000) of primary matches with id(1000) of moredata, so the place_id(1000) of newPrimary is like this:-
array(
'name' => 'PPT Shop',
'place_id' => '1000',
'category' => '220,221,900,901', // the categories get concated
'address' => '35 Lawrence Park',
'phone' => '9000000099'
)
However, for place_id(1001) of primary doesn't have phone field, so the id(1001) of newPrimary is like this:-
array(
'name' => 'Meat Shop',
'place_id' => '1001',
'category' => '220,221,909,300',
'address' => '39 Park Avenue',
)
place_id(1012) has no match, so it remains unchanged.
How can I create an array similar to newPrimary? It would be better if we can append the fields from moredata to the corresponding record from primary. I achieved the same using double foreach loop. I want to achieve this is a way to have lesser execution time.
foreach($primary as $key_fs => $prm)
{
foreach($moredata as $key_place => $pc)
{
if($prm['place_id'] == $pc['id'])
{
if(isset($pc['address']) && !empty($pc['address']))
$primary[$key_fs]['address'] = $pc['address'];
if(isset($pc['phone']) && !empty($pc['phone']))
$primary[$key_fs]['phone'] = $pc['phone'];
if(isset($pc['category']) && !empty($pc['category']))
$primary[$key_fs]['category'] .= ','.$pc['category'];
break;
}
}
}
Note:- The two arrays will have same dimension, but may not have same order.
You can use array_column to make the 2 arrays into multidimensional array. Use array_replace_recursive to merge the arrays.
You can use array_values if you want a simple array instead of multi dimensional output.
$primary = //Your array
$moredata = //Your array
$result = array_replace_recursive (array_column($primary, null, 'id') ,array_column($moredata, null, 'id') );
$result = array_values($result); //To convert the multidimensional array to simple array
Update:
You can use array_column to make a temp array for $moredata. This will make it easier to check if the id exist. Use the foreach to loop thru the array. If id exist on $moredata, simply concatenate the category.
$newMoreData = array_column($moredata, null, 'id');
$newPrimary = array();
foreach( $primary as $val ) {
if ( isset( $newMoreData[$val['place_id']] ) ) {
$temp = array_merge( $val, $newMoreData[$val['place_id']] );
$temp['category'] = $val['category'] . ',' . $newMoreData[$val['place_id']]['category'];
unset($temp['id']);
$newPrimary[] = $temp;
} else {
$newPrimary[] = $val;
}
}

How to search for subarrays with a specific ids and generate a new array from their elements?

I've been trying to organize data into a multidimensional array from a foreach loop but the data is all over the place.
This is what I coded:
$productIDs = array(
'0' => array(
'product_id' => '10',
'product_name' => 'Test',
'product_file' => 'file10',
'product_image' => 'https://i.imgur.com/dXb6.png',
),
'1' => array(
'product_id' => '20',
'product_name' => 'Test1',
'product_file' => 'file20',
'product_image' => 'https://i.imgur.com/MuP8.png',
),
'2' => array(
'product_id' => '30',
'product_name' => 'No product',
'product_file' => 'file30',
'product_image' => 'https://i.imgur.com/kWP3.png',
)
);
$urlIDs = array(10,20);
function getFiles($productIDs, $urlIDs)
{
foreach($productIDs as $ids)
{
foreach($urlIDs as $products)
{
if(in_array($products, $ids)){
$data1[] = $ids['product_id'];
$data2[] = $ids['product_file'];
$data3[] = $ids['product_image'];
}
}
}
return array($data1, $data2, $data3);
}
$getFiles = getFiles($productIDs, $urlIDs);
And the output for the following is:
Array
(
[0] => Array
(
[0] => 10
[1] => 20
)
[1] => Array
(
[0] => file10
[1] => file20
)
[2] => Array
(
[0] => https://i.imgur.com/dXb6.png
[1] => https://i.imgur.com/MuP8.png
)
)
Although what I'm trying to accomplish is:
Array
(
[0] => Array
(
[id] => 10
[file] => file10
[image] => https://i.imgur.com/dXb6.png
)
[1] => Array
(
[id] => 20
[file] => file20
[image] => https://i.imgur.com/MuP8.png
)
)
I tried the following:
function getFiles($productIDs, $urlIDs)
{
foreach($productIDs as $ids)
{
foreach($urlIDs as $products)
{
if(in_array($products, $ids)){
$data['id'] = $ids['product_id'];
$data['file'] = $ids['product_file'];
$data['image'] = $ids['product_image'];
}
}
}
return array($data);
}
Which returns the following without looping through all id's, it should return both arrays since both id's are matching.
Array
(
[0] => Array
(
[id] => 20
[file] => file20
[image] => https://i.imgur.com/MuP8.png
)
)
You could say the first batch of code without the specific key names isn't what I want, but I just wanted to show what I had tried. The last batch works (with key names), but doesn't loop through all of the $urlIDs, and for some reason that I can't understand, the code isn't even returning id = 10, it's returning id = 20, though it's second in the array. If someone could explain why this is happening I'd appreciate it.
In case it's useful: https://eval.in/764083
PHP code demo
<?php
$productIDs = array(
'0' => array(
'product_id' => '10',
'product_name' => 'Test',
'product_file' => 'file10',
'product_image' => 'https://i.imgur.com/dXb6.png',
),
'1' => array(
'product_id' => '20',
'product_name' => 'Test1',
'product_file' => 'file20',
'product_image' => 'https://i.imgur.com/MuP8.png',
),
'2' => array(
'product_id' => '30',
'product_name' => 'No product',
'product_file' => 'file30',
'product_image' => 'https://i.imgur.com/kWP3.png',
)
);
$urlIDs = array(10,20);
function getFiles($productIDs, $urlIDs)
{
$result=array();
foreach($productIDs as $key => $productData)
{
if(!in_array($productData['product_id'],$urlIDs))
{
unset($productIDs[$key]);
}
else
{
$result[]=array("id"=>$productData['product_id'],"file"=>$productData['product_file'],"image"=>$productData['product_image']);
}
}
return $result;
}
$getFiles = getFiles($productIDs, $urlIDs);
print_r($getFiles);
Output:
Array
(
[0] => Array
(
[id] => 10
[file] => file10
[image] => https://i.imgur.com/dXb6.png
)
[1] => Array
(
[id] => 20
[file] => file20
[image] => https://i.imgur.com/MuP8.png
)
)
Leveraging your tried out solution, change your tried solution to this.
function getFiles($productIDs, $urlIDs)
{
foreach($productIDs as $ids)
{
foreach($urlIDs as $products)
{
if(in_array($products, $ids)){
$data[] = [ "id" => $ids['product_id'],
"file" => $ids['product_file'],
"image" => $ids['product_image']];
}
}
}
return $data;
}
Try this one.
function getFiles($productIDs, $urlIDs)
{
$newDara = [];
foreach($productIDs as $ids)
{
foreach($urlIDs as $products)
{
if(in_array($products, $ids)){
$data['id'] = $ids['product_id'];
$data['file'] = $ids['product_file'];
$data['image'] = $ids['product_image'];
$newData[] = $data;
}
}
}
return array($newData);
}
Try this version
<?php
/**
* Created by PhpStorm.
* User: lenovo
* Date: 3/30/2017
* Time: 5:53 AM
*/
$productIDs = array(
'0' => array(
'product_id' => '10',
'product_name' => 'Test',
'product_file' => 'file10',
'product_image' => 'https://i.imgur.com/dXb6.png',
),
'1' => array(
'product_id' => '20',
'product_name' => 'Test1',
'product_file' => 'file20',
'product_image' => 'https://i.imgur.com/MuP8.png',
),
'2' => array(
'product_id' => '30',
'product_name' => 'No product',
'product_file' => 'file30',
'product_image' => 'https://i.imgur.com/kWP3.png',
)
);
$urlIDs = array(10,20);
$ouput = [];
foreach($productIDs as $product){
if(in_array($product['product_id'],$urlIDs)){
$ouput[] = array("id"=>$product["product_id"], "file"=>$product["product_file"], "image"=>$product["product_image"]);
}
}
echo "<pre>";
print_r($ouput);
echo "</pre>";
Check the
Good Luck
The other answers are either doing too many loops, or looping through the $productIDs array (which I assume is much longer in your actual project).
This method will only iterate on your search array, this should provide a performance boost. You will see this approach posted as the top comment # http://php.net/manual/en/function.array-search.php
Code: (Demo)
$productIDs = array(
'0' => array(
'product_id' => '10',
'product_name' => 'Test',
'product_file' => 'file10',
'product_image' => 'https://i.imgur.com/dXb6.png',
),
'1' => array(
'product_id' => '20',
'product_name' => 'Test1',
'product_file' => 'file20',
'product_image' => 'https://i.imgur.com/MuP8.png',
),
'2' => array(
'product_id' => '30',
'product_name' => 'No product',
'product_file' => 'file30',
'product_image' => 'https://i.imgur.com/kWP3.png',
)
);
$urlIDs=array(10,20);
foreach($urlIDs as $prd_id){
if(($i=array_search($prd_id,array_column($productIDs,'product_id')))!==false){
$result[]=['id'=>$productIDs[$i]['product_id'],
'file'=>$productIDs[$i]['product_file'],
'image'=>$productIDs[$i]['product_image']
];
}else{
echo "$prd_id not found";
}
}
var_export($result);
Output:
array (
0 =>
array (
'id' => '10',
'file' => 'file10',
'image' => 'https://i.imgur.com/dXb6.png',
),
1 =>
array (
'id' => '20',
'file' => 'file20',
'image' => 'https://i.imgur.com/MuP8.png',
),
)

Categories