Remove or merge multidimensionals arrays - php

How to from array like this
[0] => Array
(
[0] => Array
(
[0] => Array
(
[SKU] => T
[title] => BT
[min] => SPads
)
[1] =>
)
[1] => Array
(
[0] => Array
(
[SKU] => R
[title] => RT
[min] => XPads
)
[1] =>
)
)
make arrays like this:
[0] => Array
(
[0] => Array
(
[SKU] => T
[title] => BT
[min] => SPads
)
[1] =>
)
[1] => Array
(
[0] => Array
(
[SKU] => R
[title] => RT
[min] => XPads
)
[1] =>
)
And how to array like this
[0] => Array
(
[0] => Array
(
[0] => Array
(
[SKU] => T
[title] => BT
[min] => SPads
)
[1] =>
)
)
make array like this:
[0] => Array
(
[0] => Array
(
[SKU] => T
[title] => BT
[min] => SPads
)
[1] =>
)
I only found soultions with removing multidimensionals arrays using value, but here im not having value, im having just key, any help?
array_splice and array_diff is php functions that i using to solve my problem from this website https://www.php.net but i didnt found the solution.

Removing first index can be achieve by using array_shift, see demo below.
<?php
//Enter your code here, enjoy!
$a = [
0 => [
0 => [
0 => [
'SKU' => 'T',
'title' => 'BT',
'min' => 'SPads',
],
1 => '',
],
[
0 => [
'SKU' => 'R',
'title' => 'RT',
'min' => 'XPads',
],
1 => '',
],
],
];
$a = array_shift($a);
print_r($a);
array_shift - Shift an element off the beginning of array
Output
Array
(
[0] => Array
(
[0] => Array
(
[SKU] => T
[title] => BT
[min] => SPads
)
[1] =>
)
[1] => Array
(
[0] => Array
(
[SKU] => R
[title] => RT
[min] => XPads
)
[1] =>
)
)
Demo.

You can iterate in foreach loop and create new array in reqired formate.
$newAr =array();
foreach ($oldArr as $tempArr){
foreach($tempArr as $tempArr2){
$newAr[] = $tempArr2;
}
}
print_r($newArr);

Related

Reorder Multi Dimensional Array using Custom Keys

I have an array like below
Array
(
[0] => Array
(
[text] => one
[mp3] => 1.mp3
)
[1] => Array
(
[text] => two
[mp3] => 2.mp3
)
[2] => Array
(
[text] => three
[mp3] => 3.mp3
)
)
And I have another array with index to sort array(1,0,2) ,So With these I want following
Array
(
[0] => Array(
[text] => two[mp3] => 2. mp3
)
[1] => Array(
[text] => one[mp3] => 1. mp3
)
[2] => Array(
[text] => three[mp3] => 3. mp3
)
)
I googled and Found Few solutions on stackoverflow , But None seems to be successful for me
$order=array(1,0,2);
$orderedarray = array_merge(array_flip($order),$myarr);
$myarr = $orderedarray;
print_r($myarr);
Which outputs following
(
[0] => 0
[1] => 1
[2] => 2
[3] => Array
(
[text] => one
[mp3] => 1.mp3
)
[4] => Array
(
[text] => two
[mp3] => 2.mp3
)
[5] => Array
(
[text] => three
[mp3] => 3.mp3
)
)
You need to use array_multisort.
$order =[1,0,2];
array_multisort($myarr, $order);
Var_dump($myarr);
https://3v4l.org/cCT9d

merge two array result wrong array which contain second array as array rather a complete of both

I tries to array_merge in php but resultant array is not correct
1. Array ( [id] => 12 [name] => Popular )
2. Array ( [0] => Array ( [id] => 8 [name] => Flowers ) [1] => Array ( [id] => 10 [name] => Chocolates ) [2] => Array ( [id] => 11 [name] => Sweets and Dry Fruits ) )
Resultant Array
Array ( [id] => 12 [name] => Popular [0] => Array ( [id] => 8 [name] => Flowers ) [1] => Array ( [id] => 10 [name] => Chocolates ) [2] => Array ( [id] => 11 [name] => Sweets and Dry Fruits ) )
If you just want to add the new data in the same format as the existing data then use [] rather than array_merge().
$array1 = array( 'id' => 12, 'name' => 'Popular');
$array2 = array(array( 'id' => 8, 'name' => 'Flowers'),
array( 'id' => 10, 'name' => 'Chocolates'),
array( 'id' => 11, 'name' => 'Sweets and Dry Fruits')
);
$array2[] = $array1;
print_r($array2);
outputs...
Array
(
[0] => Array
(
[id] => 8
[name] => Flowers
)
[1] => Array
(
[id] => 10
[name] => Chocolates
)
[2] => Array
(
[id] => 11
[name] => Sweets and Dry Fruits
)
[3] => Array
(
[id] => 12
[name] => Popular
)
)
If you want the data to be at the front, then you need to create an array of that data and then use array_merge()...
$array3 = array_merge(array($array1), $array2);
print_r( $array3);

Get a specific value from a multidimensional array provided by AgileCRM

I'm trying to get the first deal ID from AgileCRM.
When using:
$test = json_decode($deal, true);
print_r($test);
I get the following result:
Array (
[0] => Array (
[colorName] => WHITE
[id] => 5686812383117312
[apply_discount] =>
[discount_value] => 0
[discount_amt] => 0
[discount_type] => Value
[name] => New Home Loan
[contact_ids] => Array (
[0] => 5645056174194688
)
[custom_data] => Array (
)
[products] => Array (
)
[description] => New Lead
[expected_value] => 0
[milestone] => New Loan
[probability] => 10
[close_date] => 1521192269
[created_time] => 1510824270
[milestone_changed_time] => 0
[entity_type] => deal
[notes] => Array (
)
[note_ids] => Array (
)
[note_created_time] => 0
[pipeline_id] => 5719238044024832
[archived] =>
[lost_reason_id] => 0
[deal_source_id] => 0
[total_deal_value] => 0
[updated_time] => 1510824270
[isCurrencyUpdateRequired] => 1
[currency_conversion_value] => 0
[tags] => Array (
)
[tagsWithTime] => Array (
)
[contacts] => Array (
[0] => Array (
[id] => 5645056174194688
[type] => PERSON
[properties] => Array (
[0] => Array (
[type] => SYSTEM
[name] => first_name
[value] => piet
)
[1] => Array (
[type] => SYSTEM
[name] => last_name
[value] => pompies
)
[2] => Array (
[type] => SYSTEM
[name] => name
[value] =>
)
)
)
)
[owner] => Array (
[id] => 5178546118721536
[domain] => domainname
[email] => myemail#email.com
[phone] =>
[name] => Piet Pompies
[pic] => https://d1gwclp1pmzk26.cloudfront.net/img/gravatar/48.png
[schedule_id] => Piet Pompies
[calendar_url] => https://homeside.agilecrm.com/calendar/Piet_Pompies
[calendarURL] => https://homeside.agilecrm.com/calendar/Piet_Pompies
)
)
)
I want to echo "5686812383117312" from "[id] => 5686812383117312" (4th line in the array above)
I've tried "foreach" statements but my expertise on it is limited and can't seem to get it right.
Any help will be appreciated.
In order to access the ID field you should:
get the array's first key
Access the required field
Array:
Array ( //$test
[0] => Array ( //first key [0]
[colorName] => WHITE
[id] => 5686812383117312 //the required field ['id']
[apply_discount] =>
PHP:
$test = json_decode($deal, true);
print_r($test);
echo $test[0]['id']; //Output: 5686812383117312

How to merge the values of two objects into one object in codeigniter

I have an array of objects,those all objects are same object. But finally I need to put all those values of an object into single object.These are my objects.
Array
(
[0] => Permissions Object
(
[modules] => Array
(
[0] => 3
[1] => 4
)
[submodules] => Array
(
)
[operations] => Array
(
[3M] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
[4M] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
)
)
[1] => Permissions Object
(
[modules] => Array
(
[0] => 1
[1] => 3
)
[submodules] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 7
[5] => 8
)
[operations] => Array
(
[1] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
[2] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
[3] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
[4] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
[7] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
[8] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
[3M] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
)
)
)
If you don't want to use array_merge, you can use a workaround as follows:
$global; //your array
$merged = array(); //the array you want with the merged objects
foreach ($global as $k => $object) {
foreach ($object as $key => $values) {
foreach ($values as $val)
$merged[$key][] = $val;
}
}
Before or after merging you just have to Remove duplicate values.
$array1 = [
"modules" => [
0 => 2, 1 => 4
],
"submodules" => [
"3m" => [
0 => "create", 1 => "read"
],
"4m" => [0 => "create", 1 => "read"
]
]
];
$array2 = [
"modules" => [
0 => 1,
1 => 3
],
"submodules" => [
"1m" => [
0 => "create", 1 => "read"
],
"10m" => [
0 => "create", 1 => "read"
]
]
];
$merged = array_merge_recursive($array1, $array2);
print_r($merged);
Array ( [modules] => Array (
[0] => 2 [1] => 4 [2] => 1 [3] => 3
)
[submodules] => Array (
[3m] => Array ( [0] => create [1] => read
)
[4m] =>
Array ( [0] => create [1] => read
)
[1m] => Array (
[0] => create [1] => read
)
[10m] => Array (
[0] => create [1] => read
)
)
)

Reformating associative arrays

To be more specific I want to turn the following array into an associative one. The original array is indexed like [0],[1],[2],…[n]. The function I used was Set::combine of Cakephp but I couldn't recreate all three levels of the desired associative array.
Array
(
[0] => Array
(
[ACCOUNTS] => Array
(
[description] => A
)
[HEADERS] => Array
(
[description] => B
)
[COLUMNS] => Array
(
[description] => C
[id] => 8
)
)
[1] => Array
(
[ACCOUNTS] => Array
(
[description] => A1
)
[HEADERS] => Array
(
[description] => B1
)
[COLUMNS] => Array
(
[description] => C1
[id] => 9
)
)
)
The array I want to end up is the following associative array:
Array
(
[A] => Array
(
[B] => Array
(
[C] => 8
)
)
[A1] => Array
(
[B1] => Array
(
[C1] => 9
)
)
)
I can't recreate all (3) levels of the array above.
Do you mean like:
$newarray = array($first['ACCOUNTS']['description'] => array($first['HEADERS']['description'] => array($first['COLUMNS']['description'] => $first['COLUMNS']['id'])));
So if you run the following it gives what you want:
$first = array(
'ACCOUNTS' => array('description' => 'A'),
'HEADERS' => array('description' => 'B'),
'COLUMNS' => array('description' => 'C', 'id' => '8'));
echo "<pre>";
print_r($first);
$newarray = array($first['ACCOUNTS']['description'] =>
array($first['HEADERS']['description'] =>
array($first['COLUMNS']['description'] =>
$first['COLUMNS']['id'])));
print_r($newarray);
You then end up with:
Array
(
[ACCOUNTS] => Array
(
[description] => A
)
[HEADERS] => Array
(
[description] => B
)
[COLUMNS] => Array
(
[description] => C
[id] => 8
)
)
Array
(
[A] => Array
(
[B] => Array
(
[C] => 8
)
)
)

Categories