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.
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 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 ?
I'm trying to get the values of a sub array out in a foreach loop.
I looked at this example as it seemed really relevant to my problem, but unfortunately I've not been able to get it to work, and I'm hoping someone here can pick up where I've gone wrong.
PHP foreach not working with sub-array
I have an array called $booked and inside that array I have a sub array $booked['30_booked'].
Within that second array there are multiple values, such as title, name, address etc.
My code currently looks like this:
foreach($booked['30_booking'] as $new_var){
var_dump('</br>', $booked['30_booking']);
var_dump('</br>', $new_var);
var_dump($new_var['title'], $new_var->title, $booked['30_booking']->title); exit();
}
I've output the data as you can see above in var_dump statements to try and get one of these methods to work.
Unfortunately nothing within $new_var is pulling out the title, but $booked['30_booking']->title
Have I not put it into the foreach statement correctly?
All help appreciated - thanks!
EDIT:
Main array output snippet:
array(6) { ["30_booked"]=> object(stdClass)#21 (34) { ["id"]=> string(2) "30" ["title"]=> string(2) "Ms" ["firstname"]=> string(5) "FIRST NAME" ["surname"]=> string(9) "LAST NAME" ["address"]=> string(6) "- -- -" ["postcode"]=> string(7) "FAK E99" ["country"]=> string(14) "United Kingdom" ["phone"]=> string(11) "01221111111" ["alt_phone"]=> string(0) "" ["email"]=> string(25) "fake#fake.co.uk" ["notes"]=> string(8) "FAKE DEAL" } }
EDIT 2:
Sub Array $booked['30_booking'] snippet:
object(stdClass)#21 (34) { ["id"]=> string(2) "30" ["title"]=> string(2) "Ms" ["firstname"]=> string(5) "FIRST NAME" ["surname"]=> string(9) "LAST NAME" ["address"]=> string(6) "- -- -" ["postcode"]=> string(7) "FAK E99" ["country"]=> string(14) "United Kingdom" ["phone"]=> string(11) "01221111111" ["alt_phone"]=> string(0) "" ["email"]=> string(25) "fake#fake.co.uk" ["notes"]=> string(8) "FAKE DEAL" }
EDIT 3:
My var_dump of the $new_var by itself is bringing back the value of the id - but when I try and get the second value out the sub array "title" it doesn't return anything.
FINAL FIX:
Thanks to Kita I realised I was returning a std class object and not a second array, something that I stupidly missed the first time round. Because of that I can't actually foreach on the object.
Which led me to this post which will help me fix the issue:
PHP foreach array with stdClass Object
Thank you very much for all your help!!!
You expected an array inside $booked['30_booking'] but in fact there was a stdClass object.
array(6) {
["30_booked"]=> object(stdClass)#21 (34) {
["id"]=> string(2) "30"
["title"]=> string(2) "Ms"
["firstname"]=> string(5) "FIRST NAME"
["surname"]=> string(9) "LAST NAME"
["address"]=> string(6) "- -- -"
["postcode"]=> string(7) "FAK E99"
["country"]=> string(14) "United Kingdom"
["phone"]=> string(11) "01221111111"
["alt_phone"]=> string(0) ""
["email"]=> string(25) "fake#fake.co.uk"
["notes"]=> string(8) "FAKE DEAL"
}
//I assume you have left out the other array elements from the Main array snippet.
}
Getting stdClass instead of array usually happens when you parse a JSON string with json_decode() without the second parameter.
// without second parameter
var_dump( json_decode( '{"id":"30"}' ) );
object(stdClass)#1 (1) {
["id"]=>
string(2) "30"
}
// 'true' as second parameter
var_dump( json_decode( '{"id":"30"}', true ) );
array(1) {
["id"]=>
string(2) "30"
}
Above examples are hosted at: http://ideone.com/GNMRlD
Since stdClass object itself does not provide iteration functionality, using it with foreach will yield errors.
You can convert stdClass into array with functions such as http://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/ .
For eg. if your array looks like below. foreach which I used will be working
$new_array = array(array('total'=>array('title'=>'test','text'=>'text')));
foreach ($new_array as $val)
{
print_r($val['total']['title']); // Use this for array
print_r($val->total->title); // Use this for object
}
if your array looks like below. Use the below foreach.
$new_array = array(array('total'=>array(array('title'=>'test','text'=>'text'),array('title'=>'test1','text'=>'text1'))));
foreach ($new_array as $val)
{
foreach($val['total'] as $new_val)
{
print_r($new_val['title']);
}
}
You can try this, I hope that helps you
foreach($booked as $new_var){
var_dump('</br>', $new_var);
var_dump('</br>',$new_var['30_booking']->title);
}
I think in your foreach there is mistake of word 30_booking
since in your main array out put display it shows 30_booked
check that also
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);
?>