Remove item from multidimensional array php - php

Here is the array:
[cart] => Array
(
[ProductId] => Array
(
[0] => P121100001
[1] => P121100002
)
[SellerId] => Array
(
[0] => S12110001
[1] => S12110001
)
[SpecifyId] => Array
(
[0] => 1
[1] => 2
)
[Quantity] => Array
(
[0] => 1
[1] => 1
)
[Price] => Array
(
[0] => 12
[1] => 29
)
[TotalPrice] => 41
)
I have the ProductId and I want to remove all the other items matching P121100002's key.
Is there an easy way to do this I can't can seem to come up with one?

You can loop through the full array and use unset() to, well, "unset" the specified index:
$index = array_search($cart['ProductId'], 'P121100002');
if ($index !== false) {
foreach ($cart as $key => $arr) {
unset($cart[$key][$index]);
}
}
The slight caveat to this approach is that it may disrupt your index orders. For instance, say you have:
[ProductId] => Array (
[0] => P121100001
[1] => P121100002
[2] => P121100003
)
And you want to remove P121100002, which has a corresponding index of 1. Using unset($cart['ProductId'][1]) will cause your array's to become:
[ProductId] => Array (
[0] => P121100001
[2] => P121100003
)
This may be something to remain concerned with if you're going to use a for loop to iterate through in the future. If it is, you can use array_values() to "reset" the indexes in the unset() loop from above:
foreach ($cart as $key => $arr) {
unset($cart[$key][$index]);
$cart[$key] = array_values($cart[$key]);
}

foreach($yourArray['ProductId'] as $key => $value) {
if ($value == $productIdToRemove) {
foreach($yourArray as $deleteKey => $deleteValue) {
unset($yourArray[$deleteKey][$key]);
}
break;
}
}

Use array_key_exists along with the unset() function

Related

Looping through 2 arrays and retrieve the values from the first array if the id matches with the second array. (PHP)

I've been trying to figure out how I would be able to loop through two arrays and match values from each array which should only return the value that matches of the first array.
Array 1:
[0] => Array
(
[contact] => 68
[field] => 11
[value] => DBSA
[cdate] => 2019-11-14T11:21:08-06:00
[udate] => 2021-03-30T07:54:00-05:00
)
[1] => Array
(
[contact] => 68
[field] => 131
[value] => ABC
[cdate] => 2019-11-22T08:34:03-06:00
[udate] => 2021-03-30T07:54:00-05:00
)
Array 2:
[0] => Array
(
[source] => analysis_utm_source
[id] => 131
)
[1] => Array
(
[source] => analysis_utm_medium
[destination] => UTM medium
[id] => 132
)
So in this example I would like to retrieve the values from the first array but only where id/field = 131
I've tried to work with 'array_intersect' but this doesn't seem to give the right output.
I'd appreciate it if someone would be able to push me in the right direction.
EDIT: I've been able to solve it by using array_filter, final code like so;
foreach ($fieldValuesDB as $arr) {
$options[] = $arr['id'];
}
$result = array_filter($fieldValuesAC, function($v) use ($options) {
return in_array($v['field'], $options);
});
foreach($array2 as $k => $v)
{
$key = array_search($v['id'], array_column($array1, 'field'));
if($key){
echo print_r($array1[$key]);
}
}
you will get the index of the element in your first array stored in $key variable

PHP Loop through array with no index names

In PHP I have this structure of Array (some are empty, some not, some are multiple items):
Array
(
[0] => Array
(
)
[1] => Array
(
[0] => 16534
)
[2] => Array
(
)
[3] => Array
(
[0] => 16532
[1] => 16533
)
[4] => Array
(
)
[5] => Array
(
[0] => 14869
)
}
I want to loop through this array, so as the result I get only the numbers (all of them).
I tried it this way:
foreach ($myarray as $item) {
echo '<pre>' . print_r($item) . '</pre>';
// $result[] = $this->myMethod($item);
}
So in foreach I want to use all the items from array in my method.
However when I echo the $item in the loop, I have something like this:
Array ( )
Array ( [0] => 16534 )
Array ( )
Array ( [0] => 16532 [1] => 16533 )
Array ( )
Array ( [0] => 14869 )
So still arrays (also the empty ones), and not numbers.
Can you please help with this?
UPDATE: I just noticed, some of the arrays looks like this:
[6] => Array
(
[0] => Array
(
[id] => 269
[hours] => 21.0
)
)
[7] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 2
[hours] => 12.0
)
[1] => Array
(
[id] => 7
[hours] => 24.0
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[hours] => 5.0
)
[1] => Array
(
[id] => 7
[hours] => 0.583
)
)
)
but here the solution works not.
This one seems working but it is now brutal foreach in foreach solution:
foreach ($myarray as $item2) {
foreach ($item2 as $key2 => $val2) {
if (isset($val2)) {
foreach ($val2 as $key4 => $val4) {
echo $val4['id'].',';
}
}
}
}
Just merge the array which will also remove the empties:
foreach(array_merge(...$myarray) as $item) {
// echo '<pre>' . print_r($item) . '</pre>';
$result[] = $this->myMethod($item);
}
This will also work and may be faster:
$result = array_map([$this, 'myMethod'], array_merge(...$myarray));
If you have an old PHP version you'll have to use array() instead of [] and:
call_user_func_array('array_merge', $myarray)
You are only looping through the main array.That's the reason why you are getting an array when you are printing the result set.(because those values are stored in sub arrays and you are not looping them.)
And to remove the sub arrays with empty values I'll use isset() so that you will get only the values.
Change your code into this.
foreach ($myarray as $item) {
foreach($item as $key=>$val){
if(isset($val){
$values[] = $val;
// $result[] = $this->myMethod($values);
}
}
}

PHP foreach stuck at values in first key

I have a multidimensionel array of different departments, and a want to loop through it using a foreach loop, but for some reason, the foreach loop grabs the values under the first key through every iteration.
The array looks like this:
$departmentArray =
Array
(
[0] => Array
(
[dpt_id] => 5
[dpt_name] => Administration
[dpt_employees] => Array
(
[0] => Array
(
[started] => 2000-06-01
[stopped] => 9999-99-99
[empl_id] => 21
)
[1] => Array
(
[started] => 2000-06-01
[stopped] => 2010-01-01
[empl_id] => 23
)
)
)
[1] => Array
(
[dpt_id] => 6
[dpt_name] => Warehouse
[dpt_employees] => Array
(
[0] => Array
(
[started] => 2000-10-01
[stopped] => 2012-01-01
[empl_id] => 30
)
[1] => Array
(
[started] => 2007-10-17
[stopped] => 9999-99-99
[empl_id] => 197
)
)
)
)
And the foreach loop looks like this:
foreach($departmentArray as $key => $value) {
print_r($key);
print_r($value['dpt_name']);
}
And this prints:
0 Administration 1 Administration.
Does anyone know, why the loop does not move forward in the array and grab the value (Warehouse) under key/index 1 during its second iteration?
Total stab into the dark:
You have used $value in a foreach loop before as reference, like so:
foreach ($foo as &$value) { ... }
foreach ($departmentArray as $key => $value) { ... }
This is a well known side-effect of references. unset($value) after the first loop.

Replace array keys and values

For example I have this array:
Array (
[0] => Array (
[id] => 45 [name] => Name1 [message] => Ololo [date_create] => 21:03:56 )
[1] => Array (
[id] => 46 [name] => visitor [message] => Hi! [date_create] => 21:06:28 )
)
I need converting to:
Array (
[id] => Array (
[0] => 45, [1] => 46
)
[name] => Array (
[0] => Name1, [1] => visitor
)
[message] => Array (
[0] => Ololo, [1] => Hi!
)
[date_create] => Array (
[0] => 21:03:56, [1] => 21:06:28
)
)
I like to know a function for converting this,
Try this block of code:
// Assuming the array you have is called $mainArray.
// The output will be $outputArray.
$outputArray = array();
foreach ($mainArray as $index => $array) { // Iterate through all the arrays inside the main array.
// foreach ($mainArray as $array) { // Use this if the numeric index order doesn't matter.
foreach ($array as $key => $value) { // Iterate through each inner array.
// Load the multidimensional array with the first key as one of (id, name, message, date_create) and second key as the numeric index (if you need it).
$outputArray[$key][$index] = $value;
// $outputArray[$key][] = $value; // Use this if the numeric index order doesn't matter.
}
}
print_r($outputArray);

PHP resort keys from multidimensional array

i have a multidimensional array whose index/keys (not the values) are like this:
this is how the submitted array looks
[param] => Array
(
[3] => groupedlista
[0] => groupedlistb
[2] => groupedlistc
)
[f_name] => Array
(
[3] => grouplistaa
[0] => grouplistbb
[2] => grouplistcc
)
[f_label] => Array
(
[3] => grouplistL3
[0] => grouplistL0
[2] => grouplistL2
)
this is how the order looks
0,2,3
i want that Result
[param] => Array
(
[0] => groupedlistb
[1] => groupedlistc
[2] => groupedlista
)
[f_name] => Array
(
[0] => grouplistbb
[1] => grouplistcc
[2] => grouplistaa
)
[f_label] => Array
(
[0] => grouplistL0
[1] => grouplistL2
[2] => grouplistL3
)
that's it
PS: i use a jquery sort / add / delete feature in the form and i prefer to do the final sorting php-based. the index array [$i] is required to be declared at the form.
$order = '0,2,3';
$out = array(); // This will hold the sorted values
$order = explode(',',$order); // Turn the order into an array
foreach ($multiDimArray as $key => $subArray) { // Loop outer array
foreach ($order as $pos) { // Loop order array
if (isset($subArray[$pos])) { // Make sure the key exists
$out[$key][] = $subArray[$pos]; // Put the correct value in the correct place
}
}
}
print_r($out);

Categories