I have a array like below (array 1) and I need to remove stdClass from it like in below array no. 2. Currently i'm doing it using a foreach loop, is there are better way to do that wthout looping?
Array no.1
array(3) {
[0] => object(stdClass)#169 (4) {
["id"] => string(2) "59"
["name"] => string(13) "test"
["email"] => string(21) "abc#abc.com"
["telephone"] => string(20) "898998989"
}
[1] => object(stdClass)#190 (4) {
["id"] => string(2) "58"
["name"] => string(13) "test"
["email"] => string(21) "abc#abc.com"
["telephone"] => string(8) "71877858"
}
[2] => object(stdClass)#193 (4) {
["id"] => string(2) "34"
["name"] => string(9) "test"
["email"] => string(22) "abc#abc.com"
["telephone"] => string(13) "3189028092139"
}
}
Array no.2
array(3) {
[0] => array(4) {
["id"] => string(2) "62"
["name"] => string(5) "test"
["email"] => string(22) "abc#abc.com"
["telephone"] => string(10) "898998989"
}
[1] => array(4) {
["id"] => string(2) "59"
["name"] => string(13) "test"
["email"] => string(21) "abc#abc.com"
["telephone"] => string(20) "71877858"
}
[2] => array(4) {
["id"] => string(2) "58"
["name"] => string(13) "test"
["email"] => string(21) "abc#abc.com"
["telephone"] => string(8) "3189028092139"
}
}
This is what I do (casting)
foreach($moderationContacts as $contact)
{
$contacts[] = (array)$contact;
}
try
$array = json_decode( json_encode($array), true);
EDIT:
I've tested this case, and it works:
$stdClass= new stdClass();
$stdClass->test = "foo";
$array = Array(
"a" => Array("b","c"),
"d" => $stdClass
);
$array = json_decode( json_encode($array), true);
var_dump($array);
OUTPUT
array
'a' =>
array
0 => string 'b' (length=1)
1 => string 'c' (length=1)
'd' =>
array
'test' => string 'foo' (length=3)
You can try
$array = array_map(function ($v) {
return (array) $v ; // convert to array
}, $array);
Or if this data is from json use
$array = json_decode($data,true);
Related
i'm new in twig. I had read the twig documentation but can nt find solution, as it is much easier in PHP. ((Trying two array in one loop.))
: i have two arrays one is {{ dump(render_categories) }}--
array(3) {
[0] => array(3) {
[0] => array(2) {
["category_id"] => string(2)
"65" ["name"] => string(11)
"Annivarsary"
} [1] => array(2) {
["category_id"] => string(2)
"67" ["name"] => string(9)
"Christmas"
} [2] => array(2) {
["category_id"] => string(2)
"66" ["name"] => string(8)
"Birthday"
}
} [1] => array(4) {
[0] => array(2) {
["category_id"] => string(2)
"61" ["name"] => string(6)
"Mother"
} [1] => array(2) {
["category_id"] => string(2)
"62" ["name"] => string(6)
"Sister"
} [2] => array(2) {
["category_id"] => string(2)
"60" ["name"] => string(8)
"Daughter"
} [3] => array(2) {
["category_id"] => string(2)
"63" ["name"] => string(10)
"Girlfriend"
}
} [2] => array(5) {
[0] => array(2) {
["category_id"] => string(2)
"73" ["name"] => string(8)
"Earrings"
} [1] => array(2) {
["category_id"] => string(2)
"72" ["name"] => string(22)
"Necklaces&Pendants"
} [2] => array(2) {
["category_id"] => string(2)
"71" ["name"] => string(5)
"Rings"
} [3] => array(2) {
["category_id"] => string(2)
"70" ["name"] => string(9)
"Bracelets"
} [4] => array(2) {
["category_id"] => string(2)
"69" ["name"] => string(6)
"Charms"
}
}
}
Second is {{ dump(load_steps) }} : (of different length)
array(3) {
[0] => array(4) {
["id"] => string(1)
"1" ["category_id"] => string(2)
"64" ["heading"] => string(21)
"What is the occasion?" ["status"] => string(1)
"1"
} [1] => array(4) {
["id"] => string(1)
"2" ["category_id"] => string(2)
"59" ["heading"] => string(5)
"For which Person" ["status"] => string(1)
"1"
} [2] => array(4) {
["id"] => string(1)
"3" ["category_id"] => string(2)
"68" ["heading"] => string(24)
"Type text heading 878787" ["status"] => string(1)
"1"
}
}
I'm trying to get Output like:
What is the occasion? --(get from second array)--
Annivarsary, Christmas, Birthday --(get from First array)--
For which Person? --(get from second array)--
Mother, Sister, Daughter, Girlfriend --(get from First array)--
------So, on-----
Any help is appreciated, ThankYou.
Wouldn't this do the trick ?
{% for key, step in load_steps %}
{{ step['heading'] }}
{% for category in render_categories[key] %}
{{ category['name'] }}
{% endfor %}
{% endfor %}
I have two arrays $men, $sub , I want want to sort them with this condition: if($men["titre"] == $sub["menuParent"]) I put the content of the second array that contains $sub["menuParent"] in the first one.
Case:
//Array1 ($menu)
array(3) {
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
}
["menu2"] => array(3) {
["titre"] => string(6) "Title3"
["lien"] => string(10) "google.com"
["order"] => string(1) "3"
}
["menu3"] => array(3) {
["titre"] => string(6) "Title1"
["lien"] => string(0) ""
["order"] => string(1) "1"
}
}
//Array2 ($submenu)
array(3) {
["submenu1"] => array(3) {
["titre"] => string(9) "SubTitle2"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "2"
}
["submenu2"] => array(3) {
["titre"] => string(9) "SubTitle3"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "3"
}
["submenu3"] => array(3) {
["titre"] => string(9) "SubTitle1"
["menuParent"] => string(6) "Title1"
["order"] => string(1) "1"
}
}
Here for exemple in:
array1 ["menu1"]["titre"] => string(6) "Title2"
array2 ["submenu1"]["menuParent"] => string(6) "Title2"
Now while ["menu1"]["titre"] & ["submenu1"]["menuParent"] values are the same "Title2", So I push :
["submenu1"] => array(3) {
["titre"] => string(9) "SubTitle2"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "2"
}
in:
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
}
as child like this:
array(3) {
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
["child"] => array(3) {
[0] => array(3) {
["titre"] => string(9) "SubTitle2"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "2"
}
}
So I do all this for whole elements`
Here is my try, but It push all the elements in ["menu2"] instead of ["submenu1"], ["submenu2"] IN ["menu1"] and ["submenu3"] IN ["menu3"]
My Try:
/*Injection submenu to Menu*/
foreach($menu as $men) {
foreach($submenu as $sub) {
if($men["titre"] == $sub["menuParent"]){
$key = key($menu);
$menu[$key]['child'][] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
//My array_push doesn't work also
array_push($menu, array($sub["titre"], $sub["menuParent"], $sub["order"]))
}
}
}
Result:
array(3) {
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
}
["menu2"] => array(4) {
["titre"] => string(6) "Title3"
["lien"] => string(10) "google.com"
["order"] => string(1) "3"
["child"] => array(3) {
[0] => array(3) {
[0] => string(9) "SubTitle2"
[1] => string(6) "Title2"
[2] => string(1) "2"
}
[1] => array(3) {
[0] => string(9) "SubTitle3"
[1] => string(6) "Title2"
[2] => string(1) "3"
}
[2] => array(3) {
[0] => string(9) "SubTitle1"
[1] => string(6) "Title1"
[2] => string(1) "1"
}
}
}
["menu3"] => array(3) {
["titre"] => string(6) "Title1"
["lien"] => string(0) ""
["order"] => string(1) "1"
}
}
Where I'm at fault, If anyone can explain to me why and if I took the good way.
The problem is where your picking the key up, you can use the foreach ( as $key->$value) method to get the key of each item (foreach works on it's own copy of the array and so picking the key from the original array isn't the same as the current one). Also you could use &$men, the & allows you to alter the original array element rather than create a new entry and trying to push the new element into the array...
foreach($menu as $key => $men) {
foreach($submenu as $key1=>$sub) {
if($men["titre"] == $sub["menuParent"]){
$menu[$key]['child'][$key1] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
}
}
}
gives...
Array
(
[menu1] => Array
(
[titre] => Title2
[lien] =>
[order] => 2
[child] => Array
(
[submenu1] => Array
(
[0] => SubTitle2
[1] => Title2
[2] => 2
)
[submenu2] => Array
(
[0] => SubTitle3
[1] => Title2
[2] => 3
)
)
)
[menu2] => Array
(
[titre] => Title3
[lien] => google.com
[order] => 3
)
[menu3] => Array
(
[titre] => Title1
[lien] =>
[order] => 1
[child] => Array
(
[submenu3] => Array
(
[0] => SubTitle1
[1] => Title1
[2] => 1
)
)
)
)
Update:
Code using &$men, note that in setting the data it no longer uses the key, but uses the $men variable.
foreach($menu as &$men) {
foreach($submenu as $key=>$sub) {
if($men["titre"] == $sub["menuParent"]){
$men['child'][$key] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
}
}
}
unset($men); // Good practice to ensure reference is reset
I'm working on a project with an API.
I gather data from a form and send this to their website.
The data is gathered through an array and then encoded to json format:
$pers_payload = array(
'gender' => 'Unknown', //or Male / Female
'first_name' => $_POST['billing_first_name'],
'family_name' => $_POST ['billing_last_name'],
'email' => $_POST['billing_email'],
'linked_as_contact_to_organization' => array(
array(
'organization_id' => $organization_id, // add the person as a contact to the newly created organization
'work_email' => $_POST['billing_email'],
'work_phone' => $_POST['billing_phone']
)
),
'visiting_address' => array(
'country_code' => 'NL'
), // can be extented with other address data
'postal_address' => array(
'country_code' => $_POST['billing_country']
) // can be extented with other address data
);
Then the post request.
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
I can also do a get request, this get request returns a multi dimensional array:
$tet = $SimplicateApi->makeApiCall('GET','/crm/person?q[first_name]=Kevin1');
If i do a var_dump on $tet i get this:
array(3) {
["data"] => array(2) {
[0] => array(11) {
["id"] => string(39)
"person:067af3bd2045824e62ac579e634623b8" ["interests"] => array(1) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}
}["simplicate_url"] => string(51)
"https://emark.simplicate.nl/crm/person/view?id=3552" ["avatar"] => array(2) {
["initials"] => string(2)
"Kt" ["color"] => string(7)
"#03e084"
}["linked_as_contact_to_organization"] => array(1) {
[0] => array(7) {
["id"] => string(46)
"contactperson:0f16f418f1845749c79bebf9e1e753e5" ["organization_id"] => string(45)
"organization:8632b86ba41637262e0871767f96f43e" ["name"] => string(9)
"testing12" ["work_email"] => string(24)
"ma#e-marketingsupport.nl" ["work_phone"] => string(8)
"06269684" ["work_mobile"] => string(8)
"06269684" ["interests"] => array(16) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:456e8b19c0079647" ["name"] => string(11)
"Twinkle 100"
}[1] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:a70e69b83382e85a" ["name"] => string(17)
"Bekend merk in NL"
}[2] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:ce50f1b5593ac180" ["name"] => string(15)
"Cross Border 30"
}[3] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f40eca1b281969d6" ["name"] => string(20)
"Meerdere vestigingen"
}[4] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:7435d7409a07cefb" ["name"] => string(26)
"Meer dan 100k in Duitsland"
}[5] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:cc072cea856ea23a" ["name"] => string(17)
"B2B leadgeneratie"
}[6] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}[7] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:9bbeb23d17283595" ["name"] => string(10)
"Exporteert"
}[8] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:97ed988af66b1abc" ["name"] => string(8)
"Debiteur"
}[9] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:3e31ffca2394bc38e1bb3149bee8b668" ["name"] => string(9)
"Marketing"
}[10] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:706fa5fa92c56081e1bb3149bee8b668" ["name"] => string(6)
"Amazon"
}[11] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:05f1a5da1c4c7df2e1bb3149bee8b668" ["name"] => string(3)
"Jur"
}[12] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:477554ee16a0c738e1bb3149bee8b668" ["name"] => string(11)
"Vertalingen"
}[13] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:f5f1b7512245a3b5e1bb3149bee8b668" ["name"] => string(5)
"Adres"
}[14] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:d1123dfaa0073c82e1bb3149bee8b668" ["name"] => string(4)
"GmbH"
}[15] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:7d3458131ea89afbe1bb3149bee8b668" ["name"] => string(3)
"Web"
}
}
}
}["gender"] => string(7)
"Unknown" ["first_name"] => string(6)
"Kevin1" ["family_name"] => string(7)
"testing" ["full_name"] => string(14)
"Kevin1 testing" ["email"] => string(24)
"ma#e-marketingsupport.nl" ["phone"] => string(8)
"06269684"
}[1] => array(11) {
["id"] => string(39)
"person:067af3bd2045824ea8c16e7ea0baf9d6" ["interests"] => array(1) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}
}["simplicate_url"] => string(51)
"https://emark.simplicate.nl/crm/person/view?id=3553" ["avatar"] => array(2) {
["initials"] => string(2)
"Kt" ["color"] => string(7)
"#dce1f3"
}["linked_as_contact_to_organization"] => array(1) {
[0] => array(7) {
["id"] => string(46)
"contactperson:f48fdcaaff0211e728a2e4ccf197900b" ["organization_id"] => string(45)
"organization:8632b86ba41637262e0871767f96f43e" ["name"] => string(9)
"testing12" ["work_email"] => string(24)
"ma#e-marketingsupport.nl" ["work_phone"] => string(8)
"06269684" ["work_mobile"] => string(8)
"06269684" ["interests"] => array(16) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:456e8b19c0079647" ["name"] => string(11)
"Twinkle 100"
}[1] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:a70e69b83382e85a" ["name"] => string(17)
"Bekend merk in NL"
}[2] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:ce50f1b5593ac180" ["name"] => string(15)
"Cross Border 30"
}[3] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f40eca1b281969d6" ["name"] => string(20)
"Meerdere vestigingen"
}[4] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:7435d7409a07cefb" ["name"] => string(26)
"Meer dan 100k in Duitsland"
}[5] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:cc072cea856ea23a" ["name"] => string(17)
"B2B leadgeneratie"
}[6] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}[7] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:9bbeb23d17283595" ["name"] => string(10)
"Exporteert"
}[8] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:97ed988af66b1abc" ["name"] => string(8)
"Debiteur"
}[9] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:3e31ffca2394bc38e1bb3149bee8b668" ["name"] => string(9)
"Marketing"
}[10] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:706fa5fa92c56081e1bb3149bee8b668" ["name"] => string(6)
"Amazon"
}[11] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:05f1a5da1c4c7df2e1bb3149bee8b668" ["name"] => string(3)
"Jur"
}[12] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:477554ee16a0c738e1bb3149bee8b668" ["name"] => string(11)
"Vertalingen"
}[13] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:f5f1b7512245a3b5e1bb3149bee8b668" ["name"] => string(5)
"Adres"
}[14] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:d1123dfaa0073c82e1bb3149bee8b668" ["name"] => string(4)
"GmbH"
}[15] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:7d3458131ea89afbe1bb3149bee8b668" ["name"] => string(3)
"Web"
}
}
}
}["gender"] => string(7)
"Unknown" ["first_name"] => string(6)
"Kevin1" ["family_name"] => string(7)
"testing" ["full_name"] => string(14)
"Kevin1 testing" ["email"] => string(24)
"ma#e-marketingsupport.nl" ["phone"] => string(8)
"06269684"
}
}["errors"] => NULL["debug"] => NULL
}
I dont want double values. For example if 'first_name' => $_POST['billing_first_name'],(in the $pers_payload variable) exists in $tet as a value. then dont run
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
else run:
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
I tried this from another question i asked:
foreach (array_keys($tet['data']) as $key) {
if (array_key_exists('first_name', $tet['data'][$key])
&& (strcasecmp($tet['data'][$key]['first_name'], 'Kevin1') == 0) ) {
echo "found key 'first_name' with value '" . $tet['data'][$key]['first_name'] . "'\n";
} else {
// perform your post request
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
}
This doesn't run the post request at all even if the value of first_name doesnt exist
I found the solution.
I accomplished this with a simple if statement:
if(strtolower($_POST['billing_last_name']) == strtolower($getOrg['data']['0']['linked_persons_contacts']['0']['family_name'])) {
// do nothing
} else {
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
How to extract this multi dimensional array from php. I am using codeigniter framework.
array(2) {
[0] => array(14)
{
["Key"] =>
string(48) "32;FaXmBQJ7/0MATwBVAFAAMAAxADIAOA==9;1261414580;"
["Type"] => string(12) "Store_Coupon"
["Code"] => string(8) "COUP0128"
["Coupon_Issuer"] => string(2) "11"
["Coupon_Reference_No"] => string(1) "1"
["Description"] => string(5) "test2"
["Price_Group"] => string(3) "ALL"
["Discount_Type"] => string(15) "Discount_Amount"
["Discount_Value"] => string(1) "0"
["Validation_Period_ID"] => string(2) "14"
["Validation_Description"] => string(15) "2016 membership"
["Starting_Date"] => string(10) "2016-01-01"
["Ending_Date"] => string(10) "2016-12-31"
["Status"] => string(8) "Disabled"
}
[1] => array(14)
{
["Key"] => string(48) "32;FaXmBQJ7/0MATwBVAFAATwBOADAAMg==9;1261413680;"
["Type"] => string(12) "Store_Coupon"
["Code"] => string(8) "COUPON02"
["Coupon_Issuer"] => string(2) "11"
["Coupon_Reference_No"] => string(1) "2"
["Description"] => string(6) "test 3"
["Price_Group"] => string(3) "ALL"
["Discount_Type"] => string(16) "Discount_Percent"
["Discount_Value"] => string(1) "0"
["Validation_Period_ID"] => string(2) "14"
["Validation_Description"] => string(15) "2016 membership"
["Starting_Date"] => string(10) "2016-01-01"
["Ending_Date"] => string(10) "2016-12-31"["Status"] => string(8) "Disabled"
}
}
I want to extract Coupon_Reference_No element. Thanks in advance
Just do a foreach and store that variable in an array. Supposing your aray is $array
$Coupon_Reference_No = array();
foreach($array as $key=>$value){
if($key == 'Coupon_Reference_No'){
$Coupon_Reference_No[] = $value;
}
}
You will get an array $Coupon_Reference_No as array("1","2")
I have this kind of array:
array(2) {
[1] => array(3) {
[3] => array(3) {
["data"] => array(3) {
["id"] => string(1) "3"
["depth"] => string(1) "1"
["parent_id"] => NULL
}
}
[4] => array(3) {
["data"] => array(3) {
["id"] => string(1) "4"
["depth"] => string(1) "1"
["parent_id"] => NULL
}
}
[2] => array(3) {
["data"] => array(3) {
["id"] => string(1) "2"
["depth"] => string(1) "1"
["parent_id"] => NULL
}
}
}
[2] => &array(3) {
[15] => array(3) {
["data"] => array(3) {
["id"] => string(2) "15"
["depth"] => string(1) "2"
["parent_id"] => string(1) "3"
}
}
[16] => array(3) {
["data"] => array(3) {
["id"] => string(2) "16"
["depth"] => string(1) "2"
["parent_id"] => string(1) "2"
}
}
[18] => array(3) {
["data"] => array(3) {
["id"] => string(2) "18"
["depth"] => string(1) "2"
["parent_id"] => string(1) "4"
}
}
}
}
First level means depth (1 hasn't child, 2 has parent without child, etc.). Order of array items is correct. I need obtain different array in the same order (of course in dependency of level).
array(6) {
[3] => array(3) {
["data"] => array(3) {
["id"] => string(1) "3"
["depth"] => string(1) "1"
["parent_id"] => NULL
}
}
[15] => array(3) {
["data"] => array(3) {
["id"] => string(1) "15"
["depth"] => string(1) "1"
["parent_id"] => 3
}
}
[4] => array(3) {
["data"] => array(3) {
["id"] => string(1) "4"
["depth"] => string(1) "1"
["parent_id"] => NULL
}
}
[18] => &array(3) {
["data"] => array(3) {
["id"] => string(2) "18"
["depth"] => string(1) "2"
["parent_id"] => string(1) "4"
}
}
[2] => array(3) {
["data"] => array(3) {
["id"] => string(2) "2"
["depth"] => string(1) "2"
["parent_id"] => NULL
}
}
[16] => array(3) {
["data"] => array(3) {
["id"] => string(2) "16"
["depth"] => string(1) "2"
["parent_id"] => string(1) "2"
}
}
}
**
First solution
**
I found solution but I do not know how make recursion from that. It works only with two levels. Third level is order badly. Variable $this->hierarchicalData contains first array.
$_sortedHierarchyData = array();
foreach ($this->hierarchicalData as $levelKey => $_level) {
foreach ($_level as $itemKey => $item) {
if (isset($this->hierarchicalData[$levelKey + 1])) {
$_sortedHierarchyData[$item['data']['id']] = $item;
$children = $this->searchChildrenFromLevelOfHieararchicalSortedData($item['data']['id'], $this->hierarchicalData[$levelKey + 1]);
$_sortedHierarchyData += $children;
}
}
}
function searchChildrenFromLevelOfHieararchicalSortedData($parent_id, $level)
{
$_children = array();
foreach ($level as $key => $item) {
if ($item['data']['parent_id'] === $parent_id) {
$_children[$key] = $item;
}
}
return $_children;
}