PHP Array remove items if missing key - php

I have the following array format:
Array
(
[0] => stdClass Object
(
[tid] => 3
[children] => Array ()
)
[1] => stdClass Object
(
[tid] => 15
)
[2] => stdClass Object
(
[tid] => 12
[children] => Array ()
)
[3] => stdClass Object
(
[tid] => 9
[children] => Array ()
)
)
I would like to remove items that do not have [children] and am having some difficulty doing so.
Help is appreciated, thank you.

Try this, where $array is the array you want to process
foreach($array as $key => $value)
{
if(!isset($value['children'])
unset($array[$key]);
}
This will remove all items in your array where children is not set or is null. Since in your example you set children to empty arrays, you will need to use isset() and not empty().

Can you give this a shot and let me know what happens?
$myArray = array_filter($myArray, "hasChildren");
function hasChildren($node) {
if (isset($node->children)) return true;
return false;
}

Related

How to merge three arrays according to common key in php

I have three arrays first array include ids and employees name and second array have monthly collection with employee ids and third array have daily collection with employee id and daily collection I want to merge these array with ids and name and dcollection and monthly collection but the desired output is not coming here my first array $ids is
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Rohit
)
[1] => stdClass Object
(
[id] => 2
[name] => Emop1
)
[2] => stdClass Object
(
[id] => 3
[name] => Pankaj
)
[3] => stdClass Object
(
[id] => 4
[name] => tejpal singh
)
)
second array $q1 is
Array
(
[0] => stdClass Object
(
[name] => Rohit
[id] => 1
[mcollecton] => 100
)
[1] => stdClass Object
(
[name] => Emop1
[id] => 2
[mcollecton] => 1222
)
)
third array $q2 is
Array
(
[0] => stdClass Object
(
[name] => Rohit
[id] => 1
[dcollecton] => 300
)
[1] => stdClass Object
(
[name] => Emop1
[id] => 2
[dcollecton] => 150
)
)
so far what I have tried
$new_array = array();
foreach($ids as $k) {
$q1n = array("id"=>$k->id,"name"=>$k->name);
foreach($q1 as $k1) {
if($k->id==$k1->id){
$mc = array("mc"=>$k1->mcollecton);
array_merge($q1n,$mc);
}
}
foreach($q2 as $k1){
if($k->id==$k1->id){
$dc = array("dc"=>$k1->dcollecton);
array_merge($q1n,$dc);
}
}
$a = array_merge($q1n,$mc);
$av = array_merge($q1n,$dc);
array_push($new_array,$q1n);
}
but the output is coming as
Array
(
[0] => Array
(
[id] => 1
[name] => Rohit
)
[1] => Array
(
[id] => 2
[name] => Emop1
)
[2] => Array
(
[id] => 3
[name] => Pankaj
)
[3] => Array
(
[id] => 4
[name] => tejpal singh
)
)
I want the output be like
Array
(
[0] => Array
(
[id] => 1
[name] => Rohit
[mcollection] => 100
[dcollection] => 300
)
[1] => Array
(
[id] => 2
[name] => Emop1
[mcollection] => 1222
[dcollection] => 150
)
[2] => Array
(
[id] => 3
[name] => Pankaj
[mcollection] => 0
[dcollection] => 0
)
[3] => Array
(
[id] => 4
[name] => tejpal singh
[mcollection] => 0
[dcollection] => 0
)
)
So I have tried many times but the desired output is not coming . please help me out how to get the desired output.
It seemed like that answer could be modified, or put in a function that you could call multiple times if needed to combine more than two arrays.
There's probably cleaner ways to handle this with array functions like array_merge or array_walk, but this is the general idea of how I might approach it. I haven't tested this, but maybe it's useful.
foreach($first as $key1 => $value){
foreach($second as $key2 => $value2){
// match the ids and check if array key exists on first array
if($value['id'] === $value2['id'] && empty($first[$key2])){
$first[$key][$key2] = $value2;
}
}
}
EDIT: Based on the answer you posted vs the question you asked, are you incrementing the collection numbers or just setting them? In other words why use +=? You should also be able to remove array_merge and array_push.
Below is geared more towards what you're trying to do. I haven't tested this either, but if you run into errors, post your code with the errors returned so that it's easier to debug:
foreach($ids as $k)
{
$thisArray = $newArray[] = array("id"=>$k->id,"name"=>$k->name);
foreach($q1 as $k1)
{
if($k->id == $k1->id && !empty($k1->mcollecton))
{
$thisArray['mc'] = $k1->mcollecton;
}
}
foreach($q2 as $k2)
{
if($k->id == $k2->id && !empty($k2->dcollecton))
{
$thisArray['dc'] = $k2->dcollecton;
}
}
}
// This should have both new collections fields on all array items
print_r($newArray)

How to Echo Multidimensional Comparison Array in PHP

I have two array:
$one = Array
(
[0] => stdClass Object
(
[id] => 2
[name] => Southampton
)
[1] => stdClass Object
(
[id] => 4
[name] => Manchester United F.C
)
)
and
$two = Array
(
[0] => stdClass Object
(
[number] => 25555
[slice_1] => 4
[slice_2] => 4
[slice_3] => 2
[slice_4] => 4
[status] => Published
)
)
I want to output, if array $two->slice_1 same as array $one->id than output $one->name.
For example:
$two[0]->slice_1 (4) compare to $one[0]->id (4) will result in Manchester United F.C.
Because the array $one and $two will be have more than one array. Please don't answer this:
if($two[0]->slice_1 == $one[1]->id){echo $one[0]->name;}
I'm stucking here and can't think out some way. Please help. Thanks in advance
avoiding loops inside loops first create temp reference array then loop though second array outputting the result
foreach($one as $id=>$data){
$temp[$data['id']] = $data['name'];
}
foreach($two as $secondary_id=>$secondary_data){
echo isset($temp[$secondary_data['slice_1']]) ? $temp[$secondary_data['slice_1']] : '';
}

PHP - Merge 2 arrays of object using a key/id

I want to merge the 2 arrays of objects based on the 'id' field of Array1 and the 'itemVendorCode' of Array2. I also wanted to remove from the resulting arrays of object anything that didn't match.
Array1:
Array
(
[0] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
)
[1] => stdClass Object
(
[id] => 89-575-2354
[qty] => 24
[price] => 230.35
)
[2] => stdClass Object
(
[id] => 89-605-1250
[qty] => 2
[price] => 230.35
)
)
Array2:
Array
(
[0] => Item Object
(
[internalId] => 14062
[itemVendorCode] => 89-605-1250
)
[1] => Item Object
(
[internalId] => 33806
[itemVendorCode] => 89-575-2354
)
[2] => Item Object
(
[internalId] => 64126
[itemVendorCode] => 26-295-1006
)
)
I was able to solve this by this code:
$indexed = array();
foreach($itemsArray as $value) {
$indexed[$value->itemVendorCode] = $value;
}
$results = array();
foreach($vendorItems as $obj) {
$value = $indexed[$obj->id];
if (isset($value)) {
foreach($value as $name => $val) {
$obj->$name = $val;
array_push($results, $obj);
}
}
}
print_r($results);
credits to the original poster. I just modified it a bit,
I was able to get the result like this:
Array
(
[0] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
[internalId] => 2035
[itemVendorCode] => 10-423-1176
)
[1] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
[internalId] => 2035
[itemVendorCode] => 10-423-1176
)
[2] => stdClass Object
(
[id] => 14-102-1010
[qty] => 16
[price] => 3.2
[internalId] => 57033
[itemVendorCode] => 14-102-1010
)
)
I think you will have to use array_map function which provides you a callback function to execute on array(s).
In the callback function:
- declare your array1
- foreach the second array
- set an if statement to check that the current iteration with the id value matches the itemVendorCode of the array2 and return it
something like this:
// You have to specify to PHP to use a local copy of your $array2 to works with it into your callback
$cb = function ($obj1) use ($array2)
{
// you foreach this array
foreach ($array2 as $obj2) {
// if the value of id matches itemVendorCode
if ($obj1->id === $obj2->itemVendorCode) {
// you return the id
return $obj->id;
}
}
};
// this function will fill a new array with all returned data
$mergedArray = array_map($cb, $array1);
This code is a sample but doesn't provide you, your needled solution, try to update it to do what you exactly want ;)

add new index using array map function in php without using looping function

this is my array
Array
(
[0] => Array
(
[id] => 277558
[text_value] => Jif
[response_count] => 13
[response_percentage] => 92
)
[1] => Array
(
[id] => 277559
[text_value] => Peter Pan
[response_count] => 20
[response_percentage] => 6
)
)
after completing the operation the out put should be
Array
(
[0] => Array
(
[id] => 277558
[text_value] => Jif
[response_count] => 13
[response_percentage] => 92
[encode_param]=>ds!##^(*!ggsfh8236542jsdgf82*&61327
)
[1] => Array
(
[id] => 277559
[text_value] => Peter Pan
[response_count] => 20
[response_percentage] => 6
[encode_param]=>ds!##^(*!ggsfh8236542jsdgf82*&61327
)
)
you can see a new array value encode_paramis added
in that function do some encode algorithms
i have achieve this in the foreach looping statement
but i need to do it in array maping
Can anybody help thank u in advance
$encode_func = function($elem) { // declare function to encode
return $elem['text_value'];
}
$result = array_map(function($elem) use($encode_func) {
$elem['encode_param'] = $encode_func($elem);
return $elem;
}, $array);
Hope it helps.

How can i sort an array of objects (descending) but explicitly declare the first element of that array?

say i have an array of objects:
Array (
[0] => stdClass Object (
[id] => 1
[name] => product_1
[cost] =>9.99
)
[1] => stdClass Object (
[id] => 2
[name] => product_2
[cost] =>2.99
)
[2] => stdClass Object (
[id] => 3
[name] => product_3
[cost] =>4.99
)
[3] => stdClass Object (
[id] => 4
[name] => product_4
[cost] =>1.99
)
[4] => stdClass Object (
[id] => 5
[name] => product_5
[cost] =>0.99
)
)
I want to order them starting from the lowest cost to the highest however the first element in the array has to have the [name] of "product_3".
To do this, you can't rely on sorting alone. You'd need a bit more logic:
Capture the soon-to-be first element in its own variable.
Remove that element from the array.
Sort the array using usort().
Shift the element from #1 to the front of the array with array_unshift().
So, for #1, you can do a bunch of things. The simplest is to loop the array to find the key that indexes where the first object is, and unset() it:
$first = null;
foreach( $array as $key => $obj) {
if( $obj->name == 'product_3') {
$first = $obj;
unset( $array[ $key ]);
break;
}
}
Now you have the first element in $first, so you have to sort the array with usort():
usort( $array, function( $a, $b) {
if( $a->cost == $b->cost)
return 0;
return $a->cost < $b->cost ? 1 : -1; // Might need to switch 1 and -1
});
Finally, add the first element back to the beginning of the now-sorted array:
array_unshift( $array, $first);
Disclaimer: None of the above implementation was tested.

Categories