PHP - Printing A Specific Number From An Array
Hello, i retrieve an array from the database that looks something like this:
$var=a:1:{s:2:"cc";a:1:{i:22;s:11:"TESTING";}}; I need to retrieve the number corresponding to the character i: and print it(11 in the first case). For example , If, $var=a:1:{s:2:"cc";a:1:{i:15;s:11:"TESTING";}}; I would need to print 15. $var=a:1:{s:2:"cc";a:1:{i:3;s:11:"TESTING";}}; I would need to print 3. Thanks in advance. Similar TutorialsHey... I'm trying to echo the title of the news in the right column but I just want to display maximum of 25 characters how can I do it? thanks hey guys i have this invoice im working on but my clients want's the invoice id start at a specific number?
do it put that number in phpmyadmin direct on the line null?or can i do it another way?
I'm pretty sure this is really simple to do using the count function, but I am having a hard time finding examples on how to count the number of times a "SPECIFIC" word occurs in a file. For example, I just want to display how many times "this-word" occurs in "http://www.mysite.com/logfile.log". Any help would be appreciated. Hi there, I'm writing an application that pulls from two separate web based APIs, collates the data into a single array of data. Once done, it then enters a loop that cycles through the array and executes a cURL call for each cycle. This worked perfectly for a controlled set of 10 artificial records and so I've started testing with live data. On running the code, I eventually the following : Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. I'm not sure how to debug this and so I'm hoping I can find some help here. I'm running WAMP, and the php.ini contains the following: max_execution_time = 0 ; Maximum execution time of each script, in seconds max_input_time = 0 ; Maximum amount of time each script may spend parsing request data Through testing, I have discovered the following : The script can do exactly 35 iterations through the loop in under 2 minutes. Any number greater than this causes the error. If I skip the first 20 curl submits, I can then do 21 to 55 iterations before failure, so I do not believe it is the 36th value. If I put a sleep(5) command into the loop, I can still do 35 and no more iterations than that. If I put a sleep(10) command into the loop, it fails on 35 and needs less. Code: [Select] $action = "post"; $url = "http://url.url.url/api/v1.0/udo_Position/create"; // I've replaced the actual URL out of necessity // set user agent { print("Setting CURL options, using URL ".$url."<br>"); $cd = curl_init(); curl_setopt($cd, CURLOPT_HEADER, 1); curl_setopt($cd, CURLOPT_URL, $url); curl_setopt($cd, CURLOPT_USERPWD, 'username:password'); curl_setopt($cd, CURLOPT_RETURNTRANSFER, 1); // Don't send return value to screen curl_setopt($cd, CURLOPT_USERAGENT, "Firefox/2.0.0.1"); // spoofing FireFox 2.0 curl_setopt($cd, CURLOPT_VERBOSE, true); // } if ($action == 'get') { ; } elseif ($action == 'post') { print("POST set;<br>"); print("<pre style='font-family:verdana;font-size:13'>"); print_r($pointval); print("</pre>"); $pointval["save"] = "Save"; // The AddNew step form requires this. curl_setopt($cd, CURLOPT_POSTFIELDS, $pointval); } else { print("Invalid usage. Please see example<br>"); return; } $reply = curl_exec($cd); $http_status = curl_getinfo($cd, CURLINFO_HTTP_CODE); print("<pre style='font-family:verdana;font-size:13'>"); print_r($http_status); print("</pre>"); print("Here is the reply from the AddNew function: <br>"); print("<pre style='font-family:verdana;font-size:13'>"); print_r($reply); print("</pre>"); if (curl_error($cd)) { print("Error: ".(curl_error($cd))."<br>"); } curl_close($cd); print("Insert Successful.<br>"); I am uncertain whether it is a PHP, Apache or cURL issue at this point. I have another script that pushes a great deal more cURL calls to the same system but pulls from only one external resource, instead of two. Any help or suggestions would be appreciated. <ul> <!-- List the rest of the articles found in the category --> <?php else : ?> <li class="otherrecentmain"><span class="title"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></span> <span class="meta"></span> </li> <?php endif; ?> <?php endwhile; ?> <!-- END --> </ul> For some reason, its stuck on displaying 9 articles but I want to be able to control how many I want. I may want to choose 17 or whatever etc, not sure how to add it into the php. Here is my code...
<?php $to = "email@email.com"; //obviously not real $from = "noreply@domain.com"; //obviously not real $firstName = $_REQUEST['firstName']; $headers = "From: $from"; $subject = "Product Sheet Request"; /* $fields = array(); $fields{"firstName"} = "firstName"; $fields{"lastName"} = "lastName"; $fields{"email"} = "email"; $fields{"phone"} = "phone"; $fields{"affiliation"} = "affiliation"; $fields{"interest"} = "interest"; $fields{"productSheets"} = "productSheets"; $fields{"referredBy"} = "referredBy"; $fields{"additional"} = "additional"; */ $firstName = $_REQUEST['firstName']; $lastName = $_REQUEST['lastName']; $email = $_REQUEST['email']; $phone = $_REQUEST['phone']; $affiliation = $_REQUEST['affiliation']; $interest = $_REQUEST['interest']; if($interest == 'inclusion'){ $interest = 'Evaluating for inclusion in product'; } elseif($interest == 'general'){ $interest = 'Interested in the technology generally'; } elseif($interest == 'research'){ $interest = 'Performing market research'; } elseif($interest == 'solutions'){ $interest = 'Looking for competitive solutions'; } $referredBy = $_REQUEST['referredBy']; $additional = $_REQUEST['additional']; $productSheets[] = $_REQUEST['productSheets']; $sheets = print_r($productSheets); /* $body = "A user has requested product sheet(s). Please review their information and respond:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } */ $body = "A user has requested product sheet(s). Please review their information and respond:\n First Name: ".$firstName."\r Last Name: ".$lastName."\r Email: ".$email."\r Phone Number: ".$phone."\r Affiliation: ".$affiliation."\r Reason for Interest: ".$interest."\r Referred By: ".$referredBy."\r Additional Questions/Comments: ".$additional."\r Product Sheet(s) Requested: ".$sheets."\n "; $send = mail($to, $subject, $body, $headers); ?>I'm not quite sure how to display the values of the $productSheets array in the email message. Everytime it either outputs "Array" or "1". The $productSheets array is from a checkbox on a form. Everything else is working as expected. I have the following array structu Code: [Select] [0] => Array ( [id] => Array ( [$t] => http://www.google.com/mate/ ) [updated] => Array ( [$t] => 2011-08-31T11:43:05.942Z ) [category] => Array ( [0] => Array ( [scheme] => http://schemas.google.com/g/ [term] => http://schemas.google.com/contact/ ) ) [title] => Array ( [type] => text [$t] => Name ) [link] => Array ( [0] => Array ( [rel] => http://schemas.google.com/contacts/2008/rel#edit-photo [type] => image/* [href] => https://www.google.com/mate/feeds/photos/media/ ) [1] => Array ( [rel] => self [type] => application/atom+xml [href] => https://www.google.com/mate/feeds/contacts ) [2] => Array ( [rel] => edit [type] => application/atom+xml [href] => https://www.google.com/mate/feeds/ ) ) [gd$email] => Array ( [0] => Array ( [rel] => http://schemas.google.com/g/2005#other [address] => email_address@gmail.com [primary] => true ) ) ) I am tried to display name and email address. I am using following method, can anyone tell me is there any better way? Thank u 4u help. NAME $name=( $emp_det[0]['title'][t]); $email =( $emp_det[0]['gdemail'][0][address]); Hi, I'm a PHP novice and trying to print out elements of what appears to be a multi-dimensional array, but I can't figure it out. I'm able to use print_r() to dump the array. Array ( [0] => 18485 [1] => Array ( [1] => Array ( [isPublished] => 1 [dateAdded] => 2019-07-18T16:13:28+00:00 [dateModified] => 2020-01-29T18:37:45+00:00 [createdBy] => [createdByUser] => Support [modifiedBy] => [modifiedByUser] => [id] => 1 [points] => 0 [color] => [fields] => Array ( [core] => Array ( [points] => Array ( [id] => 9 [label] => Points [alias] => points [type] => number [group] => core [object] => lead [is_fixed] => 1 [default_value] => 0 [properties] => a:0:{} [value] => 0 [normalizedValue] => 0 ) [last_active] => Array ( [id] => 19 [label] => Date Last Active [alias] => last_active [type] => datetime [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => 2020-01-29 18:37:47 [normalizedValue] => 2020-01-29 18:37:47 ) [title] => Array ( [id] => 1 [label] => Title [alias] => title [type] => lookup [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:1:{s:4:"list";a:3:{i:0;s:2:"Mr";i:1;s:3:"Mrs";i:2;s:4:"Miss";}} [value] => [normalizedValue] => ) [firstname] => Array ( [id] => 2 [label] => First Name [alias] => firstname [type] => text [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => Andy [normalizedValue] => Andy ) [lastname] => Array ( [id] => 3 [label] => Last Name [alias] => lastname [type] => text [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => Towne [normalizedValue] => Towne ) [company] => Array ( [id] => 4 [label] => Primary company [alias] => company [type] => text [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [position] => Array ( [id] => 5 [label] => Position [alias] => position [type] => text [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [email] => Array ( [id] => 6 [label] => Email [alias] => email [type] => email [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => andy.towne@example.com [normalizedValue] => andy.towne@example.com ) [phone] => Array ( [id] => 8 [label] => Phone [alias] => phone [type] => tel [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [mobile] => Array ( [id] => 7 [label] => Mobile [alias] => mobile [type] => tel [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [address1] => Array ( [id] => 11 [label] => Address Line 1 [alias] => address1 [type] => text [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [address2] => Array ( [id] => 12 [label] => Address Line 2 [alias] => address2 [type] => text [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [city] => Array ( [id] => 13 [label] => City [alias] => city [type] => text [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => Boston [normalizedValue] => Boston ) [state] => Array ( [id] => 14 [label] => State [alias] => state [type] => region [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => Massachusetts [normalizedValue] => Massachusetts ) [zipcode] => Array ( [id] => 15 [label] => Zip Code [alias] => zipcode [type] => text [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [timezone] => Array ( [id] => 18 [label] => lead.field.timezone [alias] => timezone [type] => timezone [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => America/New_York [normalizedValue] => America/New_York ) [country] => Array ( [id] => 16 [label] => Country [alias] => country [type] => country [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => United States [normalizedValue] => United States ) [fax] => Array ( [id] => 10 [label] => Fax [alias] => fax [type] => tel [group] => core [object] => lead [is_fixed] => 0 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [preferred_locale] => Array ( [id] => 17 [label] => Preferred Locale [alias] => preferred_locale [type] => locale [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [attribution_date] => Array ( [id] => 20 [label] => Attribution Date [alias] => attribution_date [type] => datetime [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [attribution] => Array ( [id] => 21 [label] => Attribution [alias] => attribution [type] => number [group] => core [object] => lead [is_fixed] => 1 [default_value] => [properties] => a:2:{s:9:"roundmode";i:4;s:9:"precision";i:2;} [value] => [normalizedValue] => ) [website] => Array ( [id] => 22 [label] => Website [alias] => website [type] => url [group] => core [object] => lead [is_fixed] => 0 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [number_of_email_users] => Array ( [id] => 45 [label] => Number of Email Users [alias] => number_of_email_users [type] => number [group] => core [object] => lead [is_fixed] => 0 [default_value] => [properties] => a:2:{s:9:"roundmode";s:1:"3";s:9:"precision";s:0:"";} [value] => [normalizedValue] => ) ) [social] => Array ( [facebook] => Array ( [id] => 23 [label] => Facebook [alias] => facebook [type] => text [group] => social [object] => lead [is_fixed] => 0 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [foursquare] => Array ( [id] => 24 [label] => Foursquare [alias] => foursquare [type] => text [group] => social [object] => lead [is_fixed] => 0 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [instagram] => Array ( [id] => 26 [label] => Instagram [alias] => instagram [type] => text [group] => social [object] => lead [is_fixed] => 0 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [linkedin] => Array ( [id] => 27 [label] => LinkedIn [alias] => linkedin [type] => text [group] => social [object] => lead [is_fixed] => 0 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [skype] => Array ( [id] => 28 [label] => Skype [alias] => skype [type] => text [group] => social [object] => lead [is_fixed] => 0 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) [twitter] => Array ( [id] => 29 [label] => Twitter [alias] => twitter [type] => text [group] => social [object] => lead [is_fixed] => 0 [default_value] => [properties] => a:0:{} [value] => [normalizedValue] => ) ) [personal] => Array ( ) [professional] => Array ( ) [all] => Array ( [id] => 1 [points] => 0 [last_active] => 2020-01-29 18:37:47 [title] => [firstname] => Andy [lastname] => Towne [company] => [position] => [email] => andy.towne@example.com [phone] => [mobile] => [address1] => [address2] => [city] => Boston [state] => Massachusetts [zipcode] => [timezone] => America/New_York [country] => United States [fax] => [preferred_locale] => [attribution_date] => [attribution] => [website] => [number_of_email_users] => [facebook] => [foursquare] => [instagram] => [linkedin] => [skype] => [twitter] => ) ) [lastActive] => 2020-01-29T18:37:47+00:00 [owner] => [ipAddresses] => Array ( [0] => Array ( [ipAddress] => 104.247.39.34 [id] => 5043 [ipDetails] => Array ( [city] => Boston [region] => Massachusetts [zipcode] => [country] => United States [latitude] => 42.36 [longitude] => -71.0545 [isp] => [organization] => [timezone] => America/New_York [extra] => ) ) [1] => Array ( [ipAddress] => 74.125.210.26 [id] => 6162 [ipDetails] => Array ( [city] => Hayward [region] => California [zipcode] => [country] => United States [latitude] => 37.6736 [longitude] => -122.0944 [isp] => [organization] => [timezone] => America/Los_Angeles [extra] => ) ) [2] => Array ( [ipAddress] => 73.126.166.11 [id] => 3069 [ipDetails] => Array ( [city] => Everett [region] => Massachusetts [zipcode] => [country] => United States [latitude] => 42.415 [longitude] => -71.0527 [isp] => [organization] => [timezone] => America/New_York [extra] => ) ) ) [tags] => Array ( ) [utmtags] => Array ( ) [stage] => [dateIdentified] => 2019-07-18T16:13:28+00:00 [preferredProfileImage] => gravatar [doNotContact] => Array ( ) [frequencyRules] => Array ( ) ) ) ) This output was produced using print_r($array);
I'm trying to access our contacts using the vendor's PHP API, but I'm not much of a PHP programmer. The array is stored in $contacts, using the vendor's getList() function. I believe I'm able to download the whole list of contacts in an array, but I can't iterate through it. I'd like to print out the elements of the array in CSV format.
$contacts = $contactApi->getList($searchFilter, $start, $limit, $orderBy, $orderByDir, $publishedOnly, $minimal); I've also tried something like this:
foreach ($contacts as $contact) {
<?php $menu = array( '22' => array ( 'title'=>'Businesses' ,'busstype'=>array( 5=>'Printers' ,2=>'Bookshops' ,6=>'Publishers' ,8=>'Uniform Distributors' ) ) ,21 => array ( 'title'=>'Academic' ,'busstype'=>array ( 5=>'Academy' ,6=>'Primary' ,7=>'Polytechnic' ,11=>'University' ) ) ,'Events' => array ( 'title'=>'Events' ,'busstype'=>array ( 3=>'Music' ,4=>'Road shows' ,6=>'Sports' ,6=>'Yoga' ) ) ) ; I have a dynamically created menu as above. How would i sort on the 'busstype' homogenously and print it while maintaing the key. Results should be like <a href="search.php?listing=21&busstype=5">Academy</a> <a href="search.php?listing=22&busstype=2">Bookshops</a> ....... <a href="search.php?listing=Events&busstype=6">Yoga</a> ?> Hi all, I'm trying to set up a situation where a function loops through a multidimensional array and echos an html block for each item in the $skus['skunum'] array and also increments $i each time . ( Each is a product on the site) The catch is that I'm trying to have a function within that one that goes through the $skus['sizes'] and echoes an option if the substring (shirt size) exists. The idea being that I can manage products by adding 4 elements, Skunum, name, price and size. And well, I'm failing miserably. At this point it won't even work because of the variable scope, I'm getting Notice: Undefined variable: skus from the functions, but am not sure of the right way to get that information. Am I going about doing this entirely wrong or am I on the right track? Code: [Select] <?php $BANDNAME="Apocalypse"; $BANDCAPS="APOCALYPSE"; $BANDLOWER="apocalypse"; $SKUCAPS="FGD"; $skus = array ( "skunum"=>array ( "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112" ), "name"=>array ( "RIDDICK /TSHIRT", "MAFIA / TSHIRT", "ORACLES / TSHIRT", "AGONY / TSHIRT", "BLOODY VIOLINIST / TSHIRT", "THE VIOLATION / TSHIRT", "AGONY-TOUR DATES / TSHIRT", "BAND PHOTO / TSHIRT", "SILVER AGONY LOGO / TSHIRT", "PHOENIX-TOUR DATES / TSHIRT", "PHOENIX / TSHIRT", "BLOODY VIOLINIST / ZIP HOOD" ), "price"=>array ( "15.95", "15.95", "15.95", "15.95", "15.95", "15.95", "15.95", "15.95", "15.95", "15.95", "15.95", "42.95" ), "sizes"=>array ( "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X" ) ); function makeProducts() { //Products Sizes $sizes = $skus['sizes'][$i]; $S="S";$M="M";$L="L";$XL="1";$XXL="2";$XXXL="3";$XXXXL="4"; $small = strpos($sizes,$S); $medium = strpos($sizes,$M); $large = strpos($sizes,$L); $xlarge = strpos($sizes,$XL); $xxlarge = strpos($sizes,$XXL); $xxxlarge = strpos($sizes,$XXXL); $xxxxlarge = strpos($sizes,$XXXXL); function sizeOptions () { if($small === true) { echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'S">S</OPTION>'; } if($medium === true) { echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'M">M</OPTION>'; } if($large === true) { echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'L">L</OPTION>'; } if($xlarge === true) { echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'X">XL</OPTION>'; } if($xxlarge === true) { echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'XX">XXL</OPTION>'; } if($xxxlarge === true) { echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'XXX">XXXL</OPTION>'; } if($xxxxxlarge === true) { echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'XXXX">XXXXL</OPTION>'; } } foreach ( $skus['skunum'] as $value){ echo '<!-- PRODUCT BEGIN --> <li class="product" > <a href="product_files/large/'.$SKUCAPS.$skus['skunum'][$i].'jpg" rel="lightbox"><img src="product_files/'.$SKUCAPS.$skus['skunum'][$i].'.png"></a><br> <strong>'.$skus['name'][$i].'</strong><br>('.$SKUCAPS.$skus['skunum'][$i].')<br>$'.$skus['price'][$i].'<br> <form name="'.$SKUCAPS.$skus['skunum'][$i].'" method="GET" target="_blank" action="http://www.jsrdirect.com/cgi-bin/Make-a-Store.cgi"> <input type="hidden" name="band" value="'.$BANDCAPS.'"> <input type="hidden" name="back" value="http://www.jsrdirect.com/bands/'.$BANDLOWER.'/index.html"> <strong>Qty:</strong> <input type="text" name="quantity" size="1" value="1" > <strong>Size:</strong> <SELECT name="item">' .sizeOptions(); '</SELECT> <br><br> <input type="image" src="images/addtocart.png" value="Add To Cart"> </form> </li> <!-- Product End -->'; } } // End Function ?> Hey guys. How do you remove a specific element from an array? Lets say I have a string like this: $list = "guy,girl,boy,man,woman,lady"; Then I use explode to break it up into an array by commas: $listArray = explode(",",$list); Now $listArray contains everything. Now if I wanted to delete something specific from that array, how would I do that? So for example, I want to delete "man" out of the array. How do I do that without know its key? Hello all, I have a rather large array and I need to list it's contents in an alphabetical list. The array is built like this: $array['query']['row']['0']['title'] = 'Bob'; $array['query']['row']['1']['title'] = 'Andy'; $array['query']['row']['2']['title'] = 'Tom'; I need to sort it so it is in alphabetical order based on the title. Is there an easy way to do this? I am using session variables to keep track of what is in a shopping cart. I can add the items to the cart quite easily enough. But my problem is that I can't delete a specific item in the cart. Below is a summary of what the array looks like $Item1=array("ProdID"=>"Hat","Color"=>"Not Needed","Size"=>5); $Item4=array("ProdID"=>"Shirt","Color"=>"Green","Size"=>"L"); $Item2=array("ProdID"=>"Pants","Color"=>"Not Needed","Size"=>"32x32"); $Item3=array("ProdID"=>"Socks","Color"=>"Not Needed","Size"=>"Not Needed"); $NumItems=4; $TestArray=array("NumItems"=>$NumItems,1=>$Item1,2=>$Item2,3=>$Item3,4=>$Item4); However when I attempt to delete any of the top level elements (1,2,3,4,NumItems), I either end up deleting only the last element (4) or somehow taking out 2 of them. I know that this must be an easy fix but I've looked at it for so long that I can't seem to figure out what the problem is. below is the code that I use to delete the Item from the array Code: [Select] $ItemNum=$_GET['item']; if ($ItemNum=="All"){ $ItemNum=$_GET['item']; if ($ItemNum=="All"){ unset($TestArray); unset($TestArray['NumItems']); unset($_SESSION['OrderTotal']); //header("Location:Cart.php?act=view"); } else{ $TestArray['NumItems']=$TestArray['NumItems']-1; if($TestArray['NumItems']<1) { unset($_SESSION['OrderTotal']); unset($TestArray); unset($TestArray['NumItems']); } else { unset($TestArray[$ItemNum]); } Can anyone take a look at this and point me in the correct direction? What I am after is to delete any element of the top array and keep the rest. <?php class CsvToArray { /* global $file; $file = '../Datafeeds/baseball_all.csv'; */ public function data() { $file = '../Datafeeds/baseball_all.csv'; $fileName = fopen($file,'r+'); $all_rows = array(); $header = fgetcsv($fileName); while ($row = fgetcsv($fileName)) { $games[] = array_combine($header, $row); } //print_r($games); print_r($games[64]["Home Team"]); } } $game = new CsvToArray(); $game->data(); ?> csv: https://anonymousfiles.io/vSubWgPY/ How can I get specific array values outside a class or function? About to try "File put contents" I think. That should work right? So I thought the last couple times. Or what would you suggest?. Overwritten previous attempts to share =[
Next up is seeing if I can change $file to $var to change it from other locations. Should be easy I reckon. Edited April 19 by MinzerHi all, I would like to ask you if somebody could have a better idea to build this function. The current one is working, but I feel this built a bit in an amateur manner. Feel free to add your ideas so we can learn more about PHP and how to deal with arrays.
I have an array: $arrayVideoSpecs = Array( Array( 'aspect' => '4:3', 'density' => '442368', 'resolution' => '768x576' ), Array( 'aspect' => '4:3', 'density' => '307200', 'resolution' => '640x480' ), Array( 'aspect' => '16:9', 'density' => '2073600', 'resolution' => '1920x1080' ), Array( 'aspect' => '16:9', 'density' => '121600', 'resolution' => '1280x720' ) );
and I want an array as output grouped by video aspect ratio where the key is the pixel density and the value is the video resolution, namely like that for aspect ratio 16:9 ... Array ( [2073600] => 1920x1080 [121600] => 1280x720 )
Then I coded this function which is working but seems amateur ... function groupAndExtractByAspect($array, $groupBy, $aspect, $density, $resolution) { $groupByAspect = Array(); foreach ($array as $value) { $groupByAspect[$value[$aspect]][] = Array($value[$density], $value[$resolution]); } $arrayClean = Array(); foreach ($groupByAspect as $key => $value) { if ($key == $groupBy) { $arrayClean[$key] = $value; } } foreach ($arrayClean as $aspectGroup) { $arrayOutput = Array(); for ($i = 0; $i <= count($aspectGroup); $i++) { $densityIsValid = false; $resolutionIsValid = false; if (!empty($arrayClean[$groupBy][$i][0])) { $density = $arrayClean[$groupBy][$i][0]; $densityIsValid = true; } if (!empty($arrayClean[$groupBy][$i][1])) { $resolution = $arrayClean[$groupBy][$i][1]; $resolutionIsValid = true; } if (($densityIsValid === true) && ($resolutionIsValid === true)) { $arrayOutput[$density] = $resolution; } } } return $arrayOutput; }
The usage is as follow ... $showArray = groupAndExtractByAspect($arrayVideoSpecs, '16:9', 'aspect', 'density', 'resolution'); echo '<pre>'; print_r($showArray); echo '</pre>';
Thank you very much for your ideas! Mapg Hi guys, I am getting data from a json source: ``` $jsondata = file_get_contents($url);$response = json_decode($jsondata, true) echo "<pre>"; print_r(array_keys($response));; echo "</pre>"; ``` and this returns: ``` Array ( [0] => jsonapi [1] => data [2] => links ) ``` I want to extract data from specific key in the data array (eg 'title'): ``` [data] => Array ( [0] => Array ( [type] => recipes [id] => d6cd7643-6e44-4d26-9ed9-9da7196cef12 [attributes] => Array ( [internalId] => 7 [isPublished] => 1 [title] => Deep mediterranean quiche [createdAt] => 2019-05-07T13:34:27+0200 [updatedAt] => 2019-05-07T13:34:27+0200 [isPromoted] => 1 [path] => /recipes/deep-mediterranean-quiche [cookingTime] => 30 [difficulty] => medium [ingredients] => Array ( [0] => For the pastry: [1] => 280g plain flour [2] => 140g butter [3] => Cold water [4] => For the filling: [5] => 1 onion [6] => 2 garlic cloves [7] => Half a courgette [8] => 450ml soya milk [9] => 500g grated parmesan [10] => 2 eggs [11] => 200g sun dried tomatoes [12] => 100g feta ) [numberOfservings] => 8 [preparationTime] => 40 [instructions] => ``` How do I loop through the data array and select just the 'title' and 'difficulty' for example? Edited May 12, 2019 by ludo1960Hi all, I have an array of data. I am looping through this data to generate a CSV file. However, if there are commas in any of the elements then this will throw out the structure of the CSV file. My question is, how do I loop through the array before I use it to strip out any commas and replace with a space? Thanks for your help. Hey all. another quick question. here is the output of my 2D array: Code: [Select] array(2) { ["folder.jpg"]=> array(2) { ["valid"]=> bool(true) ["error"]=> bool(false) } ["file.mp3"]=> array(2) { ["valid"]=> bool(true) ["error"]=> bool(false) } } how can i foreach my way into getting an iterator that points to the folder.jpg and file.mp3? as is now, i have something that looks like this: foreach($arr as $val){ var_dump($val) } the var_dump of $val is an *array*. Thanks for your help! I need to find out how many times the "mac" key show is in an array. Code: [Select] Array ( [serial] => Robert [added] => Peter [mac] => 1232091823 [mac] => 7538572375 [mac] =>943820348239804 [place] =>home ) |