convert indexed multidimensional array to associative multidimensional array - php

I have an indexed array with nested categories like this:
array (
0 =>
array (
'name' => 'Furniture',
'id' => 'b3cdd1k',
'content' =>
array (
0 =>
array (
'name' => 'Tables',
'id' => 'nw24ga3',
'content' =>
array (
0 =>
array (
'name' => 'Wooden tables',
'id' => 'ba5lgaz',
),
1 =>
array (
'name' => 'Glass tables',
'id' => 'rqt91gz',
),
),
),
),
),
1 =>
array (
'name' => 'Lamps',
'id' => 'vb1a4nf',
),
2 =>
array (
'name' => 'Doors',
'id' => 'a5l4gal',
'content' =>
array (
0 =>
array (
'name' => 'Entrance doors',
'id' => 'qwg30fb',
),
),
),
)
Is there elegant way to convert it to associative array (where keys are id's) and keep nesting structure?
After conversion I excepting something like this:
array (
'b3cdd1k' =>
array (
'name' => 'Furniture',
'content' =>
array (
'nw24ga3' =>
array (
'name' => 'Tables',
'content' =>
array (
'ba5lgaz' =>
array (
'name' => 'Wooden tables',
),
'rqt91gz' =>
array (
'name' => 'Glass tables',
),
),
),
),
),
'vb1a4nf' =>
array (
'name' => 'Lamps',
),
'a5l4gal' =>
array (
'name' => 'Doors',
'content' =>
array (
'qwg30fb' =>
array (
'name' => 'Entrance doors',
),
),
),
)

You could try this - not the most elegant, but seems to work:
function convert(&$a)
{
$result = Array();
foreach($a as $k=>&$v)
{
$result[$v['id']]['name'] = $v['name'];
if(is_array($v['content'])) $result[$v['id']]['content'] = convert($v['content']);
}
return $result;
}
if(count($array) != 0) $result = convert($array);

Related

Nested array, replace alphabetical keys with numeric and keep structure

I have a nested array who is 4 levels deep.
I would like to replace the keys by numerical ones but not the meta level, this array should keep the alphabetical keys.
The array will have a lot of values and there will be more accounts like Marco Mueller, this is just a short example. It is important that it will keep the same nested structure, after its replaced by numerical keys.
$array = array (
'Marco Mueller' =>
array (
'meta' =>
array (
'accountName' => 'Marco Mueller',
'accountId' => '1',
'sum' => '3,659.06',
),
'Conrad, Ute' =>
array (
'meta' =>
array (
'customerName' => 'Conrad, Ute',
'customerId' => '8391',
'sum' => '457.59',
),
'Fidor Bank' =>
array (
'meta' =>
array (
'bankName' => 'Fidor Bank',
'bankKey' => 'FID',
'sum' => '457.59',
),
'H1-2019' =>
array (
'meta' =>
array (
'periodName' => 'H1-2019',
'periodId' => '5',
'sum' => '457.59',
),
),
),
),
),
);
The result should be
$array = array (
array (
'meta' =>
array (
'accountName' => 'Marco Mueller',
'accountId' => '1',
'sum' => '3,659.06',
),
'items' => array (
'meta' =>
array (
'customerName' => 'Conrad, Ute',
'customerId' => '8391',
'sum' => '457.59',
),
'items' =>
array (
'meta' =>
array (
'bankName' => 'Fidor Bank',
'bankKey' => 'FID',
'sum' => '457.59',
),
'items' => array (
'meta' =>
array (
'periodName' => 'H1-2019',
'periodId' => '5',
'sum' => '457.59',
),
),
),
array (
'meta' =>
array (
'periodName' => 'H2-2019',
'periodId' => '6',
'sum' => '600',
),
),
),
),
),
);
I solved my problem with this function, if anyone has an idea how to make this more elegant and efficient, your welcome!
private function getValues(array $array)
{
$newArray = [];
foreach ($array as $item) {
$meta = $item['meta'];
unset($item['meta']);
$arrayValues = array_values($item);
if (count($arrayValues) > 1 || count($arrayValues[0]) > 1) {
$arrayValues = $this->getValues($arrayValues);
}
if (!$arrayValues) {
$values = [
'meta' => $meta,
];
} else {
$values = [
'meta' => $meta,
'items' => $arrayValues,
];
}
$newArray[] = $values;
}
return $newArray;
}

Extract value from array php [duplicate]

This question already has answers here:
Find value of sibling key in php array
(4 answers)
Closed 3 years ago.
I have the following array of data and I am trying to access the tracking number field.
Someting like:
$array->meta_data->TrackingNumber
After 3 hours I cannot find a way to access it.
Any help much appreciated.
array (
'id' => 448,
'parent_id' => 0,
'status' => 'completed',
'currency' => 'GBP',
'version' => '3.7.1',
'prices_include_tax' => true,
'meta_data' =>
array (
0 =>
array (
'id' => 8041,
'key' => 'is_vat_exempt',
'value' => 'no',
),
1 =>
array (
'id' => 8043,
'key' => '_wpam_id',
'value' => '4',
),
2 =>
array (
'id' => 8046,
'key' => '_woo_pp_txnData',
'value' =>
array (
'refundable_txns' =>
array (
0 =>
array (
'txnID' => '1U998392V9620752U',
'amount' => '5.95',
'refunded_amount' => 0,
'status' => 'Completed',
),
),
'txn_type' => 'sale',
),
),
3 =>
array (
'id' => 8056,
'key' => 'Payer PayPal address',
'value' => 'anthony#befive.co.uk',
),
4 =>
array (
'id' => 8057,
'key' => 'Payer first name',
'value' => 'Anthony',
),
5 =>
array (
'id' => 8058,
'key' => 'Payer last name',
'value' => 'Evans',
),
6 =>
array (
'id' => 8059,
'key' => 'Payment type',
'value' => 'instant',
),
7 =>
array (
'id' => 8060,
'key' => '_paypal_status',
'value' => 'completed',
),
8 =>
array (
'id' => 8061,
'key' => '_paypal_transaction_fee',
'value' => '0.47',
),
9 =>
array (
'id' => 8290,
'key' => 'TrackingNumber',
'value' => '10046182',
),
10 =>
array (
'id' => 8291,
'key' => 'CarrierName',
'value' => 'SEABOURNE',
),
11 =>
array (
'id' => 10349,
'key' => '_alg_wc_custom_order_number',
'value' => '6',
),
),
'line_items' =>
array (
48 =>
array (
),
),
'tax_lines' =>
array (
50 =>
array (
),
),
'shipping_lines' =>
array (
49 =>
array (
),
),
'fee_lines' =>
array (
),
'coupon_lines' =>
array (
51 =>
array (
),
),
)
You're trying to access your array with $array->value, but the correct syntax would be $array['value']
I would loop through the meta data and check each key to see if is equals "tracking number" then echo out or do whatever you want with the result!
<?php
$test =array (
'id' => 448,
'parent_id' => 0,
'status' => 'completed',
'currency' => 'GBP',
'version' => '3.7.1',
'prices_include_tax' => true,
'meta_data' =>
array (
0 =>
array (
'id' => 8041,
'key' => 'is_vat_exempt',
'value' => 'no',
),
1 =>
array (
'id' => 8043,
'key' => '_wpam_id',
'value' => '4',
),
2 =>
array (
'id' => 8046,
'key' => '_woo_pp_txnData',
'value' =>
array (
'refundable_txns' =>
array (
0 =>
array (
'txnID' => '1U998392V9620752U',
'amount' => '5.95',
'refunded_amount' => 0,
'status' => 'Completed',
),
),
'txn_type' => 'sale',
),
),
3 =>
array (
'id' => 8056,
'key' => 'Payer PayPal address',
'value' => 'anthony#befive.co.uk',
),
4 =>
array (
'id' => 8057,
'key' => 'Payer first name',
'value' => 'Anthony',
),
5 =>
array (
'id' => 8058,
'key' => 'Payer last name',
'value' => 'Evans',
),
6 =>
array (
'id' => 8059,
'key' => 'Payment type',
'value' => 'instant',
),
7 =>
array (
'id' => 8060,
'key' => '_paypal_status',
'value' => 'completed',
),
8 =>
array (
'id' => 8061,
'key' => '_paypal_transaction_fee',
'value' => '0.47',
),
9 =>
array (
'id' => 8290,
'key' => 'TrackingNumber',
'value' => '10046182',
),
10 =>
array (
'id' => 8291,
'key' => 'CarrierName',
'value' => 'SEABOURNE',
),
11 =>
array (
'id' => 10349,
'key' => '_alg_wc_custom_order_number',
'value' => '6',
),
),
'line_items' =>
array (
48 =>
array (
),
),
'tax_lines' =>
array (
50 =>
array (
),
),
'shipping_lines' =>
array (
49 =>
array (
),
),
'fee_lines' =>
array (
),
'coupon_lines' =>
array (
51 =>
array (
),
),
);
foreach($test['meta_data'] as $data){
if($data['key'] === "TrackingNumber"){
echo "The tracking number is: " . $data['value'];
}
}

Using PHP, search an array by key and replace value in another array with value from matched key

I have two arrays:
$array_a =
array (
0 => array (
0 => array (
'name' => 'name',
'label' => 'LBL_NAME',
),
1 =>
array(
'name' => 'phone_office',
'label' => 'LBL_PHONE_OFFICE',
),
),
1 => array (
0 =>
array(
'name' => 'website',
'label' => 'LBL_WEBSITE',
),
1 =>
array(
'name' => 'phone_fax',
'label' => 'LBL_FAX',
),
),
);
and
$array_b = array(
'LBL_NAME' => 'Name:',
'LBL_PHONE_OFFICE' => 'Office phone:',
'LBL_WEBSITE' => 'Website:',
'LBL_FAX' => 'Fax number:',
);
How do I go about replacing the value of [label] with the corresponding value from my second array?
In other words, what I want to end up with is:
$array_a =
array (
0 => array (
0 => array (
'name' => 'name',
'label' => 'Name:',
),
1 =>
array(
'name' => 'phone_office',
'label' => 'Office phone:',
),
),
1 => array (
0 =>
array(
'name' => 'website',
'label' => 'Website:',
),
1 =>
array(
'name' => 'phone_fax',
'label' => 'Fax number:',
),
),
);
You can do it as following:
foreach($array_a as $elemKey => $elemValue){
foreach($elemValue as $itemKey => $itemValue){
if(isset($array_a[$elemKey][$itemKey]['label'])){
$array_a[$elemKey][$itemKey]['label'] = $array_b[$array_a[$elemKey][$itemKey]['label']];
}
}
}
print_r($array_a);
This will return:
Array
(
[0] => Array
(
[0] => Array
(
[name] => name
[label] => Name:
)
[1] => Array
(
[name] => phone_office
[label] => Office phone:
)
)
[1] => Array
(
[0] => Array
(
[name] => website
[label] => Website:
)
[1] => Array
(
[name] => phone_fax
[label] => Fax number:
)
)
)
array_walk_recursive is quite handy for that:
echo "\n------------ Table A ----------\n";
print_r($array_a);
function acallback(&$value, $key, $replace) {
if (key_exists($value, $replace)) {
$value = $replace[$value];
}
}
$status = array_walk_recursive($array_a, 'acallback', $array_b);
if ($status === false) {
throw new \Exception("array_walk failed");
}
echo "\n--------- Table A Modified ----------\n";
print_r($array_a);
the acallback function can be also anonymous and used this way:
echo "\n------------ Table A ----------\n";
print_r($array_a);
$status = array_walk_recursive($array_a, function(&$value, $key, $replace) {
if (key_exists($value, $replace)) {
$value = $replace[$value];
}
}
, $array_b);
if ($status === false) {
throw new \Exception("array_walk failed");
}
echo "\n--------- Table A Modified ----------\n";
print_r($array_a);

Finding duplicate values in a multidimensional array for Search Method

My Code was :
$data = array();
foreach ($table as $key => $var) {
$data[] = ['id' => $var->id, 'value' => $var->designation];
}
My Data array should be like this
array (
0 => array (
'id' => 27,
'value' => 'laravel',
),
1 => array (
'id' => 1,
'value' => 'laravel tester',
),
2 => array (
'id' => 22,
'value' => 'laravel developer',
),
3 => array (
'id' => 23,
'value' => 'laravel casts',
),
4 => array (
'id' => 24,
'value' => 'laravel developer',
),
)
I need only one value i tried all the php core library function output:
array (
0 =>
array (
'id' => 27,
'value' => 'laravel',
),
1 => array (
'id' => 1,
'value' => 'laravel tester',
),
2 => array (
'id' => 23,
'value' => 'laravel casts',
),
3 => array (
'id' => 24,
'value' => 'laravel developer',
),
)
Based on the name only i need to remove duplicate bacause in my search bar it shows repeated mode.
You can use array_unique, wich saves indexes, and get the result by array_intersect_key
$temp = array_unique(array_column($arr, 'value'));
$res = array_intersect_key($arr, $temp);
print_r($res);

Mongodb Search from Collection

I need to Search collection in mongodb having
'food_name' => 'fish'
and
'room_features' =>
array (
0 => 'Shower',
1 => 'Hairdryer',
),
I tried the following code. But the result is not-correct. I think multiple $eq is not allowed (same index in array).
array (
'$and' =>
array (
array (
'food' =>
array (
'$elemMatch' =>
array (
'food_name' =>
array (
'$eq' => 'fish',
),
),
),
),
array (
'room' =>
array (
'$elemMatch' =>
array (
'room_features' =>
array (
'$elemMatch' =>
array (
'$eq' => 'Shower'
'$eq' => 'Hairdryer'
),
),
'roomrate' =>
array (
'$eq' => new MongoInt32(2500),
),
),
),
),
),
)
Here is the document I need to search.
array (
'_id' => new MongoId("59670aca7fafd8342e3c9869"),
'subcat_name' => 'Test',
'place' => '',
'description' => '',
'created_date' => '1499970060',
'created_by' => 'Admin',
'openingtime' => '',
'closingtime' => '',
'hotel_class_id' => '594245f67fafd87e243c986a',
'hotel_type_id' => '594244177fafd884563c9869',
'latitude' => '0',
'longitude' => '0',
'dist_id' => '5911966a7fafd8c83c3c986a',
'cat_id' => '58fb230e7fafd883183c986d',
'featured' => '0',
'visited' => new MongoInt64(5),
'subcat_slug' => 'test-trivandrum-1',
'image' => NULL,
'food' =>
array (
0 =>
array (
'food_id' => '149992634012642164',
'region_id' => '5944ba947fafd883333c9869',
'food_name' => 'fish',
'type' => 'veg',
'rate' => '100',
),
1 =>
array (
'food_id' => '14999366891994980639',
'region_id' => '595c75c17fafd835173c986c',
'food_name' => 'curry',
'type' => 'veg',
'rate' => '1000',
),
),
'room' =>
array (
0 =>
array (
'room_id' => '14999346791721342880',
'roomtype' => 'DELUXE KING ROOM1',
'roomrate' => new MongoInt64(2500),
'image' => 'beach_icon33.png',
'room_features' =>
array (
0 => 'Shower',
1 => 'Hairdryer',
),
),
1 =>
array (
'room_id' => '14999346901389554873',
'roomtype' => 'DELUXE KING ROOM new',
'roomrate' => new MongoInt64(4000),
'image' => 'beach_icon34.png',
'room_features' =>
array (
0 => 'Shower',
1 => 'Bathrobe',
),
),
),
)
Please Give me an alternate way to search multiple item from array.
Thanks in advance.
I think if you want to query list you can add the list in query,
try this
{
"food" : {
"$elemMatch": {
"food_name" : "fish"
}
},
"room" : {
"$elemMatch": {
"room_features" : ["Shower", "Hairdryer"]
}
},
}
Hope this help.

Categories