Remove Line in a WP array - php

I've an Array (wordpress) and would like to remove or add some parts. I don't know if some of my needs are possible and how to do it.
These are my needs: Remove [LineRoot] but keep all [line] (just move to parent in order to have all [Line] at the same level than [LineRoot] )
Array
(
[0] => Array
(
[Order] => 679
[LivraisonPrenom] => Joe
[LineRoot] => Array
(
[Line] => Array
(
[0] => Array
(
[ll] => hh
[Id] => 20
[SKU] => A104
[Quantity] => 1
)
)
[Line] => Array
(
[0] => Array
(
[ll] => hh
[Id] => 22
[SKU] => A105
[Quantity] => 1
)
)
)
[Meta-LangueBL] =>
[CheckoutAddOns] => Array
(
[CheckoutAddOn] => Array
(
[0] => Array
(
[ID] => 2
[Name] => Livraison
[Cost] => -48.33
)
)
)
)
)

Here is a list of PHP functions that can help you
unset
array_unshift
array_merge
array_splice
Good luck :)

Related

Compare two multidimensional key values & combine non duplicates

I have two multidimensional arrays. I'm looping through both arrays, checking for certain values & creating a new array.
$full_cats array
Array
(
[0] => Array
(
[parent_cats] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[1] => Array
(
[parent_cats] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
)
)
$sub_cats array
Array
(
[0] => Array
(
[sub_cats] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
)
[1] => Array
(
[sub_cats] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
[2] => Array
(
[sub_cats] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
I'm looping through both arrays to check if $sub_cat['sub_cats']['parent'] value is the same as the $full_cat['parent_cats']['id'] value. If this is true, both values are added to the $master_cats array.
$master_cats = array();
foreach ($sub_cats as $sub_cat) {
foreach ($full_cats as $full_cat) {
if( $sub_cat['sub_cats']['parent'] == $full_cat['parent_cats']['id'] ){
$master_cats[] = array(
"parent_cats" => array(
$full_cat['parent_cats'],
),
"sub_cats" => array(
$sub_cat['sub_cats'],
)
);
};
};
};
The $master_cats output -
Array
(
[0] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
)
[2] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
)
I'm having two issues with the $master_cats array.
1st problem - As you can see, index 0 & 1 have the same [parent_cats] values. I only want to add the [parent_cats] key/values if they dont already exist.
2nd Problem - the $master_cats array index 0 & 1, the sub_cats array, some values are different but both have the same [sub_cats][parent] => 384 so they belong in the same array index, eg 0.
Below is what I'm hoping to achieve with the $master_cats array from the foreach/loop above
Array
(
[0] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384 <-- Parent ID
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384 <-- belongs to [parent_cats][id]
)
[1] => array
(
[id] => 385
[name] => Beers
[parent] => 384 <-- belongs to [parent_cats][id]
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 387 <-- Parent ID
[name] => Wines
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387 <-- belongs to [parent_cats][id]
)
)
)
)
Here's one way to go about it. Note: your array structures are a bit over-nested... but here's a solution for the given structure
$all = [];
$tmpsubs = [];
foreach ($full_cats as $parent) {
$tmp = ['parent_cats' => $parent['parent_cats'], 'sub_cats' => []];
foreach ($sub_cats as $sub) {
if ($sub['sub_cats']['parent'] == $parent['parent_cats']['id']) {
$tmp['sub_cats'][]=$sub['sub_cats'];
}
}
$all[] = $tmp;
}
print_r($all);
Example: https://3v4l.org/01oLF
Output:
Array
(
[0] => Array
(
[parent_cats] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
[1] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
)
Note: here's an example of simplifying your array structure (which would require changing the code in my answer, but ultimately might make your life a little easier)
$parent_cats= array(
array(
"id" => "384",
"name" => "Beers & Ales",
"parent" => "0"
),
array(
"id" => "387",
"name" => "Wines",
"parent" => "0"
));

PHP: Add a dimension to an array using array_map?

I have an array and I want to add a dimension. I want to group it using the position column in the array
$view_rows = [];
Array
(
[row] => Array
(
[0] => Array
(
[position] => 1
[image_id] => 2809
[basename] => rj5ed2c90f609423.26673093.jpg
)
[1] => Array
(
[position] => 1
[image_id] => 2808
[basename] => rj5ed2c8eccc9c06.91700011.jpg
)
[2] => Array
(
[position] => 2
[image_id] => 2807
[basename] => rj5ed2c77ef32b76.96579137.jpg
)
)
)
After adding the dimension (group by location), it should look like this:
Array
(
[rows] => Array
(
[1] => Array
(
[0] => Array
(
[position] => 1
[image_id] => 2809
[basename] => rj5ed2c90f609423.26673093.jpg
)
[1] => Array
(
[position] => 1
[image_id] => 2808
[basename] => rj5ed2c8eccc9c06.91700011.jpg
)
)
[2] => Array
(
[0] => Array
(
[position] => 2
[image_id] => 2807
[basename] => rj5ed2c77ef32b76.96579137.jpg
)
)
)
)
Is there a PHP function to do this? If not, I will use the good old looping method.
I did some research here ( Add Dimension in PHP array ), and I believe it can be achieve using array_map.
I've tried it, with no success (I'm close)
$new_array = array_map(function($x) {
return [$x[0]['position'] => $x];
}, $view_rows);
Since you need to change the structure of the array, you can't use array_map, but you can use array_reduce to get the result you want:
$data['rows'] = array_reduce($data['rows'], function ($c, $a) {
$c[$a['position']][] = $a;
return $c;
}, array());
Output:
Array
(
[rows] => Array
(
[1] => Array
(
[0] => Array
(
[position] => 1
[image_id] => 2809
[basename] => rj5ed2c90f609423.26673093.jpg
)
[1] => Array
(
[position] => 1
[image_id] => 2808
[basename] => rj5ed2c8eccc9c06.91700011.jpg
)
)
[2] => Array
(
[0] => Array
(
[position] => 2
[image_id] => 2807
[basename] => rj5ed2c77ef32b76.96579137.jpg
)
)
)
)
Demo on 3v4l.org
#Nick answer works but the same thing can be achieved using a simple foreach loop easily so everyone can understand it.
$result = [];
foreach($data['rows'] as $row) {
$result[$row['position']][] = $row;
}

PHP: move values of array under others keys

I have this array:
Array
(
[0] => Array
(
[id] => 1
[amount_positive] => 10.00
[negative_sum] => -5,7
[negative] => Array
(
[0] => Array
(
[amount] => -3.00
)
[1] => Array
(
[amount] => -2.00
)
[2] => Array
(
[amount] => -0.70
)
)
)
[1] => Array
(
[id] => 13
[amount_positive] => 6.00
[negative_sum] => -7
[negative] => Array
(
[0] => Array
(
[amount] => -7
)
)
)
)
You can note that key 0 has +10.00 of positive and -5.7 of negative (they are money transactions).
Key 1 has +13 and -7.
Basically, I need to iterate into array and move 4.30 under key 0, taken from THE NEGATIVE of key 1.
This is must be the final array:
Array
(
[0] => Array
(
[id] => 1
[amount_positive] => 10.00
[negative_sum] => -10.00
[negative] => Array
(
[0] => Array
(
[amount] => -3.00
)
[1] => Array
(
[amount] => -2.00
)
[2] => Array
(
[amount] => -0.70
)
[3] => Array
(
[amount] => -4.30
)
)
)
[1] => Array
(
[id] => 13
[amount_positive] => 6.00
[negative_sum] => -2.70
[negative] => Array
(
[0] => Array
(
[amount] => -2.70
)
)
)
)
If you need code that performs the transformation on this particular array (where you know the array indexes), the following will do what you want:
$expense = -4.30;
$your_array[0]['negative'][] = ['amount' => $expense];
$your_array[0]['negative_sum'] += $expense;
$your_array[1]['negative'][0]['amount'] -= $expense;
$your_array[1]['negative_sum'] -= $expense;
If you want a more "general" approach for arbitrary indexes and arbitrary amount entries, you will need to rephrase your question in broader terms.

sorting 2D-Array in PHP

I have the following 2D-Array:
Array (
[ID] => Array
(
[0] => 150
[1] => 250
)
[Group] => Array
(
[0] => 120
[1] => 120
)
[Name] => Array
(
[0] => Name 1
[1] => Name 2
)
[Price] => Array
(
[0] => 9.99
[1] => 11.99
)
[Date] => Array
(
[0] => 12.04.2013
[1] => 11.04.2013
)
[SortIndex] => Array
(
[0] => 20
[1] => 10
)
)
and I want to sort them after the SortIndex-Array ascending. But the different Values from the Arrays are related to the others. So the association can't be changed.
How I could sort them?
Is there is any reason not to convert array to more suitable form?
Array (
[0] => Array
(
[ID] => 150
[Group] => 120
)
[1] => Array
(
[ID] => 250
[GROUP] => 120
)
....

Merging two objects

Quotes object:
Array
(
[0] => Array
(
[sitecaptions] => Array
(
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras
and Camcorders
)
)
)
The Posts Object:
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array
(
[0] => Array
(
[id] => 99
[name] => Others
)
)
)
[1] => Array
(
[Post] => Array
(
[id] => 2798
[post_title] => xx2
[item_desc] => xx2 desc
[dateadded] => 2009-12-22 11:10:45
)
[Category] => Array
(
[0] => Array
(
[id] => 99
[name] => Others
)
)
)
)
As you can see, the Posts Object contains two elements, [Post] and
[Category] for each record [0],[1] etc. I want to insert the
[sitecaptions] element into that Posts Object so that in effect it
looks like:
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array
(
[0] => Array
(
[id] => 99
[name] => Others
)
)
[sitecaptions] => Array
(
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras
and Camcorders
)
)
)
How do I combine two objects like that? Or how do I insert elements
into an existing object? Hope I'm clear about what I'm asking. Thanks
for your time...
lets call these objects $Quotes and $Posts respectively.
Quotes object:
Array (
[0] => Array (
[sitecaptions] => Array (
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras and Camcorders )
)
)
)
The Posts Object:
Array (
[0] => Array (
[Post] => Array (
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array (
[0] => Array (
[id] => 99
[name] => Others
)
)
)
[1] => Array (
[Post] => Array (
[id] => 2798
[post_title] => xx2
[item_desc] => xx2 desc
[dateadded] => 2009-12-22 11:10:45
)
[Category] => Array (
[0] => Array (
[id] => 99
[name] => Others
)
)
)
)
do you want the [sitecaptions] from the $quotes to be in both of your $posts elements? or just the one with the same key?
as to say if you have just $quotes[0] only $posts[0] will be affected. OR both $posts[0] and $posts[1] will be affected.
if you want $quotes[0] to be in both $post elements you can do this:
foreach ($posts as $key=>$post) {
$posts[$key]['sitecaptions'] = $quotes[0]['sitecaptions'];
}
if you want only the elements from $quotes that have the same index as the elements in $posts you can do this:
$posts = array_merge_recursive($posts,$quotes);
doing this second one would have $posts[1] being without a ['sitecaptions'] element.
end result:
Array (
[0] => Array (
[Post] => Array (
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array (
[0] => Array (
[id] => 99
[name] => Others
)
)
[sitecaptions] => Array (
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras and Camcorders
)
)
)
Hope it helps!

Categories