Jump to content

php multidem array - assign keys to identify each set


quasiman
Go to solution Solved by quasiman,

Recommended Posts

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;

Link to comment
Share on other sites

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 by requinix
Link to comment
Share on other sites

  • Solution

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;
                                    }
                                }
                            }
                        }
                    }
                }
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.