Merging PHP arrays with different keys - php

I want to merge two arrays.
The first one looks like this:
array(28) {
[0]=>
array(17) {
["category_id"]=>
string(2) "11"
["image"]=>
string(27) "718426050751.jpg"
["parent_id"]=>
string(1) "1"
["top"]=>
string(1) "0"
["column"]=>
string(1) "1"
["sort_order"]=>
string(2) "21"
["status"]=>
string(1) "1"
["date_added"]=>
string(19) "2015-07-30 14:06:42"
["date_modified"]=>
string(19) "2016-01-05 12:21:32"
["language_id"]=>
string(1) "1"
["name"]=>
string(7) "Faucets"
["description"]=>
string(0) ""
["meta_description"]=>
string(0) ""
["meta_keyword"]=>
string(0) ""
["store_id"]=>
string(1) "1"
["product_count"]=>
string(1) "0"
["page_count"]=>
string(1) "0"
}
And the second one looks like this:
array(17) {
["category_id"]=>
string(2) "13"
["image"]=>
string(28) "4005176268779.jpg"
["parent_id"]=>
string(2) "11"
["top"]=>
string(1) "0"
["column"]=>
string(1) "1"
["sort_order"]=>
string(1) "2"
["status"]=>
string(1) "1"
["date_added"]=>
string(19) "2015-07-30 14:06:43"
["date_modified"]=>
string(19) "2016-01-07 14:10:53"
["language_id"]=>
string(1) "1"
["name"]=>
string(12) "Sink Faucets"
["description"]=>
string(0) ""
["meta_description"]=>
string(0) ""
["meta_keyword"]=>
string(0) ""
["store_id"]=>
string(1) "1"
["product_count"]=>
string(1) "0"
["page_count"]=>
string(1) "0"
}
As you can see the second one doesn't have a key like the first one does which is [0].
When I use:
$children = array_merge((array)$children, (array)$additional);
I get a merged array but the additional array is not getting a key which for me would preferably be [1] or +1 from the last array key.
Is it possible to do this?
Thanks

$array = [
"0" => ["key1"=>"val1", "key2"=>"val2"]
];
$merge = ["key1"=>"val3", "key2"=>"val4"];
array_merge($array, [$merge]);
result:
Array
(
[0] => Array
(
[key1] => val1
[key2] => val2
)
[1] => Array
(
[key1] => val3
[key2] => val4
)
)
http://sandbox.onlinephpfunctions.com/code/a2e7abb2d9580b5f487d56b99a782505633c840b

array_merge() is not necessary, you merely need to push the second array into the first. $a[]=$b; will add $b's data to $a and assign an auto-incremented key without a function call.
Code:
$a=[
0=>[
"category_id"=>"11",
"image"=>"718426050751.jpg",
"parent_id"=>"1",
"top"=>"0",
"column"=>"1",
"sort_order"=>"21",
"status"=>"1",
"date_added"=>"2015-07-30 14:06:42",
"date_modified"=>"2016-01-05 12:21:32",
"language_id"=>"1",
"name"=>"Faucets",
"description"=>"",
"meta_description"=>"",
"meta_keyword"=>"",
"store_id"=>"1",
"product_count"=>"0",
"page_count"=>"0"
]
];
$b=[
"category_id"=>"13",
"image"=>"4005176268779.jpg",
"parent_id"=>"11",
"top"=>"0",
"column"=>"1",
"sort_order"=>"2",
"status"=>"1",
"date_added"=>"2015-07-30 14:06:43",
"date_modified"=>"2016-01-07 14:10:53",
"language_id"=>"1",
"name"=>"Sink Faucets",
"description"=>"",
"meta_description"=>"",
"meta_keyword"=>"",
"store_id"=>"1",
"product_count"=>"0",
"page_count"=>"0"
];
$a[]=$b; // push, not merge
var_export($a);
Output:
array (
0 =>
array (
'category_id' => '11',
'image' => '718426050751.jpg',
'parent_id' => '1',
'top' => '0',
'column' => '1',
'sort_order' => '21',
'status' => '1',
'date_added' => '2015-07-30 14:06:42',
'date_modified' => '2016-01-05 12:21:32',
'language_id' => '1',
'name' => 'Faucets',
'description' => '',
'meta_description' => '',
'meta_keyword' => '',
'store_id' => '1',
'product_count' => '0',
'page_count' => '0',
),
1 =>
array (
'category_id' => '13',
'image' => '4005176268779.jpg',
'parent_id' => '11',
'top' => '0',
'column' => '1',
'sort_order' => '2',
'status' => '1',
'date_added' => '2015-07-30 14:06:43',
'date_modified' => '2016-01-07 14:10:53',
'language_id' => '1',
'name' => 'Sink Faucets',
'description' => '',
'meta_description' => '',
'meta_keyword' => '',
'store_id' => '1',
'product_count' => '0',
'page_count' => '0',
),
)

Related

How to turn a nested array into a single level and retain the keys

I have the following array that I want to return as a single level so I can output it to a CSV. I need to keep the array keys. The arrays that are "children_data" I want to move to the same level as the parent array.
array(8) {
["id"]=>
int(2)
["parent_id"]=>
int(1)
["name"]=>
string(16) "Category 1"
["is_active"]=>
bool(true)
["position"]=>
int(1)
["level"]=>
int(1)
["product_count"]=>
int(1)
["children_data"]=>
array(3) {
[0]=>
array(8) {
["id"]=>
int(2380)
["parent_id"]=>
int(2)
["name"]=>
string(11) "subcat 1"
["is_active"]=>
bool(true)
["position"]=>
int(1)
["level"]=>
int(2)
["product_count"]=>
int(1)
["children_data"]=>
array(4) {
[0]=>
array(8) {
["id"]=>
int(2381)
["parent_id"]=>
int(2380)
["name"]=>
string(11) "subsub cat 1"
["is_active"]=>
bool(true)
["position"]=>
int(1)
["level"]=>
int(3)
["product_count"]=>
int(1)
["children_data"]=>
array(7) {
[0]=>
array(8) {
["id"]=>
int(2382)
["parent_id"]=>
int(2381)
["name"]=>
string(21) "subsubsub cat1"
["is_active"]=>
bool(true)
["position"]=>
int(1)
["level"]=>
int(4)
["product_count"]=>
int(1)
["children_data"]=>
array(19) {
[0]=>
array(8) {
["id"]=>
int(2383)
["parent_id"]=>
int(2382)
["name"]=>
string(12) "subsubssubsubb cat1"
["is_active"]=>
bool(true)
["position"]=>
int(1)
["level"]=>
int(5)
["product_count"]=>
int(0)
["children_data"]=>
array(4) {
[0]=>
array(8) {
["id"]=>
int(2384)
["parent_id"]=>
int(2383)
["name"]=>
string(13) "subsub1"
["is_active"]=>
bool(true)
["position"]=>
int(1)
["level"]=>
int(6)
["product_count"]=>
int(0)
["children_data"]=>
array(0) {
}
}
[1]=>
array(8) {
["id"]=>
int(2385)
["parent_id"]=>
int(2383)
["name"]=>
string(10) "subsub2"
["is_active"]=>
bool(true)
["position"]=>
int(2)
["level"]=>
int(6)
["product_count"]=>
int(0)
["children_data"]=>
array(0) {
}
}
What I need is:
array(8) {
["id"]=>
int(2)
["parent_id"]=>
int(1)
["name"]=>
string(16) "Category 1"
["is_active"]=>
bool(true)
["position"]=>
int(1)
["level"]=>
int(1)
["product_count"]=>
int(1)
array(8) {
["id"]=>
int(2380)
["parent_id"]=>
int(2)
["name"]=>
string(11) "subcat 1"
["is_active"]=>
bool(true)
["position"]=>
int(1)
["level"]=>
int(2)
["product_count"]=>
int(1)
array(8) {
["id"]=>
int(2381)
["parent_id"]=>
int(2380)
["name"]=>
string(11) "subsub cat 1"
["is_active"]=>
bool(true)
["position"]=>
int(1)
["level"]=>
int(3)
["product_count"]=>
int(1)
array(8) {
["id"]=>
int(2382)
["parent_id"]=>
int(2381)
["name"]=>
string(21) "subsubsub cat1"
["is_active"]=>
bool(true)
["position"]=>
int(1)
["level"]=>
int(4)
["product_count"]=>
int(1)
ETC
I need this to create a comparison between our Main DB categories and Magento categories. This way when we do a product import via the API, I can match the Magento category with our Main DB category.
There are a lot of good examples in the question linked by #waterloomatt, but that is a question that specifically asks "how to do this without using recursion".
The simplest way to turn your nested category tree into a flattened array is to use recursion:
function flatten($array, &$result) {
foreach ($array as $item) {
$children = $item['children_data'] ?? array();
unset($item['children_data']);
$result[] = $item;
flatten($children, $result);
}
}
$flattened = array();
flatten($categories, $flattened);
Note that this does require $categories to be an array that's nested one more level than your var_dump() sample data, so:
$categories = array(
0 => array(
"id" => 2,
"parent_id" => 1,
"name" => "Category 1",
"is_active" => true,
"position" => 1,
"level" => 1,
"product_count" => 1,
"children_data" => array(
0 => array(
"id" => 2380,
"parent_id" => 2,
...etc
This will give you the array you want, which you can then iterate over to output it into your CSV file:
var_export($flattened);
array (
0 =>
array (
'id' => 2,
'parent_id' => 1,
'name' => 'Category 1',
'is_active' => true,
'position' => 1,
'level' => 1,
'product_count' => 1,
),
1 =>
array (
'id' => 2380,
'parent_id' => 2,
'name' => 'subcat 1',
'is_active' => true,
'position' => 1,
'level' => 2,
'product_count' => 1,
),
2 =>
array (
'id' => 2381,
'parent_id' => 2380,
'name' => 'subsub cat 1',
'is_active' => true,
'position' => 1,
'level' => 3,
'product_count' => 1,
),
3 =>
array (
'id' => 2382,
'parent_id' => 2381,
'name' => 'subsubsub cat1',
'is_active' => true,
'position' => 1,
'level' => 4,
'product_count' => 1,
),
4 =>
array (
'id' => 2383,
'parent_id' => 2382,
'name' => 'subsubssubsubb cat1',
'is_active' => true,
'position' => 1,
'level' => 5,
'product_count' => 0,
),
5 =>
array (
'id' => 2384,
'parent_id' => 2383,
'name' => 'subsub1',
'is_active' => true,
'position' => 1,
'level' => 6,
'product_count' => 0,
),
6 =>
array (
'id' => 2385,
'parent_id' => 2383,
'name' => 'subsub2',
'is_active' => true,
'position' => 2,
'level' => 6,
'product_count' => 0,
),
)

How to convert Multidimensional array to Associative array in PhP?

I need to convert the multidimensional array to an associative array.
Please help me to convert the array. Need to remove the keys "sub_code" & "credits" and make the first string as "key" & the second string as "value". I tried a lot of ways but I failed.
Need to convert the below array.
array(9) {
[0]=> array(2)
{
["sub_code"] => string(6) "HS6151"
["credits"] => string(1) "4"
}
[1]=> array(2)
{
["sub_code"] => string(6) "MA6151"
["credits"] => string(1) "4"
}
[2]=> array(2)
{
["sub_code"] => string(6) "PH6151"
["credits"] => string(1) "3"
}
[3]=> array(2)
{
["sub_code"] => string(6) "CY6151"
["credits"] => string(1) "3"
}
[4]=> array(2)
{
["sub_code"] => string(6) "GE6151"
["credits"] => string(1) "3"
}
[5]=> array(2)
{
["sub_code"] => string(6) "GE6152"
["credits"] => string(1) "4"
}
[6]=> array(2)
{
["sub_code"] => string(6) "GE6161"
["credits"] => string(1) "2"
}
[7]=> array(2)
{
["sub_code"] => string(6) "GE6162"
["credits"] => string(1) "2"
}
[8]=> array(2)
{
["sub_code"] => string(6) "GE6163"
["credits"] => string(1) "1"
}
}
Like below.
array(9) {
["HS6151"] => string(1) "4"
["MA6151"] => string(1) "4"
["PH6151"] => string(1) "3"
["CY6151"] => string(1) "3"
["GE6151"] => string(1) "3"
["GE6152"] => string(1) "4"
["GE6161"] => string(1) "2"
["GE6162"] => string(1) "2"
["GE6163"] => string(1) "1"
}
You can use array_column like so:
$array = [
[
'sub_code' => 'HS6151',
'credits' => '4',
],
[
'sub_code' => 'MA6151',
'credits' => '4',
],
];
$result = array_column($array, 'credits', 'sub_code');
print_r($result);
Results in:
Array
(
[HS6151] => 4
[MA6151] => 4
)
Sounds pretty straight forward:
<?php
$input = [
[
"sub_code" => "HS6151",
"credits" => "4"
], [
"sub_code" => "MA6151",
"credits" => "4"
], [
"sub_code" => "PH6151",
"credits" => "3"
], [
"sub_code" => "CY6151",
"credits" => "3"
], [
"sub_code" => "GE6151",
"credits" => "3"
], [
"sub_code" => "GE6152",
"credits" => "4"
], [
"sub_code" => "GE6161",
"credits" => "2"
], [
"sub_code" => "GE6162",
"credits" => "2"
], [
"sub_code" => "GE6163",
"credits" => "1"
]
];
$output = [];
array_walk($input, function($entry) use (&$output) {
$output[$entry["sub_code"]] = $entry["credits"];
});
print_r($output);
The output obviously is:
Array
(
[HS6151] => 4
[MA6151] => 4
[PH6151] => 3
[CY6151] => 3
[GE6151] => 3
[GE6152] => 4
[GE6161] => 2
[GE6162] => 2
[GE6163] => 1
)

Sorting array of arrays using array_multisort() [Argument #1 is expected to be an array or a sort flag]

I have this array of arrays
foreach($qcatResults as $qcatResult){
...//codes to get stuff
$cats[] = array(
'category_code' => $qcatResult->category_code,
'category' => $qcatResult->category,
'item_count' => $item_count,
'max_qty' => (int)$max_qty
);
}
var_dump($cats);
which would result to something like
array(34) {
[0]=> array(4) { ["category_code"]=> string(2) "BB" ["category"]=> string(0) ""
["item_count"]=> string(1) "1" ["max_qty"]=> int(12000) }
[1]=> array(4) { ["category_code"]=> string(2) "AK" ["category"]=> string(6) "Anklet"
["item_count"]=> string(1) "1" ["max_qty"]=> int(6) }
[2]=> array(4) { ["category_code"]=> string(3) "BAC" ["category"]=> string(15) "Bag Accessories"
["item_count"]=> string(1) "2" ["max_qty"]=> int(352) }
[3]=> array(4) { ["category_code"]=> string(2) "WB" ["category"]=> string(4) "Bags"
["item_count"]=> string(1) "9" ["max_qty"]=> int(6290) }
[4]=> array(4) { ["category_code"]=> string(2) "AB" ["category"]=> string(20) "Bathroom Accessories"
["item_count"]=> string(2) "19" ["max_qty"]=> int(325) }
[5]=> array(4) { ["category_code"]=> string(2) "BK" ["category"]=> string(4) "Book"
["item_count"]=> string(2) "40" ["max_qty"]=>int(27291) }...
}
I want to sort $cats by max_qty in descending order, but array_multisort is giving out the error Message: array_multisort(): Argument #1 is expected to be an array or a sort flag
I have this usage of array_multisort():
var_dump(array_multisort($max_qty, SORT_DESC, $cats));
I think usort() can do the trick for you:
$cats=[
["category_code"=>"BB","category"=>"","item_count"=>"1","max_qty"=>12000],
["category_code"=>"AK","category"=>"Anklet","item_count"=>"1","max_qty"=>6],
["category_code"=>"BAC","category"=>"Bag Accessories","item_count"=>"2","max_qty"=>352],
["category_code"=>"WB","category"=>"Bags","item_count"=>"9","max_qty"=>6290],
["category_code"=>"AB","category"=>"Bathroom Accessories","item_count"=>"19","max_qty"=>325],
["category_code"=>"BK","category"=>"Book","item_count"=>"40","max_qty"=>27291]
];
usort($cats,function($a,$b){
return $b['max_qty']-$a['max_qty'];
});
var_export($cats);
Output:
array (
0 =>
array (
'category_code' => 'BK',
'category' => 'Book',
'item_count' => '40',
'max_qty' => 27291,
),
1 =>
array (
'category_code' => 'BB',
'category' => '',
'item_count' => '1',
'max_qty' => 12000,
),
2 =>
array (
'category_code' => 'WB',
'category' => 'Bags',
'item_count' => '9',
'max_qty' => 6290,
),
3 =>
array (
'category_code' => 'BAC',
'category' => 'Bag Accessories',
'item_count' => '2',
'max_qty' => 352,
),
4 =>
array (
'category_code' => 'AB',
'category' => 'Bathroom Accessories',
'item_count' => '19',
'max_qty' => 325,
),
5 =>
array (
'category_code' => 'AK',
'category' => 'Anklet',
'item_count' => '1',
'max_qty' => 6,
),
)

How to sort PHP array when there are multiple values

I have an array as below:
$people = array(
200 => array(
'id' => 12345,
'first_name' => 'Joe',
'surname' => 'Bloggs',
'age' => 23,
'sex' => 'm'
),
100 => array(
'id' => 12346,
'first_name' => 'Adam',
'surname' => 'Smith',
'age' => 18,
'sex' => 'm'
),
500 => array(
'id' => 12347,
'first_name' => 'Amy',
'surname' => 'Jones',
'age' => 21,
'sex' => 'f'
)
I want to sort this array on the first value that you can see as 200, 100 and 500.
$result = array(
array('name' => 'Tahir', 'age' => '40'),
array('name' => 'Usman', 'age' => '30'),
array('name' => 'Danish', 'age' => '20'),
array('name' => 'Aneeq', 'age' => '10')
);
echo '<pre>';
print_r($result);
echo '</pre>';
foreach ($result as $key => $row) {
$name[$key] = $row['name'];
$age[$key] = $row['age'];
}
array_multisort($age, SORT_ASC, $result);
echo '<pre>';
print_r($result);
echo '</pre>';
echo "<pre>";
echo "Name\t\tAge";
foreach ( $result as $var ) {
echo "\n", $var['name'], "\t\t", $var['age'];
}
You can use ksort() use ascending order and krsort() for descending order.
ksort($people);
krsort($people);
This should be as simple as:
ksort($people);
To make it sort by the actual numeric value (to make 1000 come after 500 not after 100), use:
ksort($people, SORT_NUMERIC);
ksort() sorts an array by its keys.
For example:
php > var_dump($people);
array(3) {
[200]=>
array(5) {
["id"]=>
int(12345)
["first_name"]=>
string(3) "Joe"
["surname"]=>
string(6) "Bloggs"
["age"]=>
int(23)
["sex"]=>
string(1) "m"
}
[100]=>
array(5) {
["id"]=>
int(12346)
["first_name"]=>
string(4) "Adam"
["surname"]=>
string(5) "Smith"
["age"]=>
int(18)
["sex"]=>
string(1) "m"
}
[500]=>
array(5) {
["id"]=>
int(12347)
["first_name"]=>
string(3) "Amy"
["surname"]=>
string(5) "Jones"
["age"]=>
int(21)
["sex"]=>
string(1) "f"
}
}
php > ksort($people);
php > var_dump($people);
array(3) {
[100]=>
array(5) {
["id"]=>
int(12346)
["first_name"]=>
string(4) "Adam"
["surname"]=>
string(5) "Smith"
["age"]=>
int(18)
["sex"]=>
string(1) "m"
}
[200]=>
array(5) {
["id"]=>
int(12345)
["first_name"]=>
string(3) "Joe"
["surname"]=>
string(6) "Bloggs"
["age"]=>
int(23)
["sex"]=>
string(1) "m"
}
[500]=>
array(5) {
["id"]=>
int(12347)
["first_name"]=>
string(3) "Amy"
["surname"]=>
string(5) "Jones"
["age"]=>
int(21)
["sex"]=>
string(1) "f"
}
}
php >
You can try this solution.
<?php
$people = array(
200 => array(
'id' => 12345,
'first_name' => 'Joe',
'surname' => 'Bloggs',
'age' => 23,
'sex' => 'm'
),
100 => array(
'id' => 12346,
'first_name' => 'Adam',
'surname' => 'Smith',
'age' => 18,
'sex' => 'm'
),
500 => array(
'id' => 12347,
'first_name' => 'Amy',
'surname' => 'Jones',
'age' => 21,
'sex' => 'f'
));
ksort($people, SORT_NUMERIC);
print_r($people);
?>
You can use the following two functions according to your requirement:-
ksort() - sort associative arrays in ascending order, according to the key
krsort() - sort associative arrays in descending order, according to the key
How you can use it:-
ksort($people);
krsort($people);

PHP Sorting multidimesional array by first key

i got an array like this:
array
'list_10' =>
array
row_0 =>
array
'Id' => string '118579'
'Status' => string '3'
row_1 =>
array
'Id' => string '117662'
'Status' => string '2'
row_2 =>
array
'Id' => string '117662'
'Status' => string '2'
'list_11' =>
array
row_0 =>
array
'Id' => string '112564'
'Status' => string '2'
row_1 =>
array
'Id' => string '153622'
'Status' => string '3'
row_2 =>
array
'Id' => string '112832'
'Status' => string '1'
i want to "natsort" the first key "list_XX", making it start with 0,1,2,.. instead of 10,11,12,13,0,1,2,3,...
i played around with array_multisort but i cant seem to
set the right params to make it do what i want, if its even capable of doing this.
any advice?
Assuming your array is something like this:
$array = [
'list_11' =>
[
'row_0' =>
[
'Id' => '118579',
'Status' => '3'
],
'row_1' =>
[
'Id' => '117662',
'Status' => '2'
],
'row_2' =>
[
'Id' => '117662',
'Status' => '2'
]
],
'list_10' =>
[
'row_0' =>
[
'Id' => '112564',
'Status' => '2'
],
'row_1' =>
[
'Id' => '153622',
'Status' => '3'
],
'row_2' =>
[
'Id' => '112832',
'Status' => '1'
]
],
'list_1' =>
[
'row_0' =>
[
'Id' => '32323232',
'Status' => '3'
],
'row_1' =>
[
'Id' => '2353333',
'Status' => '2'
],
'row_2' =>
[
'Id' => '117662',
'Status' => '2'
]
]
];
Using array_multisort :
$sort = [];
foreach($array as $el=>$val){
$sort[] = $el;
}
array_multisort($array,SORT_NUMERIC,$sort,SORT_NATURAL);
var_dump($array);
Will print :
array(3) {
["list_1"]=>
array(3) {
["row_0"]=>
array(2) {
["Id"]=>
string(8) "32323232"
["Status"]=>
string(1) "3"
}
["row_1"]=>
array(2) {
["Id"]=>
string(7) "2353333"
["Status"]=>
string(1) "2"
}
["row_2"]=>
array(2) {
["Id"]=>
string(6) "117662"
["Status"]=>
string(1) "2"
}
}
["list_10"]=>
array(3) {
["row_0"]=>
array(2) {
["Id"]=>
string(6) "112564"
["Status"]=>
string(1) "2"
}
["row_1"]=>
array(2) {
["Id"]=>
string(6) "153622"
["Status"]=>
string(1) "3"
}
["row_2"]=>
array(2) {
["Id"]=>
string(6) "112832"
["Status"]=>
string(1) "1"
}
}
["list_11"]=>
array(3) {
["row_0"]=>
array(2) {
["Id"]=>
string(6) "118579"
["Status"]=>
string(1) "3"
}
["row_1"]=>
array(2) {
["Id"]=>
string(6) "117662"
["Status"]=>
string(1) "2"
}
["row_2"]=>
array(2) {
["Id"]=>
string(6) "117662"
["Status"]=>
string(1) "2"
}
}
}

Categories