Jump to content

opconvert function malfunctioning...


Greywacke

Recommended Posts

hi all, i am having a problem regarding the following function - specifically opconvert. it is used in different places, but when $types is set as NULL when calling the function - checking it to be null in the first if statement - does not seem to be executing. so $manual stays false and the first value is not returned but the last. below the code is a data extract which is logged during execution.

function objectToArray($d) {	if (is_object($d)) {		// Gets the properties of the given object		// with get_object_vars function		$d = get_object_vars($d);	}	if (is_array($d)) {		/*		* Return array converted to object		* Using __FUNCTION__ (Magic constant)		* for recursive call		*/		return array_map(__FUNCTION__, $d);	} else {		// Return array		return $d;	}}function arrayToObject($d) {	if (is_array($d)) {		/*		* Return array converted to object		* Using __FUNCTION__ (Magic constant)		* for recursive call		*/		return (object) array_map(__FUNCTION__, $d);	}	else {		// Return object		return $d;	}}function gettypes($array) {	$types = "";	foreach ($array as $id=>$element) {		$types .= (($id>0)?",":"").$element["type"];	}	return $types;}function opconvert($array,$types=NULL,$levels=1) {	$manual = false;	if ($types===NULL) {		$manual = true;		$types = gettypes($array);		//$array = objectToArray($array);	}	switch ($levels) {		case 1:	// pre-formatted array passed			return implode(",",$array);			break;		case 2: // unformatted hash passed			$i = 0;			$out = array();			$type = explode(",",$types);			array_push($GLOBALS["sql"],				"$manual = ".print_r($manual,true),				"$types = ".print_r($types,true),				"$type = ".print_r($type,true)			);			foreach ($array as $key=>$value) {				$value = objectToArray($value);				$out[$key] = (($manual)?$value["type"]:$type[$i]).							 "|".							 (($manual)?$value[$type[$i]]:$value);				array_push($GLOBALS["sql"],					"$key = ".print_r($key,true),					"$value = ".print_r($value,true),					"$out[$key] = ".print_r($out[$key],true)				);				$i++;			}			return implode(",",$out);			break;	}}

 

[14] => $manual =

[15] => $types = number
[16] => $type = Array
(
[0] => number
)
[17] => $key = 0
[18] => $value = Array
(
[type] => mobile
[number] => 0724121932
)
[19] => $out[$key] = number|Array
[20] => $manual =
[21] => $types = address
[22] => $type = Array
(
[0] => address
)
[23] => $key = 0
[24] => $value = Array
(
[type] => other
[address] => greywacke@hotmail.com
)
[25] => $out[$key] = address|Array
[26] => $manual =
[27] => $types = address
[28] => $type = Array
(
[0] => address
)

 

why does $manual not set to true if parameter $types is passed as NULL? i have converted it from stdClass data type to array data type - which seems to make it more legit. in the end i am looking to have the following types of values returned from the function formatted as DataType|DataValue,DataType|DataValue from the passed json data extract:

 

POSSIBLE EMAIL TYPES

  • work
  • home
  • other

POSSIBLE PHONE TYPES

  • work
  • mobile
  • home
  • direct
  • fax
  • other

POSSIBLE URL TYPES

  • website
  • blog
  • twitter
  • linkedin
  • facebook
  • other

the main issue though, is when calling via "opconvert($data->data->contact->phones,NULL,2)" for instance, it does not set $manual to true - even though it enters the if statement and parses the variables property (actually array element after converting from stdClass type).

 

a swift answer (or even kick in the right direction :P), would truly be appreciated,

 

Pierre "Greywacke" du Toit.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

thankyou justsomeguy ;)

Link to comment
Share on other sites

hi - have not been able to test the function untill justnow, and unfortunately $manual still does not get set to true. how is this possible with the following version of opconvert?

function opconvert($array,$types=NULL,$levels=1) {	$manual = false;	if (is_null($types)) {		$manual = true;		$types = gettypes($array);		//$array = objectToArray($array);	}	switch ($levels) {		case 1:	// pre-formatted array passed			return implode(",",$array);			break;		case 2: // unformatted hash passed			$i = 0;			$out = array();			$type = explode(",",$types);			array_push($GLOBALS["sql"],				"$manual = ".print_r($manual,true),				"$types = ".print_r($types,true),				"$type = ".print_r($type,true)			);			foreach ($array as $key=>$value) {				$value = objectToArray($value);				$out[$key] = (($manual)?$value["type"]:$type[$i]).					     "|".					     (($manual)?$value[$type[$i]]:$value);				array_push($GLOBALS["sql"],					"$key = ".print_r($key,true),					"$value = ".print_r($value,true),					"$out[$key] = ".print_r($out[$key],true)				);				$i++;			}			return implode(",",$out);			break;	}}

with the following output from $GLOBALS["sql"] on lines 854 and 864, after clearing memcached.

 

[14] => $manual =

[15] => $types = number
[16] => $type = Array
(
[0] => number
)
[17] => $key = 0
[18] => $value = Array
(
[type] => mobile
[number] => 0724121932
)
[19] => $out[$key] = number|Array
[20] => $manual =
[21] => $types = address
[22] => $type = Array
(
[0] => address
)
[23] => $key = 0
[24] => $value = Array
(
[type] => other
[address] => greywacke@outlook.com
)
[25] => $out[$key] = address|Array
[26] => $manual =
[27] => $types = address
[28] => $type = Array
(
[0] => address
)

for some reason unbekbownst to me - it is still not setting $manual to true... -_-

sincerely,

Pierre du Toit.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

doesn't show it being false either... and $types gets populated on line 843. why would it not be executing the line before, or at least displaying $manual as true?

$manual needs to be set to true for this function to work as expected... how would i accomplish this? the following code is not working either - and was recommended on the reference url below (the post has been around for some 12 years already! :o hope this still holds water - although what i am actually referring to is contained within the Editors Note at the bottom of the post).

 

http://www.php.net/manual/en/function.is-null.php#12120

function opconvert($array,$types=NULL,$levels=1) {	$manual = ($types===NULL);	if ($manual) $types = gettypes($array);	switch ($levels) {		case 1:	// pre-formatted array passed			return implode(",",$array);			break;		case 2: // unformatted hash passed			$i = 0;			$out = array();			$type = explode(",",$types);			array_push($GLOBALS["sql"],				"$manual = ".print_r($manual,true),				"$types = ".print_r($types,true),				"$type = ".print_r($type,true)			);			foreach ($array as $key=>$value) {				$value = objectToArray($value);				$out[$key] = (($manual)?$value["type"]:$type[$i]).							 "|".							 (($manual)?$value[$type[$i]]:$value);				array_push($GLOBALS["sql"],					"$key = ".print_r($key,true),					"$value = ".print_r($value,true),					"$out[$key] = ".print_r($out[$key],true)				);				$i++;			}			return implode(",",$out);			break;	}}
Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

am currently testing setting $manual initially, the changes i have made are above.

i replaced

	$manual = false;	if (is_null($types)) {		$manual = true;		$types = gettypes($array);		//$array = objectToArray($array);	}

with

	$manual = ($types===NULL);	if ($manual) $types = gettypes($array);

 

[14] => $manual =

[15] => $types = number
[16] => $type = Array
(
[0] => number
)
[17] => $key = 0
[18] => $value = Array
(
[type] => mobile
[number] => 0782571038
)
[19] => $out[$key] = number|Array
[20] => $manual =
[21] => $types = address
[22] => $type = Array
(
[0] => address
)
[23] => $key = 0
[24] => $value = Array
(
[type] => other
[address] => krugermc.55@gmail.com
)
[25] => $out[$key] = address|Array
[26] => $manual =
[27] => $types = address
[28] => $type = Array
(
[0] => address
)

and the result from the function, even after clearing memcached - is *STILL* the above! why is $manual never true with the above condition? even when using is_null($types)...

ok have edited following line as such:

$manual = is_null($types);

testing again... ok still 0o am moving away from the inline if's lower in the function (originally lines 857 to 859, but now lines 857 to 861) and executing it as follows:

function opconvert($array,$types=NULL,$levels=1) {	$manual = is_null($types);	if ($manual) $types = gettypes($array);	switch ($levels) {		case 1:	// pre-formatted array passed			return implode(",",$array);			break;		case 2: // unformatted hash passed			$i = 0;			$out = array();			$type = explode(",",$types);			array_push($GLOBALS["sql"],				"$manual = ".print_r($manual,true),				"$types = ".print_r($types,true),				"$type = ".print_r($type,true)			);			foreach ($array as $key=>$value) {				$value = objectToArray($value);				if ($manual) {					$out[$key] = $value["type"]."|".$value[$type[$i]];				} else {					$out[$key] = $type[$i]."|".$value;				}				array_push($GLOBALS["sql"],					"$key = ".print_r($key,true),					"$value = ".print_r($value,true),					"$out[$key] = ".print_r($out[$key],true)				);				$i++;			}			return implode(",",$out);			break;	}}

and testing again...

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

why can't i get $manual set to true or false, if NULL is provided as the second parameter in the function opconvert??? this is driving me crazy - it works in other instances just not in this one. the output of $out[$key] is $out[$key] = number|Array

this is happening because it is falling past the exception. if i change the $manual to !$manual then it messes up the other instances where this function is used from.

 

 

why can't i get $manual set to true or false, if NULL is provided as the second parameter in the function opconvert???

ok - am going to try using empty($types) rather, see if this helps.

function opconvert($array,$types=NULL,$levels=1) {	$manual = empty($types);	if ($manual) $types = gettypes($array);	switch ($levels) {		case 1:	// pre-formatted array passed			return implode(",",$array);			break;		case 2: // unformatted hash passed			$i = 0;			$out = array();			$type = explode(",",$types);			array_push($GLOBALS["sql"],				"$manual = ".print_r($manual,true),				"$types = ".print_r($types,true),				"$type = ".print_r($type,true)			);			foreach ($array as $key=>$value) {				$value = objectToArray($value);				if ($manual) {					$out[$key] = $value["type"]."|".$value[$type[$i]];				} else {					$out[$key] = $type[$i]."|".$value;				}				array_push($GLOBALS["sql"],					"$key = ".print_r($key,true),					"$value = ".print_r($value,true),					"$out[$key] = ".print_r($out[$key],true)				);				$i++;			}			return implode(",",$out);			break;	}}
Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

updated line 840 to read

	$manual = (empty($types))?true:false;

but still when printing $manual i get nothing with print_r -_-

should i try it as a string? ok testing following code:

function opconvert($array,$types=NULL,$levels=1) {	$manual = (!empty($types))?"true":"false";	if ($manual) $types = gettypes($array);	switch ($levels) {		case 1:	// pre-formatted array passed			return implode(",",$array);			break;		case 2: // unformatted hash passed			$i = 0;			$out = array();			$type = explode(",",$types);			array_push($GLOBALS["sql"],				"$manual = ".print_r($manual,true),				"$types = ".print_r($types,true),				"$type = ".print_r($type,true)			);			foreach ($array as $key=>$value) {				$value = objectToArray($value);				if ($manual=="true") {					$out[$key] = $value["type"]."|".$value[$type[$i]];				} else {					$out[$key] = $type[$i]."|".$value;				}				array_push($GLOBALS["sql"],					"$key = ".print_r($key,true),					"$value = ".print_r($value,true),					"$out[$key] = ".print_r($out[$key],true)				);				$i++;			}			return implode(",",$out);			break;	}}
Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

and its finally working 100%, since Friday 20th at 07:55 am (+2 gmt) ;)

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...