I Have an array of Objects (result of mysql Query)
array(28) {
[0]=>
array(2) {
["member_id"]=>
string(5) "40105"
["last_login"]=>
string(19) "2014-02-18 06:04:06"
}
[1]=>
array(2) {
["member_id"]=>
string(5) "51758"
["last_login"]=>
string(19) "2014-04-21 09:29:11"
}
[2]=>
array(2) {
["member_id"]=>
string(5) "52682"
["last_login"]=>
string(19) "2014-04-21 08:27:00"
}
What is the best datatype to sort it in redis ? I need to search and add an object into this result?
shall I use SET or LIST ?
Related
I have a bigger multi dim array I pull from a db.
array(43593) {
[0]=>
array(4) {
["artnum"]=>
string(5) "0422272221"
["ekprice"]=>
string(5) "52.78"
["ekpriceist"]=>
string(5) "52.78"
["lieferantnr"]=>
string(7) "7000202"
}
[1]=>
array(4) {
["artnum"]=>
string(5) "04233337133333"
["ekprice"]=>
string(5) "49.45"
["ekpriceist"]=>
string(5) "49.45"
["lieferantnr"]=>
string(7) "7000211"
}
from another DB server I pull this array
array(73287) {
[0]=>
array(2) {
["ARTNUM"]=>
string(5) "0422272221"
["QUALITAETSMERKMAL"]=>
string(1) "p"
}
[1]=>
array(2) {
["ARTNUM"]=>
string(5) "04233337133333"
["QUALITAETSMERKMAL"]=>
string(1) "m"
}
When I excecute the following from user interface
foreach($aStockArticles as $aStockArticle) {
foreach($aArticleStamm as $artikel)
{
if($aStockArticle['artnum'] == $artikel['artnum'])
{
$aStockArticle['quality'] = $artikel['QUALITAETSMERKMAL'];
}
} }
I recieve
[line 451] [code ] [message Maximum execution time of 30 seconds
exceeded]
As you can see, the amount of articles and the sequence they are ordered can differ from each other.
Since I have no access environment variables
I usually would do a simple join in the db query but in this case its not possible due to the two db servers.
QUESTION:
How do I merge these arrays on the ID (artnum)?
So it looks like the following and stay within the excecution time?
array(43593) {
[0]=>
array(4) {
["artnum"]=>
string(5) "0422272221"
["ekprice"]=>
string(5) "52.78"
["ekpriceist"]=>
string(5) "52.78"
["lieferantnr"]=>
string(7) "7000202"
["QUALITAETSMERKMAL"]=>
string(1) "p"
}
[1]=>
array(4) {
["artnum"]=>
string(5) "04233337133333"
["ekprice"]=>
string(5) "49.45"
["ekpriceist"]=>
string(5) "49.45"
["lieferantnr"]=>
string(7) "7000211"
["QUALITAETSMERKMAL"]=>
string(1) "m"
}
I have a CMS I am using that serializes their data in the database. I used the unserialize() function to convert the data into an associative array. Now I am having a hard time pulling the value of the image from the associative array:
This is the simple while loop I am using to loop through the rows:
while($row = mysql_fetch_assoc($query_models)){
$model_name = $row['ModelName'];
$model_thumbnail = unserialize($row['info']);
}
this is the Key and Value of the array I need to get the value of, so I can assign the correct thumbnail image to the respected person:
["1x_filename"]=> string(19) "00/83/83-set-1x.jpg"
The full array is below, and the key I am targeting is located more at the bottom of this array:
array(1) {
["thumbs"]=> array(2) {
[16]=> array(17) {
["id"]=> string(2) "82"
["1x_width"]=> string(3) "220"
["1x_height"]=> string(3) "330"
["2x_width"]=> string(3) "440"
["2x_height"]=> string(3) "660"
["3x_width"]=> string(3) "660"
["3x_height"]=> string(3) "990"
["4x_width"]=> string(3) "880"
["4x_height"]=> string(4) "1320"
["width"]=> string(3) "220"
["height"]=> string(3) "330"
["retinamode"]=> string(1) "1"
["filename"]=> string(10) "82-set.jpg"
["1x_filename"]=> string(19) "00/82/82-set-1x.jpg"
["2x_filename"]=> string(19) "00/82/82-set-2x.jpg"
["3x_filename"]=> string(19) "00/82/82-set-3x.jpg"
["4x_filename"]=> string(19) "00/82/82-set-4x.jpg"
}
[17]=> array(17) {
["id"]=> string(2) "83"
["1x_width"]=> string(3) "106"
["1x_height"]=> string(3) "150"
["2x_width"]=> string(3) "212"
["2x_height"]=> string(3) "300"
["3x_width"]=> string(3) "318"
["3x_height"]=> string(3) "450"
["4x_width"]=> string(3) "424"
["4x_height"]=> string(3) "600"
["width"]=> string(3) "106"
["height"]=> string(3) "150"
["retinamode"]=> string(1) "1"
["filename"]=> string(10) "83-set.jpg"
["1x_filename"]=> string(19) "00/83/83-set-1x.jpg"
["2x_filename"]=> string(19) "00/83/83-set-2x.jpg"
["3x_filename"]=> string(19) "00/83/83-set-3x.jpg"
["4x_filename"]=> string(19) "00/83/83-set-4x.jpg"
}
}
}
Any help would be greatly appreciated. Thanks!
Just like this :
$model_thumbnail = unserialize($row['info']);
$picturePath = $model_thumbnail['thumbs'][17]['1x_filename'];
picturePath will contain your images path
I would recommend you to use mysqli or an other database adapter in php, as mysql_ functions are depricated and even removed in never php versions. But according to your code, you could simply use a foreach loop to get your data.
Your results gives back some kind of data groups, I assume those are the user ids or something similar. It could also be the version or something like it. As I do not know that here are two possible ways
Get all:
while($row = mysql_fetch_assoc($query_models)){
$model_name = $row['ModelName'];
$model_thumbnail = unserialize($row['info']);
// echo all 1x_filename values from all given "ids"
foreach ($model_thumbnail['thumbs'] as $model_id => $model_values) {
print $model_values['1x_filename'];
}
}
Get only the latest id, in case those are for some kind of versioning:
while($row = mysql_fetch_assoc($query_models)){
$model_name = $row['ModelName'];
$model_thumbnail = unserialize($row['info']);
// sort array, so highest ID (assume-ably the newest)
krsort($model_thumbnail['thumbs']);
// get array entry
$firstEntry = reset($model_thumbnail['thumbs']);
print $firstEntry['1x_filename'];
}
I just need to parse a JSON :
object(stdClass)#363 (3)
{
["type"]=> string(8) "champion"
["version"]=> string(6) "5.22.3"
["data"]=> object(stdClass)#362 (127) {
["Thresh"]=> object(stdClass)#366 (4) { ["id"]=> int(412) ["key"]=> string(6) "Thresh" ["name"]=> string(6) "Thresh" ["title"]=> string(18) "Garde aux chaînes" }
["Aatrox"]=> object(stdClass)#365 (4) { ["id"]=> int(266) ["key"]=> string(6) "Aatrox" ["name"]=> string(6) "Aatrox" ["title"]=> string(17) "Épée des Darkin" }
["Tryndamere"]=> object(stdClass)#368 (4) { ["id"]=> int(23) ["key"]=> string(10) "Tryndamere" ["name"]=> string(10) "Tryndamere" ["title"]=> string(11) "Roi barbare" } etc...
How to parse all the datas from this JSON with PHP by using object return.
Thanks in advance for help.
Use json_decode.
Returns as object:
json_decode($json_string);
Returns as associative array:
json_decode($json_string, true);
From what I guess you used json_decode and it returned object. You can now access object values using variable name let's say $var
echo $var->type; // will output champion
You can also convert json to array by providing second argument set to true
then you can access this data via
echo $var['type'];
I am trying to pull one single piece of data from a json encoded string. I managed to get the string into php's jason format, but I dont understand how to display the exact data I want (confirmations) Every time I try to access any of the data it just gives me a bracket({) and no data. I know I'm doing something wrong with retreiving the data from the variable, but I can't pu tmy finger on it.
Here's the data I want to parse, could someone please show me how to access the confirmations value from within the array of data? Thanks.
( https://projectbuilder.info/link.php?ID=jsontest&CMD=page ) [easier to read]
Source Code:
<?PHP
$return = file_get_contents("https://api.chain.com/v2/bitcoin/transactions/76e6f17cb940745255e2b8439eea5dae945a b148f1fbba98a9fb99c9a5801320?api-key-id=ae7317a1cd4ff0d12e49a77bfd8b9dec");
var_dump(json_decode($return));
echo $return[0]["confirmations"]; //one of my many attempts to get the info, also tried without the leading zero, and different numbers. I understand arrays, I just dont understand how the json data is formatted inside the array.
?>
Output Copy(what do I do with $return to get it's confirmation value of 2896?):
object(stdClass)#1 (11) {
["hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["block_hash"]=>
string(64) "00000000000000001646d024d4622a0e4a5c06299d7d776de041bc9c317be1f8"
["block_height"]=>
int(329486)
["block_time"]=>
string(20) "2014-11-11T02:10:21Z"
["chain_received_at"]=>
string(24) "2014-11-11T02:05:48.259Z"
["confirmations"]=>
int(2896)
["lock_time"]=>
int(0)
["inputs"]=>
array(2) {
[0]=>
object(stdClass)#2 (7) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_hash"]=>
string(64) "4f7b6066396e422f1cabd60767093ec6fb4480b60f206c408ad50895541bc023"
["output_index"]=>
int(0)
["value"]=>
int(144475)
["addresses"]=>
array(1) {
[0]=>
string(34) "1HPvAS96JXYUuLDs5CKNh61SvH6NJT1ykH"
}
["script_signature"]=>
string(213) "304602210087289ed01fd7d04e3c7eb5c38ea1944cbc3789658a1122610079a4f0421e2426022100fca6f5f4623bbac131f06ebdc7389ea0c76b4355da9508ecc7b02107385ee79d01 029a79a3cf6f8b90b7c1210e593a21e46c81ffbbe544eb2ab3ebbd89f33e4f8b2e"
["sequence"]=>
int(4294967295)
}
[1]=>
object(stdClass)#3 (7) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_hash"]=>
string(64) "22de0dbb8d72fac9a6e8775f6f80fa3fc991c41d33fcd5081acb49c0479f2a62"
["output_index"]=>
int(1)
["value"]=>
int(7390022)
["addresses"]=>
array(1) {
[0]=>
string(34) "158kR5o6EWWhFEZfLqGDvkgfoyi2Ep2fhA"
}
["script_signature"]=>
string(209) "304402205e2f36c9c22e02767e3accc8bc609b74d5a23c58e4a8edbac24cb4baf1e3feaa022043ad33becd57c5dee03fd1c1d8be1524281f6f49ba1e44a1a2b340f89554c42d01 031181694e14973f71d45f5f3ab73ee0f30dfa3f488bf53c44e46cf9f4f3d3d722"
["sequence"]=>
int(4294967295)
}
}
["outputs"]=>
array(2) {
[0]=>
object(stdClass)#4 (9) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_index"]=>
int(0)
["value"]=>
int(144475)
["addresses"]=>
array(1) {
[0]=>
string(34) "1P5rwnk3GYbxgpxN9M9EziLrpvoih4c8JC"
}
["script"]=>
string(85) "OP_DUP OP_HASH160 f23e1f6dd21bab989f18c14f26bf37b4e2372eef OP_EQUALVERIFY OP_CHECKSIG"
["script_hex"]=>
string(50) "76a914f23e1f6dd21bab989f18c14f26bf37b4e2372eef88ac"
["script_type"]=>
string(10) "pubkeyhash"
["required_signatures"]=>
int(1)
["spent"]=>
bool(false)
}
[1]=>
object(stdClass)#5 (9) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_index"]=>
int(1)
["value"]=>
int(7380022)
["addresses"]=>
array(1) {
[0]=>
string(34) "1DpY5Mu2qTNkwsnPwgqFnaZ1Kq7GpKDboy"
}
["script"]=>
string(85) "OP_DUP OP_HASH160 8c9efff6e8500a36c16a939054a333d81ef23166 OP_EQUALVERIFY OP_CHECKSIG"
["script_hex"]=>
string(50) "76a9148c9efff6e8500a36c16a939054a333d81ef2316688ac"
["script_type"]=>
string(10) "pubkeyhash"
["required_signatures"]=>
int(1)
["spent"]=>
bool(true)
}
}
["fees"]=>
int(10000)
["amount"]=>
int(7524497)
}
{
Have a look at the docs: http://php.net/json_decode
Convert the json-string to a php-array and save it in a variable. Then use it like any other array to get values from it. Like this:
$return = file_get_contents('http://etc.com/');
$decoded_return = json_decode($return);
echo $decoded_return['confirmations']; //Make sure the key exists, obviously
So keep in mind that $return is a string. The json_decode function returns an array, so you have to save that array in a (new - preferably) variable to get values from it.
Okay, so I'm writing an app that allows me to see steam data from a database of whoever registered.
I met a problem. Firstly, the steam API for multiple users is not standardized. (e.g. everytime you refresh this, the position of user changes (What kind of API does this?!)
Since steam does not standardize the API, I'll have to do it myself, so after doing a json_decode($url, true). It is not an assoc array.
I want to sort the assoc array by the steam ID (which is numeral) and match them against my own database of user (also contains steam ID, but can be sorted in the database), so how do I go about doing that?
E.g.
Array 1:
array(3) {
[0]=>
array(2) {
["steam_id32"]=>
string(17) "76561198025035234"
["name"]=>
string(7) "Mitsuki"
}
[1]=>
array(2) {
["steam_id32"]=>
string(17) "76561197968270056"
["name"]=>
string(3) "nrn"
}
[2]=>
array(2) {
["steam_id32"]=>
string(17) "76561197982490298"
["name"]=>
string(4) "Ximp"
}
}
Array 2:
array(1) {
["response"]=>
array(1) {
["players"]=>
array(3) {
[0]=>
array(16) {
["steamid"]=>
string(17) "76561197982490298"
["communityvisibilitystate"]=>
int(3)
["profilestate"]=>
int(1)
["personaname"]=>
string(53) "……‮‮‮‮‮‮‮‮‮‮Ximp ……FUS RO DAH"
["lastlogoff"]=>
int(1328569605)
["profileurl"]=>
string(34) "http://steamcommunity.com/id/ximp/"
["avatar"]=>
string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/f8/f8ee0cf00a2ec20417bf5b26b99fd6fb4dc176c1.jpg"
["avatarmedium"]=>
string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/f8/f8ee0cf00a2ec20417bf5b26b99fd6fb4dc176c1_medium.jpg"
["avatarfull"]=>
string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/f8/f8ee0cf00a2ec20417bf5b26b99fd6fb4dc176c1_full.jpg"
["personastate"]=>
int(1)
["realname"]=>
string(9) "I life in"
["primaryclanid"]=>
string(18) "103582791430354400"
["timecreated"]=>
int(1146939839)
["gameextrainfo"]=>
string(20) "The Binding Of Isaac"
["gameid"]=>
string(6) "113200"
["loccountrycode"]=>
string(2) "DE"
}
[1]=>
array(14) {
["steamid"]=>
string(17) "76561197968270056"
["communityvisibilitystate"]=>
int(3)
["profilestate"]=>
int(1)
["personaname"]=>
string(3) "nrn"
["lastlogoff"]=>
int(1328618220)
["profileurl"]=>
string(34) "http://steamcommunity.com/id/nrnx/"
["avatar"]=>
string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/50/50b908e0aa2c730fa0f68ab0afc8b04fddb133f1.jpg"
["avatarmedium"]=>
string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/50/50b908e0aa2c730fa0f68ab0afc8b04fddb133f1_medium.jpg"
["avatarfull"]=>
string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/50/50b908e0aa2c730fa0f68ab0afc8b04fddb133f1_full.jpg"
["personastate"]=>
int(1)
["realname"]=>
string(9) "Nathaniel"
["primaryclanid"]=>
string(18) "103582791432850562"
["timecreated"]=>
int(1092771678)
["loccountrycode"]=>
string(2) "US"
}
[2]=>
array(14) {
["steamid"]=>
string(17) "76561198025035234"
["communityvisibilitystate"]=>
int(3)
["profilestate"]=>
int(1)
["personaname"]=>
string(23) "[ProudiA] Mitsuki Sakai"
["lastlogoff"]=>
int(1328621807)
["commentpermission"]=>
int(1)
["profileurl"]=>
string(42) "http://steamcommunity.com/id/mitsukisakai/"
["avatar"]=>
string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d279f349422cbbed55adf1c8eabb0924ea0a719.jpg"
["avatarmedium"]=>
string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d279f349422cbbed55adf1c8eabb0924ea0a719_medium.jpg"
["avatarfull"]=>
string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d279f349422cbbed55adf1c8eabb0924ea0a719_full.jpg"
["personastate"]=>
int(1)
["realname"]=>
string(12) "酒井å‚è¼"
["primaryclanid"]=>
string(18) "103582791432752089"
["timecreated"]=>
int(1273714689)
}
}
}
}
For sortig array you can find a list of all function that you need here
Update:
first you must create a 1d array from a 2d or 3d you can use this code to make an easy access array and sortable (this an example):
<?php
$inArr;//This is the 2D array
$outArr = array();
for($i=0;$i<count($inArr);$i++){
$outArr[$i] = $inArr[$i][0];
?>
then you can sort it with ksort() or krsort() function.and for adding an array to another :
<?php
$stack = array("value1", "value2");
array_push($stack, "value3", "value4");
print_r($stack);
?>