Collect 3rd level data dependent on the first two - php

I usually don't like to give up but it feels like this could be more efficient, but for the life of me I can't see it.
I have a Webservice from which I can request the Models, Makes and Types of Cars.
But to be able to request the Types I need the Makes and to be able to request the Makes I need the Models...
Because I need ALL Types (last level) to iterate over them and use them in a different piece of functionality I need to make these 3 steps.
The only problem: my current code takes about 3 minutes to finish and it feels incorrect because of Big O..
If you guys could make this more efficient I would be forever gratefull:
$cars = [];
$makes = getMakes()['makes']['Make'];
$models = [];
$types = [];
$allTypes = [];
$timeStart = microtime(true);
// Takes waaaaaay too long
foreach ($makes as $key => $make) {
$cars[$key]['makeName'] = $make['makename'];
$cars[$key]['makeCode'] = $make['makecode'];
$models = getModels($make['makecode'])['models']['Model'];
foreach ($models as $subKey => $model) {
if (is_array($model)) {
if (array_key_exists('modelname', $model)) {
$cars[$key]['models'][$subKey]['modelName'] = $model['modelname'];
}
if (array_key_exists('modelcode', $model)) {
$cars[$key]['models'][$subKey]['modelCode'] = $model['modelcode'];
}
$types = getTypes($model['modelcode']);
foreach ($types as $ssKey => $type) {
if (is_array($type)) {
if (array_key_exists('typecode', $type)) {
$cars[$key]['models'][$subKey]['types'][$ssKey]['typeCode'] = $type['typecode'];
$allTypes[] = $type['typecode'];
}
}
}
}
}
}
$timeEnd = microtime(true);
print_r(($timeEnd - $timeStart) / 60); // 3.228285531203 (~3 minutes)
// print_r($cars);

Related

A better way to flatten an array in PHP

I require the array keys to be maintained, with a number appended to the key to make it unique when flattening an array.
Example input:
$array = array(
array("name"=>"bob", "age"=>32, "third_param"=>"something"),
array("name"=>"ted", "age"=>57, "third_param"=>"something else"),
array("name"=>"ned", "age"=>103, "third_param"=>"another something"),
);
Required output:
$array = array(
"name-1"=>"bob",
"age-1"=>32,
"third_param-1"=>"something",
"name-2"=>"ted",
"age-2"=>57,
"third_param-2"=>"something else",
"name-3"=>"ned",
"age-3"=>103,
"third_param-3"=>"another something"
);
I was able to figure out how to do it, but my solution is slow and messy. There has got to be a better way.
Here's my current function:
function flatten_array($array, $flat = array()) {
foreach($array as $k=>$v){
$k = strval($k);
if(!is_array($v)){
$i = 0;
while(true){
$i++;
$key = $k."-".strval($i);
if(!isset($flat[$key])) break;
}
$flat[$key] = $v;
}else{
$flat = flatten_array($v, $flat);
}
}
return $flat;
}
Here's an example usage: http://3v4l.org/6QVj0/
(Hit the execute button, and then have a look at the "Performance" tab.
This takes a long time and has timed-out on me when testing it on real data, however it does produce the result I need. What can I do to this to make it quicker and not take up so much memory?
$ar = array();
foreach ($array as $i => $items) {
foreach ($items as $key => $value) {
$ar[$key.'-'.($i+1)] = $value;
}
}

How to reference cells with specific condition in php multidimensional array

I got an array like this:
$array[0][name] = "Axel";
$array[0][car] = "Pratzner";
$array[0][color] = "black";
$array[1][name] = "John";
$array[1][car] = "BMW";
$array[1][color] = "black";
$array[2][name] = "Peggy";
$array[2][car] = "VW";
$array[2][color] = "white";
I would like to do something like "get all names WHERE car = bmw AND color = white"
Could anyone give advice on how the PHP spell would look like?
function getWhiteBMWs($array) {
$result = array();
foreach ($array as $entry) {
if ($entry['car'] == 'bmw' && $entry['color'] == 'white')
$result[] = $entry;
}
return $result;
}
Edited: This is a more general solution:
// Filter an array using the given filter array
function multiFilter($array, $filters) {
$result = $array;
// Removes entries that don't pass the filter
$fn = function($entry, $index, $filter) {
$key = $filter['key'];
$value = $filter['value'];
$result = &$filter['array'];
if ($entry[$key] != $value)
unset($result[$index]);
};
foreach ($filters as $key => $value) {
// Pack the filter data to be passed into array_walk
$filter = array('key' => $key, 'value' => $value, 'array' => &$result);
// For every entry, run the function $fn and pass in the filter data
array_walk($result, $fn, $filter);
}
return array_values($result);
}
// Build a filter array - an entry passes this filter if every
// key in this array corresponds to the same value in the entry.
$filter = array('car' => 'BMW', 'color' => 'white');
// multiFilter searches $array, returning a result array that contains
// only the entries that pass the filter. In this case, only entries
// where $entry['car'] = 'BMW' AND $entry['color'] = 'white' will be
// returned.
$whiteBMWs = multiFilter($array, $filter);
Doing this in code is more or less emulating what a RDBMS is perfect for. Something like this would work:
function getNamesByCarAndColor($array,$color,$car) {
$matches = array();
foreach ($array as $entry) {
if($entry["color"]== $color && $entry["car"]==$car)
matches[] = $entry["name"];
}
return $matches;
}
This code would work well for smaller arrays, but as they got larger and larger it would be obvious that this isn't a great solution and an indexed solution would be much cleaner.

PHP Can't get the right format for array

I got stuck somehow on the following problem:
What I want to achieve is to merge the following arrays based on key :
{"Entities":{"submenu_id":"Parents","submenu_label":"parents"}}
{"Entities":{"submenu_id":"Insurers","submenu_label":"insurers"}}
{"Users":{"submenu_id":"New roles","submenu_label":"newrole"}}
{"Users":{"submenu_id":"User - roles","submenu_label":"user_roles"}}
{"Users":{"submenu_id":"Roles - permissions","submenu_label":"roles_permissions"}}
{"Accounting":{"submenu_id":"Input accounting data","submenu_label":"new_accounting"}}
Which needs to output like this:
[{"item_header":"Entities"},
{"list_items" :
[{"submenu_id":"Parents","submenu_label":"parents"},
{"submenu_id":"Insurers","submenu_label":"insurers"}]
}]
[{"item_header":"Users"},
{"list_items" :
[{"submenu_id":"New roles","submenu_label":"newrole"}
{"submenu_id":"User - roles","submenu_label":"user_roles"}
{"submenu_id":"Roles - permissions","submenu_label":"roles_permissions"}]
}]
[{"item_header":"Accounting"},
{"list_items" :
[{"submenu_id":"Input accounting data","submenu_label":"new_accounting"}]
}]
I have been trying all kinds of things for the last two hours, but each attempt returned a different format as the one required and thus failed miserably. Somehow, I couldn't figure it out.
Do you have a construction in mind to get this job done?
I would be very interested to hear your approach on the matter.
Thanks.
$input = array(
'{"Entities":{"submenu_id":"Parents","submenu_label":"parents"}}',
'{"Entities":{"submenu_id":"Insurers","submenu_label":"insurers"}}',
'{"Users":{"submenu_id":"New roles","submenu_label":"newrole"}}',
'{"Users":{"submenu_id":"User - roles","submenu_label":"user_roles"}}',
'{"Users":{"submenu_id":"Roles - permissions","submenu_label":"roles_permissions"}}',
'{"Accounting":{"submenu_id":"Input accounting data","submenu_label":"new_accounting"}}',
);
$input = array_map(function ($e) { return json_decode($e, true); }, $input);
$result = array();
$indexMap = array();
foreach ($input as $index => $values) {
foreach ($values as $k => $value) {
$index = isset($indexMap[$k]) ? $indexMap[$k] : $index;
if (!isset($result[$index]['item_header'])) {
$result[$index]['item_header'] = $k;
$indexMap[$k] = $index;
}
$result[$index]['list_items'][] = $value;
}
}
echo json_encode($result);
Here you are!
In this case, first I added all arrays into one array for processing.
I thought they are in same array first, but now I realize they aren't.
Just make an empty $array=[] then and then add them all in $array[]=$a1, $array[]=$a2, etc...
$array = '[{"Entities":{"submenu_id":"Parents","submenu_label":"parents"}},
{"Entities":{"submenu_id":"Insurers","submenu_label":"insurers"}},
{"Users":{"submenu_id":"New roles","submenu_label":"newrole"}},
{"Users":{"submenu_id":"User - roles","submenu_label":"user_roles"}},
{"Users":{"submenu_id":"Roles - permissions","submenu_label":"roles_permissions"}},
{"Accounting":{"submenu_id":"Input accounting data","submenu_label":"new_accounting"}}]';
$array = json_decode($array, true);
$intermediate = []; // 1st step
foreach($array as $a)
{
$keys = array_keys($a);
$key = $keys[0]; // say, "Entities" or "Users"
$intermediate[$key] []= $a[$key];
}
$result = []; // 2nd step
foreach($intermediate as $key=>$a)
{
$entry = ["item_header" => $key, "list_items" => [] ];
foreach($a as $item) $entry["list_items"] []= $item;
$result []= $entry;
}
print_r($result);
I would prefer an OO approach for that.
First an object for the list_item:
{"submenu_id":"Parents","submenu_label":"parents"}
Second an object for the item_header:
{"item_header":"Entities", "list_items" : <array of list_item> }
Last an object or an array for all:
{ "Menus: <array of item_header> }
And the according getter/setter etc.
The following code will give you the requisite array over which you can iterate to get the desired output.
$final_array = array();
foreach($array as $value) { //assuming that the original arrays are stored inside another array. You can replace the iterator over the array to an iterator over input from file
$key = /*Extract the key from the string ($value)*/
$existing_array_for_key = $final_array[$key];
if(!array_key_exists ($key , $final_array)) {
$existing_array_for_key = array();
}
$existing_array_for_key[count($existing_array_for_key)+1] = /*Extract value from the String ($value)*/
$final_array[$key] = $existing_array_for_key;
}

PHP Reconstruct array Paypal TransactionSearch Function

I am making a call to Paypals API TransactionSearch and I am getting back a flat array of data. I am needing to reconstruct this array. Here is the structure that I get back from paypal:
array(36){
[
"L_TIMESTAMP0"
]=>string(28)"2012%2d09%2d18T22%3a10%3a13Z"[
"L_TIMESTAMP1"
]=>string(28)"2012%2d09%2d18T19%3a55%3a41Z"[
"L_TIMESTAMP2"
]=>string(28)"2012%2d09%2d18T19%3a55%3a41Z"[
"L_TIMEZONE0"
]=>string(3)"GMT"[
"L_TIMEZONE1"
]=>string(3)"GMT"[
"L_TIMEZONE2"
]=>string(3)"GMT"[
"L_TYPE0"
]=>string(7)"Payment"[
"L_TYPE1"
]=>string(7)"Payment"[
"L_TYPE2"
]=>string(7)"Payment"[
"L_EMAIL0"
]=>string(26)"XXXXX%40hotmail%2ecom"[
"L_EMAIL1"
]=>string(31)"XXXX%40lvcoxmail%2ecom"[
"L_EMAIL2"
]=>string(23)"XXXXt%2ecom"[
"L_TRANSACTIONID0"
]=>string(17)"13E586955G649992Y"[
"L_TRANSACTIONID1"
]=>string(17)"8LH96897T3119113R"[
"L_TRANSACTIONID2"
]=>string(17)"87U867057E085230E"[
"L_STATUS0"
]=>string(9)"Completed"[
"L_STATUS1"
]=>string(9)"Completed"[
"L_STATUS2"
]=>string(9)"Completed"[
"L_AMT0"
]=>string(7)"85%2e00"[
"L_AMT1"
]=>string(7)"85%2e00"[
"L_AMT2"
]=>string(7)"85%2e00"[
"L_CURRENCYCODE0"
]=>string(3)"USD"[
"L_CURRENCYCODE1"
]=>string(3)"USD"[
"L_CURRENCYCODE2"
]=>string(3)"USD"[
"L_FEEAMT0"
]=>string(9)"%2d2%2e17"[
"L_FEEAMT1"
]=>string(9)"%2d2%2e17"[
"L_FEEAMT2"
]=>string(9)"%2d2%2e17"[
"L_NETAMT0"
]=>string(7)"82%2e83"[
"L_NETAMT1"
]=>string(7)"82%2e83"[
"L_NETAMT2"
]=>string(7)"82%2e83"[
"TIMESTAMP"
]=>string(28)"2012%2d11%2d08T14%3a24%3a30Z"[
"CORRELATIONID"
]=>string(13)"52c22d68648cd"[
"ACK"
]=>string(7)"Success"[
"VERSION"
]=>string(6)"51%2e0"[
"BUILD"
]=>string(7)"4137385"
}
I need to reset the array to just the following:
["L_STATUSn"]=>string(9)"Completed"
["L_TRANSACTIONIDn"]=>string(17)"8LH96897T3119113R"
with 'n' being the number, being the array key number that paypal returns.
Here is the code I am using, and it is borked.
$i = 0;
$c = 0;
foreach ($comparison AS $aKey => $v) {
$findme1 = 'L_TIMESTAMP'.$i++;
$findme2 = 'L_STATUS'.$c++;
$txid = $myarray[$findme1];
$status = $myarray[$findme2];
$TXid = array_search('$findme1', $aKey);
$Status = array_search('$findme2', $aKey);
$TxID[] = array('Status' => $aStatus, 'TransactionID' => $aTransactionID);
}
appreciate abetter way to reconstruct this array, the method I am trying to use doesnt appear to be too efficient.
Somewhat late, but maybe others have this request too. So here goes:
function process_response($str)
{
$data = array();
$x = explode("&", $str);
foreach($x as $val)
{
$y = explode("=", $val);
preg_match_all('/^([^\d]+)(\d+)/', $y[0], $match);
if (isset($match[1][0]))
{
$text = $match[1][0];
$num = $match[2][0];
$data[$num][$text] = urldecode($y[1]);
}
else
{
$text = $y[0];
// $data[$text] = urldecode($y[1]);
}
}
return $data;
}
Just feed the result from your curl call into this and take the result as a formatted array.
Note the commented out line, there are some fields that are global, such as version, if you want these, uncomment, but then you may have to adjust some formatting code down stream.
if you want to feed this into PHPExcel object, you could do so like so:
$index = 1;
foreach($data as $row)
{
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$index, $row['L_TIMESTAMP'])
->setCellValue('B'.$index, $row['L_TIMEZONE'])
->setCellValue('C'.$index, $row['L_TYPE'])
->setCellValue('D'.$index, $row['L_EMAIL'])
->setCellValue('E'.$index, $row['L_NAME'])
->setCellValue('F'.$index, $row['L_TRANSACTIONID'])
->setCellValue('G'.$index, $row['L_STATUS'])
->setCellValue('H'.$index, $row['L_AMT'])
->setCellValue('I'.$index, $row['L_CURRENCYCODE'])
->setCellValue('J'.$index, $row['L_FEEAMT'])
->setCellValue('K'.$index, $row['L_NETAMT']);
$index++;
}

Merging data structures

I have a set of structured data, that I'm trying to merge together, based on a set of conditions.
There is a set of rows, returned from the db. Each row has a course id, and a discipline id. There's an equal amount of disciplines in each course, but some disciplines are repeated in both courses.
I want to build a data structure where if the discipline is in both courses, then it only appears once on a line in a new data structure, and nothing else, but if there are two unmatched disciplines, then they are both included in a new course.
The code I'm using so far, is filtering and removing the keys that are duplicated, and adding them to a new array. This works fine.
However, because I can't control the order in which the data comes (don't ask) I'm having troubles making sure that each line has either a discipline that appears in both courses, or one of each.
I think I need some techniques to help deal with this, and was wondering if anyone had come across this before. I want to avoid making many consecutive loops, if possible.
Thanks.
edit: messy and horrible code below:
function buildDisciplineMap(){
$sql = "SELECT [idcurso]
,[idversao]
,[ordem]
,[bloco]
,[obsbloco]
,[iddisciplina] as idd
,cast([iddisciplina] as TEXT) as iddisciplina
,[descdisciplina]
,[iddepartamento]
,[descdepartamento]
,[ects]
,[horas]
,[idregente]
,[regente]
,[idregente1]
,[regente1]
,[idregente2]
,[regente2]
,[idregente3]
,[regente3]
,cast([objectivos] as TEXT) as objectivos
,cast([programa] as TEXT) as programa
,[descdisciplina_en]
,cast([objectivos_en] as TEXT) as objectivos_en
,cast([programa_en] as TEXT) as programa_en
FROM [proffile2].[dbo].[vw_site_CursosDisciplinas_FEG]
where idcurso = '3512 GE' or idcurso = '3513 ECON' order by idcurso desc ";
$discs = $this->returnObject($sql);
$map = new stdClass();
// find blocos, and titles
foreach ($discs as $key => $value) {
if (isset($map->bloco[$value->bloco])) {
// block already exists
} else {
#echo "making new block";
$map->bloco[$value->bloco] = new stdClass();
}
if (strlen($value->obsbloco)>1) {
$map->bloco[$value->bloco]->title = $value->obsbloco;
}
}
foreach ($map->bloco as $keybloco => $value) {
$map->bloco[$keybloco]->lines = array();
$processed_ids = array();
foreach ($discs as $kd => $vd) {
if ($vd->bloco == $keybloco) {
// check if this discipline occurs more than once in this block
foreach ($discs as $kdd => $vdd) {
if ($vdd->iddisciplina == $vd->iddisciplina && $kdd != $kd && !in_array($kd,$processed_ids) && !in_array($kdd,$processed_ids)) {
// this discipline is for both courses
$details = array();
$details['both'] = $vd;
$map->bloco[$keybloco]->lines[] = $details;
array_push($processed_ids, $kd, $kdd);
unset($discs[$kdd]);
unset($discs[$kd]);
break;
}
}
}
}
}
$processed_ids = array();
foreach ($discs as $key => $value) {
echo $value->idcurso."\n";
}
foreach ($discs as $kd => $vd) {
$bloco = $vd->bloco;
$lastidx =sizeof($map->bloco[$bloco]->lines)-1;
$line = $map->bloco[$bloco]->lines[$lastidx];
echo sizeof($map->bloco[$bloco]->lines);
#pr($line);
if (isset($line['both'])) {
echo "omog - \n ";
$map->bloco[$bloco]->lines[][$vd->idcurso] = $vd;
unset($discs[$kd]);
continue;
}
#pr($line['both']->idcurso);
foreach ($map->bloco[$bloco]->lines as $k => $v) {
echo $k."-";
#echo $v['3513 ECON']->idcurso;
}
if ($line[$vd->idcurso]) {
echo 'add';
$map->bloco[$bloco]->lines[][$vd->idcurso] = $vd;
} else {
echo 'fill';
$map->bloco[$bloco]->lines[sizeof($map->bloco[$bloco]->lines)-1][$vd->idcurso] = $vd;
}
}
echo sizeof($discs);
return $map;
}
You said "don't ask", but I've got to: why can't you control the order of the rows? Aren't you the one writing the database query?
If you think fixing the order of the rows will help you parse and build a new data structure better, why not make sorting the rows the first step in your process? Or, is the data set too large?
You might find some of PHP's array set manipulations to be of use. i.e. array_diff, array_intersect_key etc.

Categories