Jump to content

Search the Community

Showing results for tags 'arrays'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Welcome to PHP Freaks
    • Announcements
    • Introductions
  • PHP Coding
    • PHP Coding Help
    • Regex Help
    • Third Party Scripts
    • FAQ/Code Snippet Repository
  • SQL / Database
    • MySQL Help
    • PostgreSQL
    • Microsoft SQL - MSSQL
    • Other RDBMS and SQL dialects
  • Client Side
    • HTML Help
    • CSS Help
    • Javascript Help
    • Other
  • Applications and Frameworks
    • Applications
    • Frameworks
    • Other Libraries
  • Web Server Administration
    • PHP Installation and Configuration
    • Linux
    • Apache HTTP Server
    • Microsoft IIS
    • Other Web Server Software
  • Other
    • Application Design
    • Other Programming Languages
    • Editor Help (Dreamweaver, Zend, etc)
    • Website Critique
    • Beta Test Your Stuff!
  • Freelance, Contracts, Employment, etc.
    • Services Offered
    • Job Offerings
  • General Discussion
    • PHPFreaks.com Website Feedback
    • Miscellaneous

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


AIM


MSN


Website URL


ICQ


Yahoo


Jabber


Skype


Location


Interests


Age


Donation Link

Found 13 results

  1. I have my data array which i converted to json by splitting the keys using for each loop. Data: $data = Array ( [table6-ios-camt] => [table6-ios-iamt] => [table6-ios-samt] => [table6-ios-type] => [table6-isd-camt] => 0 [table6-isd-iamt] => 0 [table6-isd-samt] => 0 [table6-isd-type] => [table6-ios-csamt] => [table6-isd-csamt] => 0 [table6-other-camt] => [table6-other-iamt] => [table6-other-samt] => [table6-other-type] => [table6-tran1-camt] => 0 [table6-tran1-iamt] => [table6-tran1-samt] => 0 [table6-tran1-type] => [table6-tran2-camt] => 0 [table6-tran2-iamt] => [table6-tran2-samt] => 0 [table6-tran2-type] => [table6-itc_3b-csamt] => 0 [table6-dump_iog-camt] => [table6-dump_iog-iamt] => [table6-dump_iog-samt] => [table6-dump_iog-type] => [table6-dump_oth-camt] => 4386.09 [table6-dump_oth-iamt] => [table6-dump_oth-samt] => 4386.09 [table6-dump_oth-type] => [table6-itc_clmd-camt] => [table6-itc_clmd-iamt] => [table6-itc_clmd-samt] => [table6-itc_clmd-type] => [table6-total_10-camt] => 4386.09 [table6-total_10-iamt] => 0 [table6-total_10-samt] => 4386.09 [table6-total_10-type] => [table6-total_11-type] => [table6-total_8-csamt] => 0 [table6-total_9-csamt] => 0 [table6-dump_iog-csamt] => [table6-dump_isrc-camt] => [table6-dump_isrc-iamt] => [table6-dump_isrc-samt] => [table6-dump_isrc-type] => [table6-dump_oth-csamt] => [table6-iog_goods-camt] => [table6-iog_goods-iamt] => [table6-iog_goods-samt] => [table6-iog_goods-type] => Capital Goods [table6-itc_clmd-csamt] => [table6-total_10-csamt] => 0 [table6-dump_isrc-csamt] => [table6-iog_goods-csamt] => [table6-iog_inputs-camt] => [table6-iog_inputs-iamt] => [table6-iog_inputs-samt] => [table6-iog_inputs-type] => Input Services [table6-iog_inputs-csamt] => [table6-iog_services-camt] => [table6-iog_services-iamt] => [table6-iog_services-samt] => [table6-iog_services-type] => Input Services [table6-supp_non_rchrg_goods-camt] => [table6-supp_non_rchrg_goods-iamt] => [table6-supp_non_rchrg_goods-samt] => [table6-supp_non_rchrg_goods-type] => Capital Goods [table6-supp_rchrg_reg_goods-camt] => [table6-supp_rchrg_reg_goods-iamt] => [table6-supp_rchrg_reg_goods-samt] => [table6-supp_rchrg_reg_goods-type] => Capital Goods [table6-supp_non_rchrg_goods-csamt] => [table6-supp_non_rchrg_inputs-camt] => 4386.09 [table6-supp_non_rchrg_inputs-iamt] => [table6-supp_non_rchrg_inputs-samt] => 4386.09 [table6-supp_non_rchrg_inputs-type] => Inputs [table6-supp_rchrg_reg_goods-csamt] => [table6-supp_rchrg_reg_inputs-camt] => [table6-supp_rchrg_reg_inputs-iamt] => [table6-supp_rchrg_reg_inputs-samt] => [table6-supp_rchrg_reg_inputs-type] => Input Services [table6-supp_non_rchrg_inputs-csamt] => [table6-supp_rchrg_reg_inputs-csamt] => [table6-supp_rchrg_unreg_goods-camt] => [table6-supp_rchrg_unreg_goods-iamt] => [table6-supp_rchrg_unreg_goods-samt] => [table6-supp_rchrg_unreg_goods-type] => Capital Goods [table6-supp_non_rchrg_services-camt] => [table6-supp_non_rchrg_services-iamt] => [table6-supp_non_rchrg_services-samt] => [table6-supp_non_rchrg_services-type] => Input Services [table6-supp_rchrg_reg_services-camt] => [table6-supp_rchrg_reg_services-iamt] => [table6-supp_rchrg_reg_services-samt] => [table6-supp_rchrg_reg_services-type] => Input Services [table6-supp_rchrg_unreg_goods-csamt] => [table6-supp_rchrg_unreg_inputs-camt] => [table6-supp_rchrg_unreg_inputs-iamt] => [table6-supp_rchrg_unreg_inputs-samt] => [table6-supp_rchrg_unreg_inputs-type] => Input Services [table6-supp_non_rchrg_services-csamt] => [table6-supp_rchrg_reg_services-csamt] => [table6-supp_rchrg_unreg_inputs-csamt] => [table6-supp_rchrg_unreg_services-camt] => [table6-supp_rchrg_unreg_services-iamt] => [table6-supp_rchrg_unreg_services-samt] => [table6-supp_rchrg_unreg_services-type] => Input Services [table6-supp_rchrg_unreg_services-csamt] => ); json: { "table6":{ "ios":{ "camt":0, "iamt":0, "samt":0, "csamt":0 }, "isd":{ "camt":0, "iamt":0, "samt":0, "csamt":0 }, "other":{ "camt":0, "iamt":0, "samt":0, "csamt":0 }, "tran1":{ "camt":0, "iamt":0, "samt":0, "csamt":0 }, "tran2":{ "camt":0, "iamt":0, "samt":0, "csamt":0 }, "itc":{ "camt":0, "iamt":550, "samt":230, "csamt":680 }, "iog":[ { "itc_typ":"cg", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"ip", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"is", "camt":0, "samt":0, "iamt":0, "csamt":0 } ], "supp_non_rchrg":[ { "itc_typ":"cg", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"ip", "camt":4386.09, "samt":4386.09, "iamt":0, "csamt":0 }, { "itc_typ":"is", "camt":0, "samt":0, "iamt":0, "csamt":0 } ], "supp_rchrg_reg":[ { "itc_typ":"cg", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"ip", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"is", "camt":0, "samt":0, "iamt":0, "csamt":0 } ], "supp_rchrg_unreg":[ { "itc_typ":"cg", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"ip", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"is", "camt":0, "samt":0, "iamt":0, "csamt":0 } ] } } The next thing i want to do is to unset the keys or entire inner objects of the json. Keeping in mind the following conditions: 1.If all the keys in the objects are 0 then the entire object will be removed (easy part). 2.If a key in the object has any value other than 0 , the object should not be removed and the key with "0" value should also be there. i've tried splitting value and using conditions to check for 0 or null values but the problem with that is it removes all keys that have 0 values thus failing condition 2 for the code. $vars = $data; $count = ""; $finalArray = array(); foreach ($vars as $keys => $values) { foreach ($values as $k => $v) { foreach ($v as $ke => $ve) { if ($keys == "table6") { $count = (array)$v; foreach ($count as $ck => $cv) { if ($cv != "0") { $flag = true; break; } } if($flag == false) { array_push($finalArray, $count); } } } } print_r($finalArray); die(); I expeced the output to contain the objects with key value pairs that does not contain 0 value but everything prints here.
  2. I dont understand arrays in PHP and its necessary to finally fess up and ask for help. It seems so trivial,, but I've spent Whole DAYS trying to make sense of this. (I came from assemble language programming). I am receiving UDP packets of a known size. in this case, 4 bytes of binary data. It gets unpacked ( I dont know why. I cut and pasted this simple parser from an example). All I want is the array[0] byte. I'm trying to avoid complex loops and pointers. // First lets grab a data packet and put our few bytes into the variable $buf while(1) { socket_recvfrom($socket, $buf, 16, 0, $from, $port); // Easy! but the UDP data in $buf is binary not useful like this. //Convert to array of decimal values $array = unpack("C*", $buf); // 'C' tells unpack to make unsigned chars from the binary. //Convert decimal values to ASCII characters: $chr_array = array(); // wait a second. What is happening here? for ($i = 0; $i < count($array); $i++) { $chr_array[] = chr($array[$i]); // Whats going on here? Anyone? } // So now I can assume the first byte of this $chr_array[] is my prize, right? $mychr = chr_array[0]; // Nope. Looking at $chr_array[0] either reveals a zero or the actual word "Array", depending on how late I've stayed up. print_r($array); // This is my debugger. It shows SOMETHING is working. echo "Received $mychr from remote $address $from and remote port $port" . PHP_EOL; } socket_close($sock); Output of above code... assuming the value read was 60 decimal: Array ( [1] => 60 [2] => 0 [3] => 0 [4] => 0 ) Received 0 from remote address nnn.nnn.nnn.nnn and remote port 8888 So print_r knows what to do, the last line of my code doesn't. Any help would be appreciated, as this goes to the heart of my issue of being mystified by how PHP makes and processes arrays. Its clearly not just a string of numbers. I'm trying to pull ONE printable number from that buffer. Halp!
  3. Hi all, I am trying to echo reset($starttimes) to show the first value in my $starttimes array however i it is only showing the second value in the array and skipping the first i want to echo. The statement I have written is inside a while statement however I am not sure if that is where lies the problem. Please take a look at line 29 here: http://pastebin.com/2KQ7kgy9
  4. Need Help!!! I am trying to build an application in php without database connectivity. I have a index page where i have 6 users. 1 among the 6 users will act as admin. I am using array to get the username & password When the users logs in, some of the options should be disabled or non-editable. Assume "raj" plays the admin role Code what i have: <?php session_start(); /* Starts the session */ /* Check Login form submitted */ if(isset($_POST['Submit'])){ /* Define username and associated password array */ $logins = array('raj' => 'raj123@123','ram' => 'ram@123','dev' => 'dev@123','dave' => 'dave@123','Sugi' => 'sugi@123','raki' => 'raki@123','sam' => 'sam@123'); /* Check and assign submitted Username and Password to new variable */ $Username = isset($_POST['Username']) ? $_POST['Username'] : ''; $Password = isset($_POST['Password']) ? $_POST['Password'] : ''; /* Check Username and Password existence in defined array */ if (isset($logins[$Username]) && $logins[$Username] == $Password){ /* Success: Set session variables and redirect to Protected page */ $_SESSION['UserData']['Username']=$logins[$Username]; header("location:signin.php"); exit; } else { /*Unsuccessful attempt: Set error message */ $msg="<span style='color:red'>Invalid Login Details</span>"; } } ?> Any help on this will be much appreciated!!!! Thanks
  5. Hi all, I am using foreach statement to gather records and program levenshtein to detect any misspellings and suggesting the correct words. The thing is i am unable to convert all those strings into a single array. After trying to correct this issue so many times, i wonder if it is really possible to convert strings into a single array? Any input is welcome
  6. I'm using an array based off of two other arrays in order to get a count of product IDs per customer. The following array is what's printing now: Array ( [5] => 4 ) Array ( [5] => 1 ) Array ( [5] => 1 ) Array ( [5] => 1 ) Array ( [5] => 2 ) So the [5] is the product number, the number after => is the quantity per customer (each array line represents a customer). I want to continue having my source array data at the customer level, but what I really want right now is one main array for each product number with totals in the array, so for the above, my desired result is this: array ( [5] => 9 ) here's what gives me the output I currently have: $skuTots = array_fill_keys(array_unique(array_column($skuResult, 'sku_id')), 0); foreach ($skuResult as $rec) { $skuTots[$rec['sku_id']] += $dealerResult[$rec['dealer_id']]; } print_r($skuTots); How can I get this desired change?
  7. Hi guys, i've been struggling with a problem regarding reading the JSON structure, decoding it and than trying to extract a UNIQUE key structure from it. So let me show you by example what im trying to do here. Let's take a look at this JSON: { "status": true, "message": "Everything was ok", "posts":[ { "id": 1, "post_type": "type_a", "posted_on": "", "updated_on":"", "user":{ "id": 1, "name": "" }, "num_comments": 2, "comments":[ { "id": 1, "comment":"", "commented_on":"", "user":{ "id": 1, "name": "", "profile_image": "" } }, { "id": "", "comment": "", "commented_on": "", "user":{ "id": "", "name": "", "profile_image":"" } } ], "message": "", "images":[ { "image":"", "image_large":"" } ] }, { "id": 2, "post_type": "type_b", "posted_on": "", "updated_on":"", "notes":{ "id": 1, "name": "" }, "user":{ "id": 1, "name": "" }, "num_comments":"0", "images":[ { "image":"", "image_large":"" } ] }, ] } So as you can see from the example the structure of the two post arrays varies, one has a comments section, the other one doesnt, one has a notes section, the other one doesnt etc etc. What i would somehow like to be able to do is extract a UNIQUE JSON structure for this document. Meaning i only care about the KEYS not the values and i wanna show the user what can he expect and what are the possibilities. Here is my code so far: function keys_are_equal($array1, $array2, $key) { return !array_diff_key($array1, $array2) && !array_diff_key($array2, $array1); } function prettyParseJson($json) { $decoded = json_decode($json, true); if($decoded && is_array($decoded)) { return recursiveJsonRead($decoded); } } function recursiveJsonRead($data) { $previous_array = array(); $unique_array = array(); foreach ($data as $key => $val) { if(is_array($val)) { if(!empty($val)) { if(keys_are_equal($previous_array, $val, $key)) { } else { $unique_array[$key] = gettype($val); $unique_array[$key] = recursiveJsonRead($val); $previous_array = $val; } } } else { $unique_array[$key] = gettype($val); } } return $unique_array; } As you can see im trying to decode the json, im trying to walk through it and extract all keys and their types but currently it only works if the structure is ALWAYS the same, if it varies if creates duplicates of keys that are already defined in a previous array. And again i hope im explaing this correctly but all i wanna do is be able to load a JSON file and read out its UNIQUE key structure. If someone can help me out it would be great cos im losing my mind over it.
  8. I have to complete a simple task which says 'use foreach to print the value of the sum of two cards, the deck has 16 cards and each card has one number and one colour'. Moreover; I can write four cards and four numbers, but how do I write all sixteen cards (I would prefer if they could sit abreast - parallel to each other)? The code I have so far is this: <?php $a = array( "Red" => 1, "Blue" => 2, "Green" => 3, "Yellow" => 4, ); foreach ($a as $key => $val) { echo "<h4>$key => $val.</h4>"; } ?> // Output: Red => 1. Blue => 2. Green => 3. Yellow => 4.
  9. I'm developing a database-powered quiz and need help upgrading the basic script. The original script (posted below) works fine, but it has some limitations. So someone suggested an alternate script (posted next) that turns each answer (or group of answers into an array within an array. However, I can't get the new script to work. My latest attempt is posted below the first two scripts. var_dump($_POST) tells me that the answer key ($answers) is working in all examples, but that's about all. One error message I'm getting is array_diff(): Argument #1 is not an array - in reference to this code: if(is_array($answer)){ if(array_diff($qa[$num], $answer) == array()){ I get the same error message if I change it to this: if(array_diff($qa, $answer) == array()){ If a quiz has 10 questions, then each question has an ID (e.g. q1 or q10). If the first question has four possible answers, each answer has its own ID, from q1-A to q1-D. I posted the HTML for two questions at the end of this post. Sorry for all the code. I think my problem is relatively simple, but I just can't figure out what I'm doing wrong. Thanks! ORIGINAL SCRIPT if (isset($_POST)): $totalCorrect = 0; $answers = array(1 => 'A', 2 => 'Jupiter', 3 => 'C', 4 => 'D', 5 => 'A', 6 => 'C', 7 => 'C', 8 => 'C', 9 => 'B', 10 => array('A','B','C')); foreach ($answers as $num => $answer): $question = 'q'.$num; if(is_array($answer) && isset($_POST[$question])){ $ans_cnt = count($answer); $ans_value = (1 / $ans_cnt); $post_cnt = count($_POST[$question]); //find matches between answer array and post array $matches = array_intersect($answer,$_POST[$question]); $good_answers = count($matches); //Get bad answer count, which be be subtracted from overall score $bad_answers = 0; foreach($_POST[$question] as $post_answer): if(!in_array($post_answer,$answer)): $bad_answers++; endif; endforeach; if($good_answers ==3 && $bad_answers==0){ $result = 1; }else{ $result = 0; } if(($post_cnt != $ans_cnt) || ($post_cnt == $ans_cnt && $ans_cnt != count($matches))){ $result = $result * $ans_value; $totalCorrect = $totalCorrect + $result; }else{ $totalCorrect++; } }elseif(isset($_POST[$question]) && strtolower($_POST[$question]) === strtolower($answer)){ $totalCorrect++; } endforeach; $pct = round( (($totalCorrect/count($answers)) * 100), 0); echo $totalCorrect.' correct ('.$pct.'%)'; endif; REVISED SCRIPT if ($_SERVER['REQUEST_METHOD'] == 'POST') { $qa = 'q'.$num; $correct = 0; $answers = array(1 => array('A'), 2 => array('Jupiter'), 3 => array('C'), 4 => array('D'), 5 => array('A'), 6 => array('C'), 7 => 'C', 8 => 'C', 9 => 'B', 10 => array('A','B','C')); $total = count($answers); foreach($answers as $k => $v){ if(is_array($v)){ if(array_diff($qa[$k], $v) == array()){ $correct++; } } else if($qa[$k] === $v){ $correct++; } } $grade= ($correct/count($answers))*100; echo"<p>Score $grade%</p>"; } MY LATEST TEST if ($_SERVER['REQUEST_METHOD'] == 'POST') { $correct = 0; $answers = array(1 => array('A'), 2 => array('Jupiter'), 3 => array('C'), 4 => array('D'), 5 => array('A'), 6 => array('C'), 7 => 'C', 8 => 'C', 9 => 'B', 10 => array('A','B','C')); $total = count($answers); foreach($answers as $num => $answer){ $qa = 'q'.$num; if(is_array($answer)){ if(array_diff($qa[$num], $answer) == array()){ $correct++; } } else if($qa[$num] === $answer){ $correct++; } } $grade= ($correct/count($answers))*100; $grade2= ($correct/count($answer))*100; echo"<p>Score $grade%</p>"; echo"<p>Score2 $grade2%</p>"; // echo $totalCorrect.' correct ('.$grade.'%)'; } SOME HTML <li id="q9"> <div class="Question">Scientists believe the universe is:</div> <div class="Answer"> <label class="Wide" for="q9-A"><div class="Radio"><input type="radio" name="q9" id="q9-A" value="A" style="display: none;"> A. disappearing</div></label></div> <div class="Answer"> <label class="Wide" for="q9-B"><div class="Radio"><input type="radio" name="q9" id="q9-B" value="B" style="display: none;"> B. expanding</div></label></div> <div class="Answer"> <label class="Wide" for="q9-C"><div class="Radio"><input type="radio" name="q9" id="q9-C" value="C" style="display: none;"> C. contracting</div></label></div> <div class="Answer"> <label class="Wide" for="q9-D"><div class="Radio"><input type="radio" name="q9" id="q9-D" value="D" style="display: none;"> D. becoming bipolar</div></label></div> </li> <li id="q10"> <div class="Question">Check each item that can be found in our solar system.</div> <div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;"> <label for="q10-A"><input type="checkbox" name="q10[]" id="q10-A" value="A">planet</label> <label for="q10-B"><input type="checkbox" name="q10[]" id="q10-B" value="B">asteroid</label> <label for="q10-C"><input type="checkbox" name="q10[]" id="q10-C" value="C">comet</label> <label for="q10-D"><input type="checkbox" name="q10[]" id="q10-D" value="D">black hole</label> <label for="q10-E"><input type="checkbox" name="q10[]" id="q10-E" value="E">neutrino star</label> <label for="q10-F"><input type="checkbox" name="q10[]" id="q10-F" value="F">quasar</label> </div> </li>
  10. Hi All, I have written some code that currently reads multiple CSV files into a multidimensional array and am now trying to produce a new array where the values for a particular key match across the arrays. Then I would output a new CSV file built from the new array. Currently I have the following code: <?php $file = ''; $path = 'data/'; # CSV file from URL /* if(isset($_GET['filename'])) { */ $files = explode(':', $_GET['filename']); $CSV_Files_Count = count($files); echo $CSV_Files_Count; //$file = 'data/' . strip_tags($_GET['filename'] . '.csv'); echo var_dump($files); $NewArray = array(); # Loop through multiple CSV files for($i = 0; $i < $CSV_Files_Count; $i++) { if(file_exists($path . $files[$i])) { # read CSV file into an associative array $csv = array_map('str_getcsv', file($path . $files[$i])); array_walk($csv, function(&$a) use ($csv) { $a = array_combine($csv[0], $a); }); array_shift($csv); # remove column header $MasterArray[$i] = $csv; /* echo '<pre>'; var_dump($csv); echo '</pre>'; echo '<pre>'; echo $path . $files[$i]; echo '</pre>'; */ /* echo '<pre>'; var_dump($MasterArray[$i]); echo '</pre>'; */ } } ?> If I var_dump($csv) I get the following output which is reading my CSV test files perfectly: array(3) { [0]=> array(7) { ["First Name"]=> string(4) "Mark" ["Last Name"]=> string(5) "Baker" ["Nationality"]=> string(7) "British" ["Gender"]=> string(1) "M" ["Date of Birth"]=> string(9) "19-Dec-60" ["userID"]=> string(1) "1" ["Sanity %Age"]=> string(3) "32%" } [1]=> array(7) { ["First Name"]=> string(4) "Toni" ["Last Name"]=> string(5) "Baker" ["Nationality"]=> string(7) "British" ["Gender"]=> string(1) "F" ["Date of Birth"]=> string(8) "2-May-78" ["userID"]=> string(1) "2" ["Sanity %Age"]=> string(3) "95%" } [2]=> array(7) { ["First Name"]=> string(6) "Rachel" ["Last Name"]=> string(5) "Baker" ["Nationality"]=> string(7) "British" ["Gender"]=> string(1) "F" ["Date of Birth"]=> string(8) "7-Dec-82" ["userID"]=> string(1) "3" ["Sanity %Age"]=> string(4) "100%" } } array(9) { [0]=> array(4) { ["Location"]=> string(8) "bathroom" ["duration"]=> string(5) "34:00" ["userID"]=> string(1) "1" [""]=> string(0) "" } [1]=> array(4) { ["Location"]=> string(7) "kitchen" ["duration"]=> string(5) "20:00" ["userID"]=> string(1) "2" [""]=> string(0) "" } [2]=> array(4) { ["Location"]=> string(5) "study" ["duration"]=> string(5) "18:00" ["userID"]=> string(1) "1" [""]=> string(0) "" } [3]=> array(4) { ["Location"]=> string(5) "study" ["duration"]=> string(5) "00:00" ["userID"]=> string(1) "1" [""]=> string(0) "" } [4]=> array(4) { ["Location"]=> string(7) "kitchen" ["duration"]=> string(5) "45:00" ["userID"]=> string(1) "2" [""]=> string(0) "" } [5]=> array(4) { ["Location"]=> string(8) "bathroom" ["duration"]=> string(5) "03:00" ["userID"]=> string(1) "3" [""]=> string(0) "" } [6]=> array(4) { ["Location"]=> string(7) "kitchen" ["duration"]=> string(5) "34:00" ["userID"]=> string(1) "1" [""]=> string(0) "" } [7]=> array(4) { ["Location"]=> string(7) "kitchen" ["duration"]=> string(5) "27:00" ["userID"]=> string(1) "2" [""]=> string(0) "" } [8]=> array(4) { ["Location"]=> string(5) "study" ["duration"]=> string(5) "50:00" ["userID"]=> string(1) "1" [""]=> string(0) "" } } So I want to create a master array which I would then output as a new CSV file. Where elements from the second array are appended onto the first array where the userID's match and if there are elements without any matching userID's they still remain in the array. Thank you kindly in advance
  11. I have an array that has several amounts (based on $$$ sales) attached to a 'name' 'id' and 'goal'. As you can see some of the names, id's, and goals are the same. My goal is to gather a total of amounts and attach each total to whichever 'name', 'id', and 'goal' that made the sale. I'm honestly not sure how to go about this as I'm still learning. Array ( [0] => Array ( [name] => L.Chane [id] => oper-4bceffd1-21e0af5b [goal] => 2014-10-25000 [amount] => 360.00 ) [1] => Array ( [name] => L.Chane [id] => oper-4bceffd1-21e0af5b [goal] => 2014-10-25000 [amount] => 450.00 ) [2] => Array ( [name] => L.Chane [id] => oper-4bceffd1-21e0af5b [goal] => 2014-10-25000 [amount] => 450.00 ) [3] => Array ( [name] => C.James [id] => oper-4c236420-0b11e945 [goal] => 2014-10-25000 [amount] => 370.00 ) [19] => Array ( [name] => C.James [id] => oper-4c236420-0b11e945 [goal] => 2014-10-25000 [amount] => 175.00 ) [20] => Array ( [name] => C.James [id] => oper-4c236420-0b11e945 [goal] => 2014-10-25000 [amount] => 155.00 )[61] => Array ( [name] => K.Crass [id] => oper-4c597644-402490ee [goal] => 2014-10-25000 [amount] => 200.00 ) [62] => Array ( [name] => K.Crass [id] => oper-4c597644-402490ee [goal] => 2014-10-25000 [amount] => 599.00 ) [63] => Array ( [name] => K.Crass [id] => oper-4c597644-402490ee [goal] => 2014-10-25000 [amount] => 50.00 ) [113] => Array ( [name] => R.Cervantes [id] => oper-4f05a90b-03b379f9 [goal] => 2014-10-25000 [amount] => 450.00 ) [114] => Array ( [name] => R.Cervantes [id] => oper-4f05a90b-03b379f9 [goal] => 2014-10-25000 [amount] => 589.00 ) [115] => Array ( [name] => R.Cervantes [id] => oper-4f05a90b-03b379f9 [goal] => 2014-10-25000 [amount] => 350.00 ) [166] => Array ( [name] => A.Gerred [id] => oper-4f30019a-27f27473 [goal] => 2014-10-25000 [amount] => 375.00 ) [167] => Array ( [name] => A.Gerred [id] => oper-4f30019a-27f27473 [goal] => 2014-10-25000 [amount] => 294.50 ) [168] => Array ( [name] => A.Gerred [id] => oper-4f30019a-27f27473 [goal] => 2014-10-25000 [amount] => 440.00 ) [202] => Array ( [name] => G.Whitcher [id] => oper-4f300d33-de9592e3 [goal] => 2014-10-25000 [amount] => 5.00 ) [203] => Array ( [name] => G.Whitcher [id] => oper-4f300d33-de9592e3 [goal] => 2014-10-25000 [amount] => 310.00 ) [204] => Array ( [name] => G.Whitcher [id] => oper-4f300d33-de9592e3 [goal] => 2014-10-25000 [amount] => 349.00 ) [235] => Array ( [name] => K.Lawrence [id] => oper-50f6e4ad-9effbec7 [goal] => 2014-10-25000 [amount] => 499.00 ) [236] => Array ( [name] => K.Lawrence [id] => oper-50f6e4ad-9effbec7 [goal] => 2014-10-25000 [amount] => 187.50 ) [237] => Array ( [name] => K.Lawrence [id] => oper-50f6e4ad-9effbec7 [goal] => 2014-10-25000 [amount] => 170.00 ) [246] => Array ( [name] => K.Chane [id] => oper-52657816-3d6516e2 [goal] => 2014-10-25000 [amount] => 375.00 ) [247] => Array ( [name] => K.Chane [id] => oper-52657816-3d6516e2 [goal] => 2014-10-25000 [amount] => 187.50 ) [248] => Array ( [name] => K.Chane [id] => oper-52657816-3d6516e2 [goal] => 2014-10-25000 [amount] => 229.50 ) [256] => Array ( [name] => J.Stewart [id] => oper-qtgjvw8y-1uqtw058 [goal] => 2014-10-25000 [amount] => 170.00 ) [257] => Array ( [name] => J.Stewart [id] => oper-qtgjvw8y-1uqtw058 [goal] => 2014-10-25000 [amount] => 584.00 ) [258] => Array ( [name] => J.Stewart [id] => oper-qtgjvw8y-1uqtw058 [goal] => 2014-10-25000 [amount] => 249.50 ) ) Here is my code so far (And I know it's not clean, because I'm not using PDO yet, I'm just trying to get it to work): $result = mysql_query("SELECT a.*,u.OperatorName,u.MonthlyGoal FROM tblUserPayments a LEFT JOIN tblOperatorGoals u ON a.OperatorID = u.OperatorID WHERE a.ChargeAmount IS NOT NULL AND a.PaymentStatus='OK' AND a.PaymentDate LIKE '$currentDate%' AND u.MonthlyGoal LIKE '$currentDate%' " ); while ($row = mysql_fetch_assoc($result)) { $operArray[] = array( 'name' => $row['OperatorName'], 'id' => $row['OperatorID'], 'goal' => $row['MonthlyGoal'], 'amount' => $row['ChargeAmount']); } foreach ($operArray as $value) { if($value['id'] == '' || $value['id'] == null) { continue; } if(array_key_exists($value['id'], $operSums)) { $operSums[$value['id']] += $value['amount']; } else { $operSums[$value['id']] = $value['amount']; } } Any help is greatly appreciated. Associative arrays are just not my cup of tea.
  12. I have to complete a multidimensional array, I have written PHP code but I keep getting numbers ontop of the arrays? Moreover; could you give me feedback on the multidimensional array is it anygood? <?php $customers = array( $customers =['Name' => 'Bob','id' => '587','Date' => '17/10/1015',], $customers =['Name' => 'Stu','id' => '100','Date' => '02/04/2010', ], $customers =['Name' => 'Kate','id' => '12','Date' => '09/12/2013',], ); $keys = array_keys($customers); $keys = array_keys($customers); for($i = 0; $i < count($customers); $i++) { echo $keys[$i] . "<br>"; foreach($customers[$keys[$i]] as $key => $value) { echo $key . " : " . $value . "<br>";} echo "<br>";} ?> //Ouput: 0Name : Bobid : 587Date : 17/10/1015 1Name : Stuid : 100Date : 02/04/2010 2Name : Kateid : 12Date : 09/12/2013
  13. Hi, I am working on a betting API, The data comes from an XML Feed. I am using a PHPPhar file to iterate the XML data, Seems to work really well. What I need to do now is iterate each "outcome" type and order it by the highest decimal to lowest decimal value (Best odds) My current array of data from the XML looks like this (See pastebin for full version) : SimpleXMLElement Object ( [Outcome] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 1 [name] => 1X2 ) [Bookmaker] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 8 [name] => Bet365 [isLive] => false [lastUpdate] => 2015-01-15T04:17:03 [bookieEventID] => [bookieLeagueID] => [BookieOfferTypeID] => 40 ) [Odds] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 10026743121640944 [bet] => 1 [startPrice] => 9/2 [currentPrice] => 5/1 [line] => [LastUpdate] => 2015-01-15T04:17:03 [bookieOutcomeID] => [Status] => Open ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 10026743131640944 [bet] => 2 [startPrice] => 31/50 [currentPrice] => 57/100 [line] => [LastUpdate] => 2015-01-15T04:17:03 [bookieOutcomeID] => [Status] => Open ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 10026743511640944 [bet] => X [startPrice] => 11/4 [currentPrice] => 3/1 [line] => [LastUpdate] => 2015-01-14T17:55:48 [bookieOutcomeID] => [Status] => Open ) ) ) )[1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 2 [name] => Under/Over ) [Bookmaker] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 8 [name] => Bet365 [isLive] => false [lastUpdate] => 2015-01-15T15:25:31 [bookieEventID] => [bookieLeagueID] => [BookieOfferTypeID] => 981 ) [Odds] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 14185280871640944 [bet] => Over [startPrice] => 1/20 [currentPrice] => 3/50 [line] => 0.5 [LastUpdate] => 2015-01-15T15:15:14 [bookieOutcomeID] => [Status] => Open ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 8219239561640944 [bet] => Over [startPrice] => 9/50 [currentPrice] => 1/5 [line] => 1.25 [LastUpdate] => 2015-01-15T08:30:43 [bookieOutcomeID] => [Status] => Open ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 13305582441640944 [bet] => Over [startPrice] => 29/100 [currentPrice] => 8/25 [line] => 1.5 [LastUpdate] => 2015-01-15T15:25:31 [bookieOutcomeID] => [Status] => Open ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 15814388431640944 [bet] => Over [startPrice] => 7/20 [currentPrice] => 2/5 [line] => 1.75 [LastUpdate] => 2015-01-15T08:30:43 [bookieOutcomeID] => [Status] => Open ) ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 9542253841640944 [bet] => Over [startPrice] => 9/20 [currentPrice] => 13/25 [line] => 2.0 [LastUpdate] => 2015-01-15T08:53:07 [bookieOutcomeID] => [Status] => Open ) ) [5] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 18435608691640944 [bet] => Over [startPrice] => 17/25 [currentPrice] => 77/100 [line] => 2.25 [LastUpdate] => 2015-01-15T08:53:07 [bookieOutcomeID] => [Status] => Open ) )[2] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 17 [name] => Both Teams To Score ) [Bookmaker] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 8 [name] => Bet365 [isLive] => false [lastUpdate] => 2015-01-14T17:55:48 [bookieEventID] => [bookieLeagueID] => [BookieOfferTypeID] => 10150 ) [Odds] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 14294083431640944 [bet] => No [startPrice] => 83/100 [currentPrice] => 73/100 [line] => [LastUpdate] => 2015-01-14T17:55:48 [bookieOutcomeID] => [Status] => Open ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 18070790851640944 [bet] => Yes [startPrice] => 83/100 [currentPrice] => 1/1 [line] => [LastUpdate] => 2015-01-14T17:55:48 [bookieOutcomeID] => [Status] => Open ) ) ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 31 [name] => William Hill [isLive] => false [lastUpdate] => 2015-01-15T15:58:30 [bookieEventID] => [bookieLeagueID] => [BookieOfferTypeID] => 226344747 ) [Odds] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 11341591031640944 [bet] => No [startPrice] => 73/100 [currentPrice] => 67/100 [line] => [LastUpdate] => 2015-01-15T15:58:30 [bookieOutcomeID] => [Status] => Open ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 10996680931640944 [bet] => Yes [startPrice] => 1/1 [currentPrice] => 11/10 [line] => [LastUpdate] => 2015-01-15T15:58:30 [bookieOutcomeID] => [Status] => Open ) ) ) ) Is it possible build an array of outcomes, Then sort by the best decimal price. (I have a function built that can convert the "currentPrice" to decimal. Thnak You!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.