Remove similar objects from array? - php

I have an array of objects, but I need to remove a similar objects by a few properties from them:
for example:
array(12) {
[0]=>
object(stdClass)#848 (5) {
["variant"]=>
object(stdClass)#849 (4) {
["name"]=>
string(8) "Alex"
}
["age"]=>
int(10)
}
[1]=>
object(stdClass)#851 (5) {
["variant"]=>
object(stdClass)#852 (4) {
["name"]=>
string(8) "Alex"
}
["age"]=>
int(10)
}
How to make a one object in array for this ( if for example I need to compare only by a name property? )
Still have an issue with it.
Updated
I've create a new array of objects:
$objects = array(
(object)array('name'=>'Stiven','age'=>25,'variant'=>(object)array('surname'=>'Sigal')),
(object)array('name'=>'Michael','age'=>30,'variant'=>(object)array('surname'=>'Jackson')),
(object)array('name'=>'Brad','age'=>35,'variant'=>(object)array('surname'=>'Pit')),
(object)array('name'=>'Jolie','age'=>35,'variant'=>(object)array('surname'=>'Pit')),
);
echo "<pre>";
print_r($objects);
So what I need to do is to compare an object properties (variant->surnames and ages), if two objects has a similar age and variant->surname we need to remove the one of these objects.
A half of solution is:
$tmp = array();
foreach ($objects as $item=>$object)
{
$tmp[$object->variant->surname][$object->age] = $object;
}
print_r($tmp);
Unfortunatelly I need an old-style array of objects.

I've found an example.
<?php
$a = array (
0 => array ( 'value' => 'America', ),
1 => array ( 'value' => 'England', ),
2 => array ( 'value' => 'Australia', ),
3 => array ( 'value' => 'America', ),
4 => array ( 'value' => 'England', ),
5 => array ( 'value' => 'Canada', ),
);
$tmp = array ();
foreach ($a as $row)
if (!in_array($row,$tmp)) array_push($tmp,$row);
print_r ($tmp);
?>
Quoted from here

Related

combine 2 comma separated list into one in php [duplicate]

This question already has answers here:
How would combine multiple exploded lists created by foreach into one array? [duplicate]
(4 answers)
Closed 7 months ago.
I have 2 arrays that are coming from the database, [options] column has comma-separated values.
Array
(
[0] => Array
(
[id] => 143
[menu_id] => 590
[name] => bread
[options] => small, large
)
[1] => Array
(
[id] => 144
[menu_id] => 590
[name] => jam
[options] => mango, orange, grape
)
)
Is there have any way to combine the list of [options] at [0] and [1]?
Example: small, large, mango, orange, grape
Approach
<?php foreach ($menu_options as $key => $menu_option) : ?>
<?PHP $exploded_variant_options = explode(',', $menu_option['options']);
foreach ($exploded_variant_options as $exploded_variant_option) : ?>
<?php echo ucfirst(sanitize($exploded_variant_option)); ?> //print one by one
<?php endforeach; ?>
<?php endforeach; ?>
My suggestion would be to use array_merge. The code would look like this:
<?php
$all_options = [];
foreach( $menu_options as $key => $menu_option ) {
$all_options = array_merge($all_options, explode(", ", $menu_option["options"]));
}
?>
if this is your array :
$arrays = array (
array( 'id' => 143, 'menu_id' => 590, 'name' => 'bread', 'options' => 'small,large'),
array( 'id' => 144, 'menu_id' => 590, 'name' => 'jam', 'options' => 'mango,orange,grape')
);
you can use array_map for achieving that :
function spliter($key, $value){
if($key == 'options')
return $value = explode(',', $value);
else
return $value;
}
foreach($arrays as &$array)
$array = array_map( 'spliter' , array_keys($array), $array);
then this will be a dump of your $arrays :
array(2) {
[0]=>
array(4) {
[0]=>
int(143)
[1]=>
int(590)
[2]=>
string(5) "bread"
[3]=>
array(2) {
[0]=>
string(5) "small"
[1]=>
string(5) "large"
}
}
[1]=>
&array(4) {
[0]=>
int(144)
[1]=>
int(590)
[2]=>
string(3) "jam"
[3]=>
array(3) {
[0]=>
string(5) "mango"
[1]=>
string(6) "orange"
[2]=>
string(5) "grape"
}
}
}
If you just want to print out all the options (insertion order) and not modify them further, you could also just call this:
<?php
$arr = [
['id' => 143, 'menu_id' => 590, 'name' => 'bread', 'options' => 'small, large'],
['id' => 144, 'menu_id' => 590, 'name' => 'jam', 'options' => 'mango, orange, grape']
];
var_dump(array_reduce(array_column($arr, 'options'), function ($c, $i) {
return $c ? "${c}, ${i}" : $i;
}));
// prints: string(34) "small, large, mango, orange, grape"
?>
Otherwise, I suggest using the approach shown by "Teodor".

Move array in array to parent array php

Is there a specific function to move array which is in array to the parent array as key or value.
array(5) { [0]=> array(1) { [0]=> string(2) "id" } [1]=> array(1) { [0]=> string(7)
"buydate" } [2]=> array(1) { [0]=> string(6) "expire" } [3]=> array(1) { [0]=> string(6)
"planid" } [4]=> array(1) { [0]=> string(5) "buyer" } }
Result I would like to get is:
array() { [0] => 'id', [1] => 'buydate' etc. }
Or
array('id', 'buydate' etc.. )
Is it possible to achieve without foreach ?
array_map() is extremely powerful and should do the trick:
$array = ... ; // your initial array
$flattened_array = array_map(function($item) {
return $item[0];
}, $array);
If you want to flatten the desired array, and use foreach you can do it this way.
Consider this example:
// Sample data:
$values = array(
0 => array(
0 => 'id',
),
1 => array(
0 => 'buydate',
),
2 => array(
0 => 'expire',
),
3 => array(
0 => 'planid',
),
4 => array(
0 => 'buyer',
),
);
$new_values = array();
foreach($values as $key => $value) {
$new_values[] = $value[0];
}
print_r($new_values);
Sample Output:
Array
(
[0] => id
[1] => buydate
[2] => expire
[3] => planid
[4] => buyer
)
Or alternatively, you can you the iterator. Consider this example:
$new_values = array();
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($values));
foreach($iterator as $value) {
$new_values[] = $value;
}
It should gave you the same output.

PHP recursive array from JSON to key-value to values

I want to apologize in advanced if this was already asked, I'm not exactly sure what this would be called.
I'm storing data from a form into a MongoDB database and I'd like to create defined key-value pairs to make sorting easier.
Using this code I am able to do this with a one-dimensional array, but it does not work with multidimensional arrays:
/* $array = The array */
$new_array = array();
foreach ($array as $key => $value) {
array_push($new_array, array(
'name' => $key,
'value' => $value
));
}
Example:
Input array:
Array
(
[email] => test#mail.com
[name] => John
[sports] => Array
(
[outdoor] => Array
(
[0] => Football
[1] => Baseball
)
[indoor] => Array
(
[0] => Basketball
[1] => Hockey
)
)
)
Output array:
Array
(
[0] => Array
(
[name] => email
[value] => test#mail.com
)
[1] => Array
(
[name] => name
[value] => John
)
[2] => Array
(
[name] => sports
[value] => Array
(
[outdoor] => Array
(
[0] => Football
[1] => Baseball
)
[indoor] => Array
(
[0] => Basketball
[1] => Hockey
)
)
)
)
Notice how it stops at the sports value array and does not change the array within it. How can I continue this pattern throughout all the arrays within it?
You can use recursion:
function keyValue($array){
$new_array = array();
foreach ($array as $key => $value) {
array_push($new_array, array(
'name' => $key,
'value' => is_array($value) ? keyValue($value) : $value
));
}
return $new_array;
}
Try this on for size:
$array = array(
'email' => 'test#email.com',
'name' => 'John',
'sport' => array('Soccor', 'Hockey')
);
$func = function($value, $key) {
$return = array();
$return['name'] = $key;
$return['value'] = $value;
return $return;
};
$parsed = array_map($func, $array, array_keys($array));
For me, this returned:
array(3) {
[0]=>
array(2) {
["name"]=>
string(5) "email"
["value"]=>
string(14) "test#email.com"
}
[1]=>
array(2) {
["name"]=>
string(4) "name"
["value"]=>
string(4) "John"
}
[2]=>
array(2) {
["name"]=>
string(5) "sport"
["value"]=>
array(2) {
["outdoor"]=>
array(2) {
[0]=>
string(8) "Football"
[1]=>
string(8) "Baseball"
}
["indoor"]=>
array(2) {
[0]=>
string(10) "Basketball"
[1]=>
string(6) "Hockey"
}
}
}
}
It is pretty simple to turn associative arrays to JSON objects (StdClass) and vice versa, preserving native data types (int, float, bool). Here's my attempt:
To Key-Value Array
function toKeyValue($values)
{
$result = [];
foreach ($values as $key => $value)
{
if (is_array($value)) {
$result[$key] = toKeyValue($value);
} elseif (is_object($value)) {
$result[$key] = toKeyValue((array) $value);
} else {
$result[$key] = $value;
}
}
return $result;
}
To JSON Object
function toJson($values)
{
return json_decode(json_encode($values));
}
$values should always be an array.

Recreate Multidimensional Array in PHP

This is pretty basic, but my question is:
Given an array:
$a = array(
0 => array('Rate'=> array('type_id'=>1, 'name' => 'Rate_1', 'type'=>'day','value'=>10)),
1 => array('Rate'=> array('type_id'=>1, 'name' => 'Rate_2', 'type'=>'night','value'=>8)),
2 => array('Rate'=> array('type_id'=>2, 'name' => 'Rate_3', 'type'=>'day','value'=>7)),
3 => array('Rate'=> array('type_id'=>2, 'name' => 'Rate_4', 'type'=>'nigh','value'=>16)),
4 => array('Rate'=> array('type_id'=>3, 'name' => 'Rate_5', 'type'=>'day','value'=>10))
);
What is the most efficient way to change it so we have something like:
$new_array = array(
[type_id] => array(
[type] => array(
[value]
)
)
)
);
In other words, I would like to strip some data (the name, which I don't need) and reorganise the dimensions of the array. In the end I would have an array which I would be able to access the values by $new_array['type_id']['type']['value'].
Not entirely sure if this is exactly what you want, but with this you can access the values by saying
echo $new[TYPE_ID][DAY_OR_NIGHT];
$new = array();
foreach($a AS $b){
$c = $b['Rate'];
$new[$c['type_id']][$c['type']] = $c['value'];
}
Using print_r on $new would give you:
Array
(
[1] => Array
(
[day] => 10
[night] => 8
)
[2] => Array
(
[day] => 7
[night] => 16
)
[3] => Array
(
[day] => 10
)
)
Since php 5.3.0, array_reduce() allows using an array as the initial value, given your initial array $a, you can use the following code
function my_reducer ($result, $item) {
$result[$item['Rate']['type_id']][$item['Rate']['type']] = $item['Rate']['value'];
return $result;
}
$assoc_arr = array_reduce($a, 'my_reducer', array());
var_dump($assoc_arr);
This returns
array(3) { [1]=> array(2) {
["day"]=>
int(10)
["night"]=>
int(8) } [2]=> array(2) {
["day"]=>
int(7)
["nigh"]=>
int(16) } [3]=> array(1) {
["day"]=>
int(10) } }

multi-dimensional array, move into a simple array

I have an array that looks like this,
[0] => Array
(
[youtube_showreel_url_1] => youtube1.com
[youtube_showreel_description] => youtube1.com - desc
)
[1] => Array
(
[youtube_showreel_url_2] => youtube2.com
[youtube_showreel_description] => youtub2.com - desc
)
[2] => Array
(
[youtube_showreel_url_3] => youtube3.com
[youtube_showreel_description] => youtube3.com - desc
)
[3] => Array
(
[youtube_showreel_url_4] => youtube4.com
[youtube_showreel_description] => youtube4.com - desc
)
[4] => Array
(
[youtube_showreel_url_5] => youtube5.com
[youtube_showreel_description] => youtube5.com - desc
)
Is it possible with PHP to turn it into something that looks like this?
[0] => Array (
[youtube_showreel_url_1] => youtube1.com
[youtube_showreel_description] => youtube1.com - desc
[youtube_showreel_url_2] => youtube2.com
[youtube_showreel_description] => youtub2.com - desc
[youtube_showreel_url_3] => youtube3.com
[youtube_showreel_description] => youtube3.com - desc
[youtube_showreel_url_4] => youtube4.com
[youtube_showreel_description] => youtube4.com - desc
[youtube_showreel_url_5] => youtube5.com
[youtube_showreel_description] => youtube5.com - desc
)
Can explode it or run it through a loop or something?
Assuming your original data is held in a variable called $input:
// This will hold the result
$result = array();
foreach ($input as $index => $item) { // Loop outer array
foreach ($item as $key => $val) { // Loop inner items
$result[$key] = $val; // Create entry in $result
}
}
// Show the result
print_r($result);
However, your input has the same key appearing in it more than once, and the later values will overwrite the first one. So you might want to do something like this:
foreach ($input as $index => $item) { // Loop outer array
foreach ($item as $key => $val) { // Loop inner items
$result[$key.$index] = $val; // Create entry in $result
}
}
<?php
$aNonFlat = array(
1,
2,
array(
3,
4,
5,
array(
6,
7
),
8,
9,
),
10,
11
);
$objTmp = (object) array('aFlat' => array());
array_walk_recursive($aNonFlat, create_function('&$v, $k, &$t', '$t->aFlat[] = $v;'), $objTmp);
var_dump($objTmp->aFlat);
/*
array(11) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
int(5)
[5]=>
int(6)
[6]=>
int(7)
[7]=>
int(8)
[8]=>
int(9)
[9]=>
int(10)
[10]=>
int(11)
}
*/
?>
source : http://ca.php.net/manual/fr/function.array-values.php#86784
array keys have to have unique names meaning that you wouldn't be able to have multiple youtube_showreel_description keys or else you just overwrite the value. You could rename the key to something like youtube_showreel_description_NN where NN is the number of the description similar to how you have the url.
there would be a collision on "youtube_showreel_description"
you should think of a better data structure for that
I would suggest you retain its multidimensional array structure
Array
(
[0] => Array
(
[url] => youtube1.com
[description] => youtube1.com - desc
)
[1] => Array
(
[url] => youtube2.com
[description] => youtube2.com - desc
)
[2] => Array
(
[url] => youtube3.com
[description] => youtube3.com - desc
)
[3] => Array
(
[url] => youtube1.com
[description] => youtube3.com - desc
)
[4] => Array
(
[url] => youtube1.com
[description] => youtube4.com - desc
)
)
If you like it that way, I think its cleaner, and easier to parse
Not exactly, the [youtube_showreel_description] would overwrite itself after each declaration, you would need to use a unique identifier for each key. If the keys are not important you can just loop through your existing array with a foreach loop and set a regular iterative key and "know" that the array comes in sets where the first element is the url and the second is the description.
Edit: If the keys aren't important, you could use the url as your key and description as your value, this will make it easier to iterate over as each element pertains to only one item.
As everyone has already mentioned, you're going to get a collision on 'youtube_showreel_description'. How about something like this?:
// values (stays the same - no need to reformat)
$values = array(
array(
'youtube_showreel_url_1' => 'youtube1.com',
'youtube_showreel_description' => 'youtube1.com desc'
),
array(
'youtube_showreel_url_2' => 'youtube2.com',
'youtube_showreel_description' => 'youtube2.com desc'
),
array(
'youtube_showreel_url_3' => 'youtube3.com',
'youtube_showreel_description' => 'youtube3.com desc'
),
array(
'youtube_showreel_url_4' => 'youtube4.com',
'youtube_showreel_description' => 'youtube4.com desc'
)
);
// the good stuff
$result = array_map(function($v) {
return array(
'url' => array_shift($v),
'description' => $v['youtube_showreel_description']
);
}, $values);
// result
array(4) {
[0]=>
array(2) {
["url"] => string(12) "youtube1.com"
["description"] => string(17) "youtube1.com desc"
}
[1]=>
array(2) {
["url"] => string(12) "youtube2.com"
["description"] => string(17) "youtube2.com desc"
}
[2]=>
array(2) {
["url"] => string(12) "youtube3.com"
["description"] => string(17) "youtube3.com desc"
}
[3]=>
array(2) {
["url"] => string(12) "youtube4.com"
["description"] => string(17) "youtube4.com desc"
}
}

Categories