quasiman Posted April 22, 2014 Share Posted April 22, 2014 Sorry for the topic title if it's confusing, I wasn't sure what to call this. So, I'm calling an array of API request and pushing the responses all into one array - but I need to identify which API is which. I've assigned keys to the api array, but I'm not sure how to get them into the returned data. Here's what I have so far..... $api = array( "domains" => "api/domains/search.json", "websrvs" => "api/webservices/search.json", "snlinus" => "api/singledomainhosting/linux/us/search.json", "digcert" => "api/digitalcertificate/search.json"); foreach ($api as $type => $value) { // Joomla style trigger to get the API data $data[] = $dispatcher->trigger('getData', array(&$value, $string)); } // Loop through results foreach ($data as $key) { if (is_array($key)) { foreach ($key as $val) { if (is_array($val)) { foreach ($val as $v) { if (is_array($v)) { $query[] = $v; } } } } } } return $query; Quote Link to comment https://forums.phpfreaks.com/topic/287922-php-multidem-array-assign-keys-to-identify-each-set/ Share on other sites More sharing options...
requinix Posted April 22, 2014 Share Posted April 22, 2014 (edited) Into $query? 1. Get rid of the second foreach loop. Just stick all of its code into the first one. And make $data just a temporary variable, not an array of everything. 2. Work the $type into $query. How depends on what you want $query to look like. [edit] And don't by-reference $value. It's dangerous. Edited April 22, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/287922-php-multidem-array-assign-keys-to-identify-each-set/#findComment-1476898 Share on other sites More sharing options...
Solution quasiman Posted April 22, 2014 Author Solution Share Posted April 22, 2014 I've been trying and trying, but can't seem to get how you're meaning... 1. I need $data to be an array of everything, so that I can iterate over it to get the $query array of records. I'm not sure how I can make $data a temporary variable to do that - can you explain further? 2. I've tried combining the two arrays together, but really that just iterates over each $api method, reassigning values all over them. I'm sure I'm not doing what you had intended, but this seems to work foreach ($api as $type => $value) { $data[$type] = $dispatcher->trigger('getData', array(&$value, $string)); } // Loop through results foreach ($data as $k=>$key) { if (is_array($key)) { foreach ($key as $val) { if (is_array($val)) { foreach ($val as $v) { if (is_array($v)) { $v['apimethod'] = $k; $query[] = $v; } } } } } } Quote Link to comment https://forums.phpfreaks.com/topic/287922-php-multidem-array-assign-keys-to-identify-each-set/#findComment-1476906 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.