0

I am looking at how to condense this down into a proper "for loop" which means it runs the text, value, and id fields for the length of the json array.

This works but is not dynamic. there has to be a way to do this and I just cant find an example how beyond the point I'm at.

                array(
                "text" => $data[3]['name'],
                "value" => $data[3]['slug'],
                "id" => "id-2"
            )

Everything I have found so far has been useful but nothing past this point and I'm sure it's a simple answer.

what this does is get all the entries within a taxonomy to populate a drop-down form's field

function myfunction() {

    $request = wp_remote_get( 'https://shatner/wp-json/wpas-api/v1/enterprize-crew?type=name' );

    if( is_wp_error( $request ) ) {
        return false; // Bail early
    }

$body = wp_remote_retrieve_body( $request );

$data = json_decode( $body, true );
    $elementCount  = count($data);
    foreach($data as $key['name'] => $value) {
     
        $options = array(
            array(
                "text" => $data[0]['name'],
                "value" => $data[0]['slug'],
                "id" => "id-1"
            ),
            array(
                "text" => $data[1]['name'],
                "value" => $data[1]['slug'],
                "id" => "id-2"
            ),
                            
            array(
                "text" => $data[2]['name'],
                "value" => $data[2]['slug'],
                "id" => "id-1"
            ),
            array(
                "text" => $data[3]['name'],
                "value" => $data[3]['slug'],
                "id" => "id-2"
            )
        );
        
        return $options;
    }
    

}

2
  • 2
    Please add result of 'var_dump($data);'. And "id" tags should be really id-1, id-2, id-1, id-2 or id-1, id-2, id-3, id-4? Commented Sep 1, 2021 at 5:47
  • php.net/manual/en/function.var-dump.php Not sure what you mean, the json values are already there and I can grab the values if I hardcode the references, what I'm trying to do is make it dynamic through a loop Yes, I caught that. those values don't matter quite yet until I get this part working. Im using ex contact form and it uses those id values to trigger conditionals in the form. Commented Sep 1, 2021 at 19:49

1 Answer 1

1

You do not have a json instance, but we assume your json is a list of name and slug, so you can do the following:

foreach($data as $key => $value) {
    $options[] = ["text" => $value['name'], "value" => $value['slug'], "id" => "id-" . ($key + 1)];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Would this replace the entire for each loop I already have?
Yes, give it a try

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.