Tunday Posted June 19, 2015 Share Posted June 19, 2015 Array ( [0] => Array ( [Height] => 64 [Purpose] => Thumbnail [Purposes] => Array ( [0] => Thumbnail ) [ResizeUrl] => http://demo/demo.png => http://demo/demo.png [Width] => 64 ) [1] => Array ( [Height] => 95 [Purpose] => SellImage [Purposes] => Array ( [0] => SellImage ) [ResizeUrl] => http://demo/demo.png => http://demo/demo.png [Width] => 420 ) ) I am currently trying go get the Array value if Purposes = Thumbnail. To do this I have setup a for each loop and if statement: foreach(Array as $key => $val) { if ($val['Purpose'] == "Thumbnail") { print_r($val[url]); } } I have done some testing and I have seen this take over 9 seconds to return the url. My question is their a better way to get $val? Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/ Share on other sites More sharing options...
Tunday Posted June 19, 2015 Author Share Posted June 19, 2015 Apologies for the multiple post was having issues when posting this. Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514320 Share on other sites More sharing options...
Muddy_Funster Posted June 19, 2015 Share Posted June 19, 2015 there is no way that running that code should take 9 seconds unless your doing it on a 386. either you're not giving all the info or something somewhere is seriously wrong with your setup. Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514325 Share on other sites More sharing options...
rwhite35 Posted June 19, 2015 Share Posted June 19, 2015 First you have a multi-dimensional array, an array or arrays. In your case the first array is indexed. So you can deal with that array using a for loop. The array inside the indexed array is associative, so you can deal with that array using the foreach. Using for outer/inner looping construct: for ($i=0;$i>=count($Array);$i++) { // loop through each indexed array foreach ($Array[$i] as $key=>$value) { // loop through each associative array $Url = ($value == $string) ? $Array[$i]['Uri'] : null; // assign url to variable if $string is thumbnail } } Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514328 Share on other sites More sharing options...
Muddy_Funster Posted June 19, 2015 Share Posted June 19, 2015 First you have a multi-dimensional array, an array or arrays. In your case the first array is indexed. So you can deal with that array using a for loop. The array inside the indexed array is associative, so you can deal with that array using the foreach. Using for outer/inner looping construct: for ($i=0;$i>=count($Array);$i++) { // loop through each indexed array foreach ($Array[$i] as $key=>$value) { // loop through each associative array $Url = ($value == $string) ? $Array[$i]['Uri'] : null; // assign url to variable if $string is thumbnail } } First, you can deal with any array using a foreach Second - nesting loops is detrimental to performance, especially when you don't break out properly when the desired point is reached. Instead of iterating through to top level arrays and checking values within them you are suggesting that the OP could improve performance by iterating trough all 16 array entries even if the value is found first check. That's just nonsense. Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514330 Share on other sites More sharing options...
Tunday Posted June 19, 2015 Author Share Posted June 19, 2015 there is no way that running that code should take 9 seconds unless your doing it on a 386. either you're not giving all the info or something somewhere is seriously wrong with your setup. There can be up to 10 values in the array list. I only included two for my example. Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514331 Share on other sites More sharing options...
rwhite35 Posted June 19, 2015 Share Posted June 19, 2015 (edited) First, you can deal with any array using a foreach Second - nesting loops is detrimental to performance, especially when you don't break out properly when the desired point is reached. Instead of iterating through to top level arrays and checking values within them you are suggesting that the OP could improve performance by iterating trough all 16 array entries even if the value is found first check. That's just nonsense Okay, you could shorten the process with this modification. It only evaluates one element of the associative array. for ($i=0;$i>=count($Array);$i++) { // loop through indexed array $Url = ($Array[$i]['Purpose'] == $string) ? $Array[$i]['Uri'] : null; // assign url to variable if $string is thumbnail } Edited June 19, 2015 by rwhite35 Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514334 Share on other sites More sharing options...
Muddy_Funster Posted June 19, 2015 Share Posted June 19, 2015 that's still not 9 seconds worth, 9 seconds to a computer is a long time something else must be affecting the performance. Can you explain how each of your arrays is being created, do you have control over the order of elements? Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514337 Share on other sites More sharing options...
Tunday Posted June 19, 2015 Author Share Posted June 19, 2015 (edited) So I am currently using: https://xboxapi.com/ My aim to get the game image image of a users played game. foreach($xbox360Games['titles'] as $val) { //This Loop gets all xbox360 games from a userid if ($val['currentGamerscore'] > 0 ){ //this rules out non game appes iplayer,twitch etc $xbox360Games = getGameInfo(dechex($val['titleId'])); //GetGameInfo is a fuction which will return the array for each game as listed in my original post foreach($xbox360Games['Items'][0]['Images']as $val2) { if ($val2['Purpose'] == "BoxArt" && $val2['Height'] == 300) { $url = $val2['Url']; $htmlxbox .= "<img src=\" $url \" title= Current Game Score:". $val['currentGamerscore'] . ">"; break; } } } } Edited June 19, 2015 by Tunday Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514390 Share on other sites More sharing options...
CroNiX Posted June 19, 2015 Share Posted June 19, 2015 foreach($xbox360Games['titles'] as $val) { // ... $xbox360Games = getGameInfo(dechex($val['titleId'])); //Don't create a new $xbox360Games here, you're wiping out the original array that you're looping over above! } Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514397 Share on other sites More sharing options...
Tunday Posted June 20, 2015 Author Share Posted June 20, 2015 foreach($xbox360Games['titles'] as $val) { // ... $xbox360Games = getGameInfo(dechex($val['titleId'])); //Don't create a new $xbox360Games here, you're wiping out the original array that you're looping over above! } I agree. However, I do not reuse the top level $xbox360Games again inside the loop. Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514418 Share on other sites More sharing options...
Barand Posted June 20, 2015 Share Posted June 20, 2015 It's the programming equivalent of sitting on a branch while you saw through it. You are processing an array but in that process you overwrite the very array you are trying to process Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514419 Share on other sites More sharing options...
CroNiX Posted June 20, 2015 Share Posted June 20, 2015 I agree. However, I do not reuse the top level $xbox360Games again inside the loop. I disagree Here, you break out of the 2nd loop ONLY if the condition passes, which returns you to the original first loop where you have already overridden the original array. If the condition never passes in the 2nd loop, it also returns to the first original loop. if ($val2['Purpose'] == "BoxArt" && $val2['Height'] == 300) { $url = $val2['Url']; $htmlxbox .= "<img src=\" $url \" title= Current Game Score:". $val['currentGamerscore'] . ">"; break; } Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514455 Share on other sites More sharing options...
Tunday Posted June 22, 2015 Author Share Posted June 22, 2015 I disagree Here, you break out of the 2nd loop ONLY if the condition passes, which returns you to the original first loop where you have already overridden the original array. If the condition never passes in the 2nd loop, it also returns to the first original loop. if ($val2['Purpose'] == "BoxArt" && $val2['Height'] == 300) { $url = $val2['Url']; $htmlxbox .= "<img src=\" $url \" title= Current Game Score:". $val['currentGamerscore'] . ">"; break; } Sorry I don't understand this fully. This if statement will always be true. As every game has a Box Art image with height of 300. Since the 2nd loop is indented and I have begun the loop I don't think I am overwriting $xbox360Games. I have now changed this variable in the 2nd loop as I am confusing myself now . Also, Please can some help me with the sleep of this algorithm as I am still experiencing upto 9 seconds for each cycle in the 2nd loop. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514559 Share on other sites More sharing options...
CroNiX Posted June 22, 2015 Share Posted June 22, 2015 If the if() will "always be true"...then what's the point of the if()? Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514566 Share on other sites More sharing options...
Tunday Posted June 22, 2015 Author Share Posted June 22, 2015 There will be 9 items in an array the order is not always the same. The if statement will only match once and this is where I print the html Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514568 Share on other sites More sharing options...
Zane Posted June 22, 2015 Share Posted June 22, 2015 foreach(Array as $key => $val) { if (in_array('Thumbnail', $val['Purposes']) { echo $val['url'] . "\n"; } else continue; } Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514583 Share on other sites More sharing options...
Tunday Posted June 25, 2015 Author Share Posted June 25, 2015 Cant use in_array since this does not work on multi dimensional arrays. I found the reason for my code taking a while. For each image I want to display and am sending one http request. So If I want to get 9 images I send 9 http requests. Hard to say if this is directly related to my internet connection or code. will try to test on another network and compare the speeds Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514890 Share on other sites More sharing options...
maxxd Posted June 25, 2015 Share Posted June 25, 2015 Unless the images are enormous, 9 seconds overall is still a ridiculously long load time. Also, note that Zane isn't using in_array on a multidimensional array, he's using it on $val['Purposes'], which is a simple indexed array. It just so happens that the indexed array 'Purpose' happens to be contained within another array. Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1514896 Share on other sites More sharing options...
Tunday Posted June 26, 2015 Author Share Posted June 26, 2015 Hi All, Thanks for your support on this. I can confirm the issue is to do with me sending many http request on average it will take 1s for me to send and read a http request. I am currently using curl and need to because I am adding a key which allows me to use the api. I am unable to insert the key thus unable to compare speeds with file_get_content. Currently working on a cache system for the data. Quote Link to comment https://forums.phpfreaks.com/topic/296911-multinational-array/#findComment-1515062 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.