Merging multi dim arrays on ID in array value - php

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"
}

Related

Sorting php array in Redis

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 ?

filter data from an array - php

I have an array with the friends data including their name, id, gender. I want to extract the data from array which have opposite gender.
For example -
My gender is "Male"
Returned data -
[118]=> object(stdClass)#121 (3) {
["name"]=> string(9) "Rawa Su"
["gender"]=> string(4) "male"
["id"]=> string(15) "1000019100"
}
[119]=> object(stdClass)#122 (3) {
["name"]=> string(11) "Anil Gaj"
["gender"]=> string(4) "male"
["id"]=> string(15) "1000034656"
}
[120]=> object(stdClass)#123 (3) {
["name"]=> string(13) "Ankur Tri"
["gender"]=> string(4) "male"
["id"]=> string(15) "1000022271"
}
[121]=> object(stdClass)#124 (3) {
["name"]=> string(13) "Chuck Ell"
["gender"]=> string(4) "male"
["id"]=> string(15) "10000185038"
}
[122]=> object(stdClass)#125 (3) {
["name"]=> string(15) "Madhuri Tat"
["gender"]=> string(6) "female"
["id"]=> string(15) "1000880932"
}
Now i want to randomly fetch the data which have gender = female. I don't have any clue how this can be done. Any help would be appreciated. Thank you.
Okay lets say your array with all of the data is named $data:
$res = array();
foreach($data as $person) {
if(strcasecmp($person['gender'], 'female') == 0)
$res[] = $person;
}
Now you have a new array called $res which contains all females.
Nevertheless, i would filter the raw data during the sql fetch from the database,
this should be the same effort and give more performance in a long time view.
If you now want to have one of the females randomly, do something like this:
$number_of_persons = count($res);
$random_female = $res[rand(0, $number_of_persons-1)];
Why -1?
Arrays start at index 0, so you need a -1 at the total amount of persons.

php code - trying to loop array

I am noticing some strange behavior while looping through some data. I'm sure it's something simple, but I can't seem to be able to find the bug.
I have the following logic:
<?php
print 'dumping data : <BR>';
var_dump($portvlan);
print '<BR>';
print 'looping through data: <BR>';
foreach ($portvlan as $vlandetail){
echo 'Vlanid: '.$vlandetail['VlanId'].'<BR>';
echo 'Name: '.$vlandetail['Name'].'<BR>';
echo 'Mode: '.$vlandetail['Mode'].'<BR>';
}
?>
This is the output that I'm getting:
dumping data :
array(3) { ["VlanId"]=> string(2) "33" ["Name"]=> string(6) "USR_33" ["Mode"]=> string(6) "Access" }
looping through data:
Vlanid: 3
Name: 3
Mode: 3
Vlanid: U
Name: U
Mode: U
Vlanid: A
Name: A
Mode: A
What I was expecting was to see it print a row with 3 cells, with the following values:
33, USR_33, Access.
Can you tell me where I'm going wrong?
Thanks.
EDIT 1
This logic works fine when the $portvlan array has more than one entry.
For example, on another set of data, the var_dump gives this result:
array(6) { [0]=> array(3) { ["VlanId"]=> string(1) "1" ["Name"]=> string(1) "1" ["Mode"]=> string(5) "Trunk" } [1]=> array(3) { ["VlanId"]=> int(2) ["Name"]=> int(2) ["Mode"]=> string(5) "Trunk" } [2]=> array(3) { ["VlanId"]=> int(3) ["Name"]=> int(3) ["Mode"]=> string(5) "Trunk" } [3]=> array(3) { ["VlanId"]=> int(4) ["Name"]=> int(4) ["Mode"]=> string(5) "Trunk" } [4]=> array(3) { ["VlanId"]=> int(5) ["Name"]=> int(5) ["Mode"]=> string(5) "Trunk" } [5]=> array(3) { ["VlanId"]=> string(2) "33" ["Name"]=> string(2) "33" ["Mode"]=> string(5) "Trunk" } }
And the loop logic works fine.
$vlandetail will be populated with a different item from the array on every iteration of the loop. You shouldn't be treating $vlandetail as an array. Just use it directly.
To get the key name of the array entry, you have to change the loop structure to this:
foreach ($portvlan as $key => $vlandetail) {
echo $key . ': ' . $vlandetail . '<br>';
}
How you get your $portvlan?
If you can know is $portvlan is not an array of $vlandetail, you can simply do this,
$portvlan = array($portvlan);
// start loop
Or you can do this before you loop it, to make sure $portvlan is a numerical array formed by $vlandetail
$portvlan = (isset($portvlan[0]))?$portvlan:array($portvlan);
// start loop

How do I get the data out of this array? Single sign on

I have implemented a Single Sign on function using Janrain. It outputs this data (amongst other data). Is it possible to break this down and extract only the displayName for example so that I may place it in a variable?
auth_info:
array(3) {
["stat"]=>
string(2) "ok"
["profile"]=>
array(10) {
["providerName"]=>
string(8) "Facebook"
["identifier"]=>
string(48) "removed"
["preferredUsername"]=>
string(10) "OllieJones"
["displayName"]=>
string(11) "Ollie Jones"
["name"]=>
array(3) {
["formatted"]=>
string(11) "Ollie Jones"
["givenName"]=>
string(5) "Ollie"
["familyName"]=>
string(5) "Jones"
}
Here is the script that creates it and where I would like to define the variable https://github.com/janrain/Janrain-Sample-Code/blob/master/php/rpx-token-url.php
Many Thanks
Try $auth_info['profile']['displayName']

How to sort php array by value and add an array to another

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);
?>

Categories