Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7574649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:29:12+00:00 2026-05-30T16:29:12+00:00

I am very very new at PHP and I don’t even know JSON. Also,

  • 0

I am very very new at PHP and I don’t even know JSON. Also, I couldn’t know how to express it in the question. My question is;

I need to make a JSON data with PHP with the data from database. So what my expected output is like;

{ "canFireAPICalls": true, "ads": { "servers": [ { "type": "OpenX", "apiAddress": "http://openx.openvideoads.org/openx/www/delivery/fc.php", } ], "schedule": [ { "zone": "5", "position": "pre-roll" }, { "zone": "33", "width": 450, "height": 50, "startTime": "00:00:05", "duration": "15" } ], } }

What I wrote in PHP is;

while ($doZones->fetch() && $row = $doZones->toArray()) {
    $row_array['zone'] = $row['zoneid'];
    $row_array['position'] = $row['delivery'];

    if($row['delivery'] == 0 || $row['delivery'] == 1 || $row['delivery'] == 4 ){
        $row_array['width'] = $row['width'];
        $row_array['height'] = $row['height'];
    }


    array_push($return_arr,$row_array);
}

$resultJSON .=json_encode($return_arr);

And my output is

{ "canFireAPICalls": true, "ads": { "servers": [ { "type": "OpenX", "apiAddress": "http://openx.openvideoads.org/openx/www/delivery/fc.php", } ], "schedule":[{"zone":"3","position":"6"},{"zone":"2","position":"6"},{"zone":"4","position":"6"},{"zone":"5","position":"6"},{"zone":"6","position":"1","width":"468","height":"60"},{"zone":"7","position":"6","width":"468","height":"60"},{"zone":"8","position":"0","width":"120","height":"600"},{"zone":"9","position":"7","width":"120","height":"600"},{"zone":"10","position":"0","width":"120","height":"600"},{"zone":"11","position":"0","width":"728","height":"90"},{"zone":"12","position":"0","width":"120","height":"90"},{"zone":"13","position":"1","width":"468","height":"60"},{"zone":"14","position":"3","width":"468","height":"60"},{"zone":"15","position":"1","width":"560","height":"40"},{"zone":"16","position":"4","width":"468","height":"60"},{"zone":"17","position":"6","width":"468","height":"60"},{"zone":"18","position":"6","width":"468","height":"60"},{"zone":"19","position":"7","width":"468","height":"60"}] } }

If you noticed, even there shouldn’t be width and height, there is. I only want the width and height when $row[‘delivery’] is 0,1 or 4. But, once it finds a value 0,1 or 4, then even if the next row is not 0,1 or 4, it prints width and height. Is there a way to fix this? I don’t know any JSON and just started with PHP. Right now, I don’t care about the missing tags like duration or writing pre-roll. I will take care of them after I fix this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:29:14+00:00Added an answer on May 30, 2026 at 4:29 pm

    The problem is in your php. You don’t reset the values of your array $row_array, so the first time the condition is true, the values of height and width are kept in the array.

    You can fix it this way :

    while ($doZones->fetch() && $row = $doZones->toArray()) {
        $row_array['zone'] = $row['zoneid'];
        $row_array['position'] = $row['delivery'];
    
        if($row['delivery'] == 0 || $row['delivery'] == 1 || $row['delivery'] == 4 ){
            $row_array['width'] = $row['width'];
            $row_array['height'] = $row['height'];
        }
    
    
        array_push($return_arr,$row_array);
        // Add this line to reset the array and reset width / height
        $row_array = array();
    }
    
    $resultJSON .=json_encode($return_arr);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to PHP and really don't know where to start when
I know the title isn't very clear. I'm new to PHP, so there might
Warning - I am very new to NHibernate. I know this question seems simple
Very new to web programming still and I don't really have a specific question
I'm very new with JSON format, and in the tutorials I read I don't
I'm new to forms and post data ... so I don't know how solve
I'm very new to PHP and I have a small task of displaying a
I'm new to php and zend framework and fill very uncertain about following: For
Very new to FluentNHibernate, but I'm also excited about the area. I've recently started
Im very new to SQL but need to write a query to do the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.