I am trying to build a mysql query that adds items to a database. The items are held inside an object that is formatted like:
object(PhealResult)#7 (5) {
["request_time"]=>
string(19) "2011-08-04 14:14:37"
["request_time_unixtime"]=>
int(1312467277)
["cached_until"]=>
string(19) "2011-08-04 20:14:37"
["cached_until_unixtime"]=>
int(1312488877)
["_element:private"]=>
object(PhealElement)#12 (3) {
["_name"]=>
string(6) "result"
["_value"]=>
object(PhealContainer)#11 (1) {
["_container:private"]=>
array(1) {
["assets"]=>
object(PhealRowSet)#14 (3) {
[0]=>
object(PhealRowSetRow)#19 (6) {
["itemID"]=>
string(13) "1003152969505"
["locationID"]=>
string(8) "30000157"
["typeID"]=>
string(5) "11489"
["quantity"]=>
string(1) "1"
["flag"]=>
string(1) "0"
["singleton"]=>
string(1) "1"
}
[1]=>
object(PhealRowSetRow)#18 (7) {
["itemID"]=>
string(9) "290900396"
["locationID"]=>
string(8) "66002198"
["typeID"]=>
string(2) "27"
["quantity"]=>
string(1) "1"
["flag"]=>
string(2) "71"
["singleton"]=>
string(1) "1"
["contents"]=>
object(PhealRowSet)#24 (15) {
[0]=>
object(PhealRowSetRow)#29 (5) {
["itemID"]=>
string(13) "1003305129036"
["typeID"]=>
string(4) "2183"
["quantity"]=>
string(1) "4"
["flag"]=>
string(3) "117"
["singleton"]=>
string(1) "0"
}
**cropped**
}
}
**cropped**
}
}
}
}
Pay particular attention to how the nesting works. The top level rowset is named “assets”, but then each item in there can have “contents.” Each item in that can also have a “contents” list. There’s a limit to how far it can go in terms of EVE but the limit is unknown and the code should assume there’s no real limit.
I have played around a little with the one of the code snippets located on the http://php.net/manual/en/language.types.array.php which appears to be down as of now.
function array_value_recursive($key, array $arr){
$val = null;
array_walk_recursive($arr, function($v, $k) use($key, &$val){
$val = $k == $key ? $v : (!is_null($val) ? $val : false);
});
return $val;
}
I cannot seem to wrap my head around how to work with the object / array given that I do not know how deep it goes. How do I know how far in I am and how do I access the data contained when i am x deep in the object/ array. I thank you in advance for your time and assistance.
The code below is the code I was using before I knew that there can be a somewhat endless nested “contents”. I have included this code so you can see what it is that I am trying to accomplish.
try {
$corpPheal = new Pheal($fullUserID, $fullAPIKey, "corp");
$corpAssetList = $corpPheal->AssetList(array('characterID'=>$fullCharID));
echo "<pre>";
var_dump($corpAssetList);
echo "</pre>";
}
catch (PhealException $e) {
echo 'error: ' . $e->code . ' meesage: ' . $e->getMessage();
}
$numOfAssetList = count($corpAssetList->assets);
for ($i =0; $i <= ($numOfAssetList -1); $i++){
$numOfAssetContents = count($corpAssetList->assets[$i]->contents);
echo $numOfAssetContents . "<br>\n";
if ($numOfAssetContents > 0){
$count = 0;
for ($j=0; $j <= ($numOfAssetContents - 1); $j++){
echo "i = $i, j = $j <br>\n";
$count++;
foreach ($corpAssetList->assets[$i]->contents[$j] as $key => $value){
switch ($key) {
case "itemID":
$qry = "INSERT INTO eve_asset_list(`itemID`, `typeID`, `quantity`, `flag`, `singleton`) VALUES('$value','";
break;
case "typeID":
$qry = $qry . $value . "','";
break;
case "quantity":
$quantity = $value;
$qry = $qry . $value . "','";
break;
case "flag":
$qry = $qry . $value . "','";
break;
case "singleton":
$qry = $qry . $value . "') ON DUPLICATE KEY UPDATE quantity = $quantity";
break;
}
}
$result = mysql_query($qry);
include "./includes/mysqlError.inc.php";
}
}else {
foreach ($corpAssetList->assets[$i] as $key => $value){
switch ($key) {
case "itemID":
$qry = "INSERT INTO eve_asset_list(`itemID`, `typeID`, `quantity`, `flag`, `singleton`) VALUES('$value','";
break;
case "typeID":
$qry = $qry . $value . "','";
break;
case "quantity":
$quantity = $value;
$qry = $qry . $value . "','";
break;
case "flag":
$qry = $qry . $value . "','";
break;
case "singleton":
$qry = $qry . $value . "') ON DUPLICATE KEY UPDATE quantity = $quantity";
break;
}
}
}
}
What your code snippet now does is apply a function to every element array_walk_recursive. This is a predefined function in PHP.
The function that is applied right now looks whether the current key equals the one that’s given as a parameter. If they’re equal it stores the value and otherwise not.
Eventually the first found value is returned. This return value can be an array itself and you could use it to access internal data.
At the moment it doesn’t do anything with the deepness of the data. Is this something that you want to change? If yes, What would you like the function to do?
Edit:
You’d probably need something like