PHP Convert multidimensional array key to be array multidimensional value - php

I have problem formatting array to become fancytree format, below array output from db:
array(44) {
[0]=>
array(3) {
["id"]=>
string(2) "35"
["title"]=>
string(28) "ROOT1"
["path"]=>
string(28) "ROOT1"
}
[1]=>
array(3) {
["id"]=>
string(2) "36"
["title"]=>
string(27) "SUB_A"
["path"]=>
string(56) "ROOT1>SUB_A"
}
[2]=>
array(3) {
["id"]=>
string(2) "39"
["title"]=>
string(12) "SUB_A_1"
["path"]=>
string(69) "ROOT1>SUB_A>SUB_A_1"
}
[3]=>
array(3) {
["id"]=>
string(2) "37"
["title"]=>
string(28) "SUB_A_2"
["path"]=>
string(85) "ROOT1>SUB_A>SUB_A_2"
}
[4]=>
array(3) {
["id"]=>
string(2) "40"
["title"]=>
string(30) "SUB_A_3"
["path"]=>
string(87) "ROOT1>SUB_A>SUB_A_3"
}
[5]=>
array(3) {
["id"]=>
string(2) "43"
["title"]=>
string(19) "SUB_A_4"
["path"]=>
string(76) "ROOT1>SUB_A>SUB_A_4"
}
[6]=>
array(3) {
["id"]=>
string(2) "41"
["title"]=>
string(12) "SUB_A_5"
["path"]=>
string(69) "ROOT1>SUB_A>SUB_A_5"
}
[7]=>
array(3) {
["id"]=>
string(1) "1"
["title"]=>
string(20) "ROOT2"
["path"]=>
string(20) "ROOT2"
}
[8]=>
array(3) {
["id"]=>
string(2) "30"
["title"]=>
string(37) "ROOT3"
["path"]=>
string(37) "ROOT3"
}
[9]=>
array(3) {
["id"]=>
string(2) "34"
["title"]=>
string(21) "SUB_B"
["path"]=>
string(59) "ROOT3>SUB_B"
}
[10]=>
array(3) {
["id"]=>
string(2) "31"
["title"]=>
string(15) "SUB_C"
["path"]=>
string(53) "ROOT3>SUB_C"
}
I want to format values of path become structured array, here is what I try:
function format(array $data) : array
{
$single = [];
// format to single data
foreach ($data as $value) {
$single[] = $value['path'];
}
$result = [];
foreach ($single as $path) {
$parts = explode('>', $path);
$section = &$result;
$sectionName = '';
foreach ($parts as $part) {
$sectionName = $part;
if (array_key_exists($sectionName, $section) === FALSE) {
$section[$sectionName] = [];
}
$section = &$section[$sectionName];
}
}
return $result;
}
result:
array(3) {
["ROOT1"]=>
array(1) {
["SUB_A"]=>
array(5) {
["SUB_A_1"]=>
array(0) {
}
["SUB_A_2"]=>
array(0) {
}
["SUB_A_3"]=>
array(0) {
}
["SUB_A_4"]=>
array(0) {
}
["SUB_A_5"]=>
array(0) {
}
}
}
["ROOT2"]=>
array(0) {
}
["ROOT3"]=>
array(2) {
["SUB_B"]=>
array(0) {
}
["SUB_C"]=>
array(0) {
}
}
i need to include values of id and title to each path as expected result:
array(3) {
[0]=>
array(3) {
["title"]=>
string(5) "ROOT1"
["id"]=>
int(35)
["children"]=>
array(1) {
[0]=>
array(3) {
["title"]=>
string(5) "SUB_A"
["id"]=>
int(36)
["children"]=>
array(5) {
[0]=>
array(2) {
["title"]=>
string(7) "SUB_A_1"
["id"]=>
int(39)
}
[1]=>
array(2) {
["title"]=>
string(7) "SUB_A_2"
["id"]=>
int(37)
}
[2]=>
array(2) {
["title"]=>
string(7) "SUB_A_3"
["id"]=>
int(40)
}
[3]=>
array(2) {
["title"]=>
string(7) "SUB_A_4"
["id"]=>
int(43)
}
[4]=>
array(2) {
["title"]=>
string(7) "SUB_A_5"
["id"]=>
int(41)
}
}
}
}
}
[1]=>
array(2) {
["title"]=>
string(5) "ROOT2"
["id"]=>
int(1)
}
[2]=>
array(3) {
["title"]=>
string(5) "ROOT3"
["id"]=>
int(30)
["children"]=>
array(2) {
[0]=>
array(2) {
["title"]=>
string(5) "SUB_B"
["id"]=>
int(34)
}
[1]=>
array(2) {
["title"]=>
string(5) "SUB_C"
["id"]=>
int(31)
}
}
}
}
I would be grateful for any help you are able to provide.

Here's how I would do it. Test
I used arrow functions to reduce the amount of lines and there are inline comments to explain what I did.
<?php
$data = [["id"=> "35", "title"=> "ROOT1", "path"=> "ROOT1"], ["id"=> "36", "title"=> "SUB_A", "path"=> "ROOT1>SUB_A"], ["id"=> "37", "title"=> "SUB_A_1", "path"=> "ROOT1>SUB_A>SUB_A_1"], ["id"=> "38", "title"=> "SUB_A_2", "path"=> "ROOT1>SUB_A>SUB_A_2"], ["id"=> "39", "title"=> "SUB_A_3", "path"=> "ROOT1>SUB_A>SUB_A_3"], ["id"=> "40", "title"=> "SUB_A_4", "path"=> "ROOT1>SUB_A>SUB_A_4"], ["id"=> "41", "title"=> "SUB_A_5", "path"=> "ROOT1>SUB_A>SUB_A_5"], ["id"=> "1", "title"=> "ROOT2", "path"=> "ROOT2"], ["id"=> "30", "title"=> "ROOT3", "path"=> "ROOT3"], ["id"=> "34", "title"=> "SUB_B", "path"=> "ROOT3>SUB_B"], ["id"=> "31", "title"=> "SUB_C", "path"=> "ROOT3>SUB_C"]];
// The path is defaulted to an empty string, so the regex would return the highest level elements only because they don't have any `>`
function get_children($data, $path = '')
{
$results = array_values(array_filter($data, fn($item) => preg_match("/^{$path}[^>]+$/", $item['path'])));
// This regex, uses the passed path (e.g ROOT1) and searches for elements with anything but another `>`, because `>` signifies an inner directory.
// So this would return elements with a path like `ROOT1>SUB_A, ROOT1>SUB_B` but not `ROOT1>SUB_A>SUB_A_1`
foreach($results as $i => $result) {
// then I iterate through the result to get the element's children
$children = array_values(get_children($data, "{$result['path']}>"));
if (count($children)) $results[$i]['children'] = $children;
unset($results[$i]['path']); // Removing the path key from the array
}
return $results;
}
$format = fn($data) => get_children($data);
print_r($format($data));

Related

How to output all flights in HTML with this nested array?

Just want to output the array I get using: https://github.com/dahabtours/amadeus-php-sdk
$date_checkin = '2019-07-16';
$date_checkout = '2019-07-18';
require('AmadeusClient.php');
$amadeus_api = new AmadeusDahabtours\SelfServiceApiClient('CLIENT_ID','CLIENT_SECRET');
# Flight Low-fare Search
$flight_offers = $amadeus_api->lowFares([
'origin' => 'AMS',
'destination' => 'BCN',
'departureDate' => $date_checkin,
'returnDate' => $date_checkout,
'nonStop' => true,
'currency' => 'EUR',
'max' => 2,
]);
I want the output to be something like this:
<div class="flights">
<div class="flight-1">VALUES</div>
<div class="flight-2">VALUES</div>
<div class="flight-3">VALUES</div>
</div>
Array:
array(5) {
["success"]=>
bool(true)
["msgs"]=>
array(0) {
}
["http_code"]=>
int(200)
["response_text"]=>
string(2597) "{"data":[{"type":"flight-offer","id":"1562680375384--1832628087","offerItems":[{"services":[{"segments":[{"flightSegment":{"departure":{"iataCode":"AMS","terminal":"3","at":"2019-07-16T07:00:00+02:00"},"arrival":{"iataCode":"BCN","terminal":"1","at":"2019-07-16T09:20:00+02:00"},"carrierCode":"IB","number":"5224","aircraft":{"code":"320"},"operating":{"carrierCode":"VY","number":"5224"},"duration":"0DT2H20M"},"pricingDetailPerAdult":{"travelClass":"ECONOMY","fareClass":"V","availability":4,"fareBasis":"VRTNVY"}}]},{"segments":[{"flightSegment":{"departure":{"iataCode":"BCN","terminal":"1","at":"2019-07-18T12:05:00+02:00"},"arrival":{"iataCode":"AMS","at":"2019-07-18T14:30:00+02:00"},"carrierCode":"IB","number":"5685","aircraft":{"code":"320"},"operating":{"carrierCode":"VY","number":"5685"},"duration":"0DT2H25M"},"pricingDetailPerAdult":{"travelClass":"ECONOMY","fareClass":"A","availability":4,"fareBasis":"ARTNVY"}}]}],"price":{"total":"209.09","totalTaxes":"74.09"},"pricePerAdult":{"total":"209.09","totalTaxes":"74.09"}}]},{"type":"flight-offer","id":"1562680375384--510820791","offerItems":[{"services":[{"segments":[{"flightSegment":{"departure":{"iataCode":"AMS","at":"2019-07-16T07:00:00+02:00"},"arrival":{"iataCode":"BCN","terminal":"1","at":"2019-07-16T09:20:00+02:00"},"carrierCode":"VY","number":"8318","aircraft":{"code":"320"},"operating":{"carrierCode":"VY","number":"8318"},"duration":"0DT2H20M"},"pricingDetailPerAdult":{"travelClass":"ECONOMY","fareClass":"X","availability":6,"fareBasis":"XRTVY"}}]},{"segments":[{"flightSegment":{"departure":{"iataCode":"BCN","terminal":"1","at":"2019-07-18T12:05:00+02:00"},"arrival":{"iataCode":"AMS","at":"2019-07-18T14:30:00+02:00"},"carrierCode":"VY","number":"8302","aircraft":{"code":"321"},"operating":{"carrierCode":"VY","number":"8302"},"duration":"0DT2H25M"},"pricingDetailPerAdult":{"travelClass":"ECONOMY","fareClass":"P","availability":8,"fareBasis":"PRTVY"}}]}],"price":{"total":"209.09","totalTaxes":"74.09"},"pricePerAdult":{"total":"209.09","totalTaxes":"74.09"}}]}],"dictionaries":{"carriers":{"VY":"VUELING AIRLINES","IB":"IBERIA"},"currencies":{"EUR":"EURO"},"aircraft":{"320":"AIRBUS INDUSTRIE A320-100/200","321":"AIRBUS INDUSTRIE A321"},"locations":{"AMS":{"subType":"AIRPORT","detailedName":"SCHIPHOL AIRPORT"},"BCN":{"subType":"AIRPORT","detailedName":"AIRPORT"}}},"meta":{"links":{"self":"https://test.api.amadeus.com/v1/shopping/flight-offers?origin=AMS&destination=BCN&departureDate=2019-07-16&returnDate=2019-07-18&adults=1&nonStop=true¤cy=EUR&max=2"},"currency":"EUR","defaults":{"adults":1}}}"
["response"]=>
array(3) {
["data"]=>
array(2) {
[0]=>
array(3) {
["type"]=>
string(12) "flight-offer"
["id"]=>
string(25) "1562680375384--1832628087"
["offerItems"]=>
array(1) {
[0]=>
array(3) {
["services"]=>
array(2) {
[0]=>
array(1) {
["segments"]=>
array(1) {
[0]=>
array(2) {
["flightSegment"]=>
array(7) {
["departure"]=>
array(3) {
["iataCode"]=>
string(3) "AMS"
["terminal"]=>
string(1) "3"
["at"]=>
string(25) "2019-07-16T07:00:00+02:00"
}
["arrival"]=>
array(3) {
["iataCode"]=>
string(3) "BCN"
["terminal"]=>
string(1) "1"
["at"]=>
string(25) "2019-07-16T09:20:00+02:00"
}
["carrierCode"]=>
string(2) "IB"
["number"]=>
string(4) "5224"
["aircraft"]=>
array(1) {
["code"]=>
string(3) "320"
}
["operating"]=>
array(2) {
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "5224"
}
["duration"]=>
string(8) "0DT2H20M"
}
["pricingDetailPerAdult"]=>
array(4) {
["travelClass"]=>
string(7) "ECONOMY"
["fareClass"]=>
string(1) "V"
["availability"]=>
int(4)
["fareBasis"]=>
string(6) "VRTNVY"
}
}
}
}
[1]=>
array(1) {
["segments"]=>
array(1) {
[0]=>
array(2) {
["flightSegment"]=>
array(7) {
["departure"]=>
array(3) {
["iataCode"]=>
string(3) "BCN"
["terminal"]=>
string(1) "1"
["at"]=>
string(25) "2019-07-18T12:05:00+02:00"
}
["arrival"]=>
array(2) {
["iataCode"]=>
string(3) "AMS"
["at"]=>
string(25) "2019-07-18T14:30:00+02:00"
}
["carrierCode"]=>
string(2) "IB"
["number"]=>
string(4) "5685"
["aircraft"]=>
array(1) {
["code"]=>
string(3) "320"
}
["operating"]=>
array(2) {
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "5685"
}
["duration"]=>
string(8) "0DT2H25M"
}
["pricingDetailPerAdult"]=>
array(4) {
["travelClass"]=>
string(7) "ECONOMY"
["fareClass"]=>
string(1) "A"
["availability"]=>
int(4)
["fareBasis"]=>
string(6) "ARTNVY"
}
}
}
}
}
["price"]=>
array(2) {
["total"]=>
string(6) "209.09"
["totalTaxes"]=>
string(5) "74.09"
}
["pricePerAdult"]=>
array(2) {
["total"]=>
string(6) "209.09"
["totalTaxes"]=>
string(5) "74.09"
}
}
}
}
[1]=>
array(3) {
["type"]=>
string(12) "flight-offer"
["id"]=>
string(24) "1562680375384--510820791"
["offerItems"]=>
array(1) {
[0]=>
array(3) {
["services"]=>
array(2) {
[0]=>
array(1) {
["segments"]=>
array(1) {
[0]=>
array(2) {
["flightSegment"]=>
array(7) {
["departure"]=>
array(2) {
["iataCode"]=>
string(3) "AMS"
["at"]=>
string(25) "2019-07-16T07:00:00+02:00"
}
["arrival"]=>
array(3) {
["iataCode"]=>
string(3) "BCN"
["terminal"]=>
string(1) "1"
["at"]=>
string(25) "2019-07-16T09:20:00+02:00"
}
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "8318"
["aircraft"]=>
array(1) {
["code"]=>
string(3) "320"
}
["operating"]=>
array(2) {
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "8318"
}
["duration"]=>
string(8) "0DT2H20M"
}
["pricingDetailPerAdult"]=>
array(4) {
["travelClass"]=>
string(7) "ECONOMY"
["fareClass"]=>
string(1) "X"
["availability"]=>
int(6)
["fareBasis"]=>
string(5) "XRTVY"
}
}
}
}
[1]=>
array(1) {
["segments"]=>
array(1) {
[0]=>
array(2) {
["flightSegment"]=>
array(7) {
["departure"]=>
array(3) {
["iataCode"]=>
string(3) "BCN"
["terminal"]=>
string(1) "1"
["at"]=>
string(25) "2019-07-18T12:05:00+02:00"
}
["arrival"]=>
array(2) {
["iataCode"]=>
string(3) "AMS"
["at"]=>
string(25) "2019-07-18T14:30:00+02:00"
}
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "8302"
["aircraft"]=>
array(1) {
["code"]=>
string(3) "321"
}
["operating"]=>
array(2) {
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "8302"
}
["duration"]=>
string(8) "0DT2H25M"
}
["pricingDetailPerAdult"]=>
array(4) {
["travelClass"]=>
string(7) "ECONOMY"
["fareClass"]=>
string(1) "P"
["availability"]=>
int(8)
["fareBasis"]=>
string(5) "PRTVY"
}
}
}
}
}
["price"]=>
array(2) {
["total"]=>
string(6) "209.09"
["totalTaxes"]=>
string(5) "74.09"
}
["pricePerAdult"]=>
array(2) {
["total"]=>
string(6) "209.09"
["totalTaxes"]=>
string(5) "74.09"
}
}
}
}
}
["dictionaries"]=>
array(4) {
["carriers"]=>
array(2) {
["VY"]=>
string(16) "VUELING AIRLINES"
["IB"]=>
string(6) "IBERIA"
}
["currencies"]=>
array(1) {
["EUR"]=>
string(4) "EURO"
}
["aircraft"]=>
array(2) {
[320]=>
string(29) "AIRBUS INDUSTRIE A320-100/200"
[321]=>
string(21) "AIRBUS INDUSTRIE A321"
}
["locations"]=>
array(2) {
["AMS"]=>
array(2) {
["subType"]=>
string(7) "AIRPORT"
["detailedName"]=>
string(16) "SCHIPHOL AIRPORT"
}
["BCN"]=>
array(2) {
["subType"]=>
string(7) "AIRPORT"
["detailedName"]=>
string(7) "AIRPORT"
}
}
}
["meta"]=>
array(3) {
["links"]=>
array(1) {
["self"]=>
string(169) "https://test.api.amadeus.com/v1/shopping/flight-offers?origin=AMS&destination=BCN&departureDate=2019-07-16&returnDate=2019-07-18&adults=1&nonStop=true¤cy=EUR&max=2"
}
["currency"]=>
string(3) "EUR"
["defaults"]=>
array(1) {
["adults"]=>
int(1)
}
}
}
}
Now i have this:
foreach ( $flight_offers as $flights ) {
if (is_array( $flights ) ){
foreach ( $flights as $value ) {
echo $value;
}
} else {
echo $flights;
}
}

PHP push new data into given array

I may seem having difficulty in understanding on how array works. So result, I can't work on the issue.
foreach($results as $key => $value){
$product_key = array(
'key' => $key
);
array_push($results, $product_key);
}
var_dump($results); exit;
Expected Output
array(2) {
[0]=>
object(stdClass)#21 (4) {
["items_id"]=>
string(1) "1"
["item_name"]=>
string(6) "laptop"
["price"]=>
string(5) "20000"
["quantity"]=>
string(2) "10"
["key"]=>
int(0)
}
[1]=>
object(stdClass)#22 (4) {
["items_id"]=>
string(1) "2"
["item_name"]=>
string(10) "smartphone"
["price"]=>
string(5) "10000"
["quantity"]=>
string(3) "200"
["key"]=>
int(1)
}
Unexpected Output
array(4) {
[0]=>
object(stdClass)#21 (4) {
["items_id"]=>
string(1) "1"
["item_name"]=>
string(6) "laptop"
["price"]=>
string(5) "20000"
["quantity"]=>
string(2) "10"
}
[1]=>
object(stdClass)#22 (4) {
["items_id"]=>
string(1) "2"
["item_name"]=>
string(10) "smartphone"
["price"]=>
string(5) "10000"
["quantity"]=>
string(3) "200"
}
[2]=>
array(1) {
["key"]=>
int(0)
}
[3]=>
array(1) {
["key"]=>
int(1)
}
}
You push new value (which is array) to the end of existsing array, what do you expect then?
If you want to modify current interated array value use this approach:
foreach($results as $key => $value) {
// use `->` as `$value` is object
$value->key = $key;
}
var_dump($results); exit;

Sorting arrays and arrays of arrays using a key parameter

I have array of students which is fetched in a variable $arraytosort and looks like
array(5027) { [0]=> array(5) { ["_id"]=> object(MongoId)#22 (1) { ["$id"]=> string(24) "58131c7799fbad4c1d000202" } ["fullname"]=> string(19) "David Robert Lara" ["firstmiddle"]=> string(12) "David Robert" ["firstlast"]=> string(14) "David Lara" ["student"]=> array(50) { ["_id"]=> object(MongoId)#23 (1) { ["$id"]=> string(24) "58131c7799fbad4c1d000202" } ["student_id"]=> float(2) ["registration_temp_perm_no"]=> string(1) "1" ["roll_no"]=> float(1) ["admission_date"]=> string(10) "01/07/2016" ["first_name"]=> string(7) "David" ["middle_name"]=> string(4) "Robert" ["last_name"]=> string(6) "Lara" ["dob"]=> string(10) "12/03/1981" ["gender"]=> string(6) "Female" ["blood_group"]=> string(2) "A+" ["birth_place"]=> string(11) "Sadar Bazar" ["nationality"]=> string(6) "Indian" ["language"]=> string(7) "English" ["religion"]=> string(8) "Agnostic" ["address_line1"]=> string(20) "4148 Hazelcrest Hill" ["address_line2"]=> string(20) "22883 Memorial Place" ["city"]=> string(11) "Sadar Bazar" ["state"]=> string(13) "Uttar Pradesh" ["pincode"]=> string(6) "190010" ["country"]=> string(5) "India" ["phone1"]=> string(10) "9039180419" ["phone2"]=> string(10) "7681559402" ["email"]=> string(24) "educianstudent#gmail.com" ["is_sms_enabled"]=> string(3) "Yes" ["is_active"]=> int(1) ["has_finished"]=> int(0) ["student_category"]=> string(1) "5" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["Biometric_ID"]=> string(1) "1" ["siblings"]=> string(14) "Cynthia Taylor" ["guardian_name"]=> string(14) "Cynthia Taylor" ["guardian_occupation"]=> string(13) "Senior Editor" ["guardian_qualification"]=> string(20) "Research Assistant I" ["guardian_email_id"]=> string(23) "educianparent#gmail.com" ["gaurdain_contact_details"]=> string(10) "9419513603" ["guardian_relationship"]=> string(6) "Father" ["height"]=> string(3) "4.9" ["weight"]=> string(4) "34.9" ["allergies"]=> string(0) "" ["batch"]=> int(2) ["academicyear"]=> string(4) "2015" ["batchhistory"]=> array(1) { [0]=> array(5) { ["batchid"]=> float(2) ["academic_year"]=> string(4) "2015" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["sequenceno"]=> int(1) ["courseId"]=> object(MongoId)#24 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } } } ["uploads"]=> array(1) { ["profile_pic"]=> string(39) "58131c7799fbad4c1d000202schoolgirl2.jpg" } ["created_at"]=> NULL ["updated_at"]=> string(0) "" ["routearray"]=> array(2) { [0]=> array(5) { ["routeid"]=> int(2) ["academicyear"]=> string(4) "2016" ["current"]=> int(0) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "09/28/2016" } [1]=> array(5) { ["routeid"]=> int(3) ["academicyear"]=> string(4) "2016" ["current"]=> int(1) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "11/17/2016" } } ["HostelAlloted"]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#25 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#26 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } ["HostelAllotmentHistory"]=> array(1) { [0]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#27 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#28 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } } ["courseId"]=> object(MongoId)#29 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } } }
...
...
}
I have written below function to sort these students on the basis of key value given in $key so that the array will contain the similar results on top/first.
$sortedarray = array();
$sortedarray = $this->multiSort(
$arraytosort,
$key,
'fullname',
'firstmiddle',
'firstlast',
'first_name',
'middle_name',
'last_name',
'roll_no',
'email'
);
function multiSort()
{
$args = func_get_args();
$c = count($args);
if ($c < 2) {
return false;
}
$key= $args[1];
$array = array_splice($args, 0, 1);
$array = $array[0];
usort($array, function($a) use($args) {
$i = 0;
$c = count($args);
$cmp = 0;
while($cmp == 0 && $i < $c)
{
$cmp = stripos($a[$args[$i] ],$key);
$i++;
}
return $cmp;
});
return $array;
}
It throws error message "Undefined variable: key".
Please help!!!

PHP Nested Array convert into flat array

I have a problem to transform my hierarchical array like this:
array(
[0]=>
array(3) {
["id"]=>
int(2353011010)
["name"]=>
string(17) "LEDER ACCESSOIRES"
["order"]=>
int(15)
}
[1]=>
array(3) {
["id"]=>
int(2371475010)
["name"]=>
string(15) "SPORT AUFKLEBER"
["order"]=>
int(25)
}
[2]=>
array(4) {
["id"]=>
int(2563635010)
["name"]=>
string(17) "KENNZEICHENHALTER"
["order"]=>
int(10)
["children"]=>
array(6) {
[0]=>
array(4) {
["id"]=>
int(3854259010)
["name"]=>
string(9) "EDELSTAHL"
["order"]=>
int(92)
["children"]=>
array(2) {
[0]=>
array(3) {
["id"]=>
int(20878056010)
["name"]=>
string(5) "test1"
["order"]=>
int(1)
}
}
}
[1]=>
array(3) {
["id"]=>
int(3854260010)
["name"]=>
string(5) "CHROM"
["order"]=>
int(91)
}
}
[3]=>
array(4) {
["id"]=>
int(19754330010)
["name"]=>
string(30) "SCHALTMANSCHETTEN CARBON OPTIK"
["order"]=>
int(3)
}
}
)
Into a flat ones like this:
array(
[0]=>
array(3) {
["id"]=>
int(2353011010)
["name"]=>
string(17) "LEDER ACCESSOIRES"
["order"]=>
int(15)
}
[1]=>
array(3) {
["id"]=>
int(2371475010)
["name"]=>
string(15) "SPORT AUFKLEBER"
["order"]=>
int(25)
}
[2]=>
array(3) {
["id"]=>
int(2563635010)
["name"]=>
string(17) "KENNZEICHENHALTER"
["order"]=>
int(10)
}
[3]=>
array(4) {
["id"]=>
int(3854259010)
["name"]=>
string(9) "EDELSTAHL"
["order"]=>
int(92),
["parentId"]=> 2563635010
}
[4]=>
array(4) {
["id"]=>
int(20878056010)
["name"]=>
string(5) "test1"
["order"]=>
int(1),
["parentId"]=> 2563635010
}
[5]=>
array(4) {
["id"]=>
int(3854260010)
["name"]=>
string(5) "CHROM"
["order"]=>
int(91),
["parentId"]=> 2563635010
}
[6]=>
array(4) {
["id"]=>
int(19754330010)
["name"]=>
string(30) "SCHALTMANSCHETTEN CARBON OPTIK"
["order"]=>
int(3)
}
)
The children antities should be removed and every child element should get a parentId entity of the higher level id. I need this solution for transfering into DB.
thx
Now, I have create a "temporary" method that works for me, but is noch flexible to use:
function recursive($categories) {
foreach ($categories as $value) {
$result[$value->id]['id'] = $value->id;
$result[$value->id]['name'] = $value->name;
$result[$value->id]['order'] = $value->order;
$result[$value->id]['parentId'] = 0;
if(isset($value->children)) {
$parentId = $value->id;
foreach($value->children as $value2) {
$result[$value2->id]['id'] = $value2->id;
$result[$value2->id]['name'] = $value2->name;
$result[$value2->id]['parentId'] = $parentId;
if(isset($value2->children)) {
$parentId = $value2->id;
foreach($value2->children as $value3) {
$result[$value3->id]['id'] = $value3->id;
$result[$value3->id]['name'] = $value3->name;
$result[$value3->id]['parentId'] = $parentId;
}
}
}
}
}
return $result;
}
Do anybody know a recursive solution for this method?

getting a unique values from array

I currently have an array that looks like the one below but I only want to display show results in the array from which are unique (description), I presume with array_unique? but I keep getting the same results?
Here is my array:
$taglist = array(5) {
[0]=> array(5) {
["id"]=> string(2) "27"
["page_id"]=> string(2) "18"
["description"]=> string(10) "Web Design"
["slug"]=> string(10) "web-design"
["visibility"]=> string(7) "visible"
}
[1]=> array(5) {
["id"]=> string(2) "29"
["page_id"]=> string(2) "18"
["description"]=> string(3) "Tutorials"
["slug"]=> string(3) "tutorials"
["visibility"]=> string(7) "visible"
}
[2]=> array(5) {
["id"]=> string(2) "31"
["page_id"]=> string(2) "21"
["description"]=> string(3) "tag"
["slug"]=> string(3) "tag"
["visibility"]=> string(7) "visible"
}
[3]=> array(5) {
["id"]=> string(2) "32"
["page_id"]=> string(2) "21"
["description"]=> string(10) "Web Design"
["slug"]=> string(10) "web-design"
["visibility"]=> string(7) "visible"
}
}
Here is my while:
$items = array();
$results = $taglist;
foreach ($results as $result)
{
$items[]= $result['description'];
$items = array_unique($items);
}
echo '<ul>';
while ($tag_item = current($items))
{
echo '<li>'.$tag_item['description'].'</li>';
next($items);
}
echo '</ul>';
$taglist = array(
0 => array('description'=>'one'),
1 => array('description'=>'two'),
2 => array('description'=>'one'),
3 => array('description'=>'three'),
4 => array('description'=>'one'),
);
// echo var_export($taglist, 1); // uncomment to see the diff vs var_dump()
foreach($taglist as $tag){
$new[] = $tag['description'];
}
var_dump(array_unique($new));

Categories