Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

Could you point what must to be corrected at this PHP code to make it correctly generates the needed JSON code?

Hi Experts

Could you point what must to be corrected at this PHP code to make it correctly generates the needed JSON code?

Accordingly to:
<?
$prof_anterior = '';
						
while ($reg3 = $bd1->obtenerRegA())
{						
	//....
	
	// Checks if the id_profissional changed
	if( $reg3['id_profissional'] !== $prof_anterior )
	{

            // If changed mount completelly the object					
			$obj->professionals = array(
										array('name'=>$reg3['nome_completo'],
											  'nickname'=>'',
											  'roles'=>array($papel),
											  'internal_id'=>"CONS:".$conselho.'-'.$identificador_interno,
											  'cellphone'=>$reg3['celular_profissional'],
											  'tags'=>array("COOP:".$reg3['cooperativa'],
											  "FUNC:".$reg3['tipo_profissional'],
											  "TIPO_PAGAMENTO:".$tipo_pagamento,
											  "VALOR:R$".$reg3['valor_prof']),
											  "gender"=>$reg3['sexo_profissional'], 
											  "send_invite_when_submit"=>true,
											  'shifts' => $shifts)  // I guess at this point a for/ next must to be implemented to mount all the shift related to 'id_profissional', following the rule, but I don't know how to implement it..
										  );
		
			$schedules[] = $obj; //add each object to the array
	}
	else
	{		
			// Just add another object, not completelly the new array
			$shift_prof->shifts =  $shifts;	
			$schedules[] = $shift_prof; //add each object to the array
	}

    // Goes to the next id_profissional 
	$prof_anterior = $reg3['id_profissional'];

	//....
}
?>		

Open in new window

The JSON assembled is:

   {...
    [...
	 {...
      "shifts": [
            {
              "date": "2012-06-26",
              "period_name": "ENFER",
              "tags": [
                "ESC:"
              ]
            }
          ]
        }
      ]
    },
    {
      "shifts": [
        {
          "date": "2012-06-27",
          "period_name": "ENFER",
          "tags": [
            "ESC:"
          ]
        }
      ]
    },

Open in new window


The needed JSON needs to be like this, where every shift must be an object member of the array shifts.

"shifts": [
		{
		  "date": "2012-06-26",
		  "period_name": "ENFER",
		  "tags": [
			"ESC:"
		  ]
		}
		{
		  "date": "2012-06-27",
		  "period_name": "ENFER",
		  "tags": [
			"ESC:"
		  ]
		}
	  ],

Open in new window


Thanks in advance
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Unfortunately, we're only seeing a part of your code so it's difficult to tell you what to do.

Firstly, it doesn't really makes sense that you've wrapped your code in a while loop. Each time through the loop, you're just overwriting $obj->professionals with a new array.

Secondly, you're setting a shifts property to the value of the $shifts variable but we have no way of knowing what that is.

The JSON that you're after is an array with a shifts property (key) that has it's value set to an array of 2 objects. A quick demo of that follows:

$obj1 = (object)array(
    "date" => "2012-06-26",
    "period_name" => "ENFER",
    "tags" => array("ESC:")
);

$obj2 = (object)array(
    "date" => "2012-06-27",
    "period_name" => "ENFER",
    "tags" => array("ESC:")
);

$shifts = array ($obj1, $obj2);
$data = array("shifts" => $shifts);

echo json_encode($data);

Open in new window

What we've done here is create 2 objects - $obj1 and $obj2 (we've created an array and converted it to an object). We've then created a new array called $shifts that contains these 2 objects. Finally, we've created an array called $data and added a property called "shifts". We've set the value of that property to the $shifts variable (the array containing the 2 objects). The output you get from that code is :

 {
 	"shifts": [{
 		"date": "2012-06-26",
 		"period_name": "ENFER",
 		"tags": ["ESC:"]
 	}, {
 		"date": "2012-06-27",
 		"period_name": "ENFER",
 		"tags": ["ESC:"]
 	}]
 }

Open in new window

Avatar of Eduardo Fuerte

ASKER

Hi @Chris

Thank you for the reply, by now.
I'm out of office until monday,  I'm going to send you a more meaningfull part of the code.
Hi @Chris

Here is the code used.

Escala-EE_02.php

In the meanwhile I tought  if the better strategy is to use real arrays and objects, since the structure became more and more intricated, since it began.
Maybe to use HEREDOC to mimic how the real arrays and objects would be wrote, and simply wrote it on the file, like a XML is assembled. I think it could simplify a lot the job. Do you agree?
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

 If you're suggesting that you should manually write out the JSON data, I would argue that it will be a lot more complicated to try and do that. You're better off creating an object with properties and then encoding that when you need the JSON.

Very good advice, I returned back to the initial strategy, based mainly on your 1st post and I'm getting advances...
Thank you for the guidance.
@Chris

Is it possible to you to give a look at this question also?

another quetion.