I am trying to generate a multidimensional array. However, some of the levels could be excluded based on the situation. I am trying to accomplish this without excessive use of if statements. I know you can do it with those. So I would like to generate a path for it and execute it.
I set the following variables, before the loop.
//Count countries and decide to show it
$country = count($countries) > 1 ? '[(string) $box->A0_NAME]' : '';
//If city is needed, $byCity is passed to function externaly
$city = $byCity ? '[(string) $box->ZIP]' : '';
Now enter the loop, and do some things, now comes where it breaks for me. $box is passed from foreach loop and contains data to be phrased. $return is return multidimensional array being generated.
//Check if country needed and exists
if(!empty($country)) {
if(!array_key_exists($box->A0_NAME, $return)) {
$return[$box->A0_NAME] = array();
}
}
//Check if city needed, if so the add it, and we break here.
if($byCity) {
if(!array_key_exists($box->ZIP, ${'return'.$country})) {
${'return'.$country.'['.$box->ZIP.']'} = array();
}
}
The initial part works and returns everything as expected. I cannot get the second part to work. In short, I generate array which has countries, then in each country, there should be an array of ZIP codes. When checking, I get no errors, but the $return is always empty of ZIP codes.
Is this even possible? Or should I just go back and rethink it all? 😀
Sample box object from source:
<LOCATION>
<ZIP>96146</ZIP>
<NAME>Test place</NAME>
<TYPE>0</TYPE>
<A0_NAME>EE</A0_NAME>
<A1_NAME>Harju maakond</A1_NAME>
<A2_NAME>Tallinn</A2_NAME>
<A3_NAME>Lasnamäe linnaosa</A3_NAME>
<A4_NAME/>
<A5_NAME>Foobox tn</A5_NAME>
<A6_NAME/>
<A7_NAME>99</A7_NAME>
<A8_NAME/>
<X_COORDINATE>24.000000</X_COORDINATE>
<Y_COORDINATE>59.000000</Y_COORDINATE>
<SERVICE_HOURS/>
<TEMP_SERVICE_HOURS/>
<TEMP_SERVICE_HOURS_UNTIL/>
<TEMP_SERVICE_HOURS_2/>
<TEMP_SERVICE_HOURS_2_UNTIL/>
<COMMENT_EST/>
<COMMENT_ENG/>
<COMMENT_RUS/>
<COMMENT_LAV/>
<COMMENT_LIT/>
<MODIFIED>2019-10-14T06:15:34.560+03:00</MODIFIED>
</LOCATION>
Sample output as requested. Note tho, the point of the whole script is to make it dynamic. If it is requested, it skips splitting into countries or zip codes or even both.
$return = array (
"EE" => array (
"112233" => arrayOfData(), //Each Zip would have array of multiple entries
"981874" => arrayOfData(),
"608765" => arrayOfData(),
"346885" => arrayOfData(),
...
),
"UK" => array (
"112233" => arrayOfData(),
"981874" => arrayOfData(),
"608765" => arrayOfData(),
"346885" => arrayOfData(),
...
),
...
);
It should also be able to return:
$return = array (
"112233" => arrayOfData(),
"981874" => arrayOfData(),
"608765" => arrayOfData(),
"346885" => arrayOfData(),
...
);
$return = array (
"EE" => arrayOfData(),
"UK" => arrayOfData(),
...
);
The server is running PHP version 5.3.17
2
Answers
Here is one approach:
https://php.net/simplexml.examples-basic
Instead of processing the data through a function, just populate and format the data. This will result in faster processing and much more simple / clean code.
Output: