Jump to content

Search the Community

Showing results for tags 'multidimensional array'.

  • 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 (PhpStorm, VS Code, 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 6 results

  1. Hi 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
  2. Hi all, This seems like it should be really simple but to me it's not. I have an array in a variable called $numbers. When I run print_r ($numbers) I get the following... $earchForNumber = '0304'; // Array called $numbers Array ( [0] => Array ( [0] => 0219, 0220, 0221, 0222, 0223 ) [1] => Array ( [0] => 0301, 0302, 0303, 0304 ) [2] => Array ( [0] => 1230, 1231, 0101, 0102 ) ) if (in_array($earchForNumber, $numbers)) {echo 'The key is' . $key;} What I need to do is search for a number within all of the sub arrays and find the key which it belongs to. So if I wanted to find the key for the value of $earchForNumber (0304) it would return a key of 1. There are tons of posts on stack overflow about this sort of thing but I can't find any that perform this task. I played around with inarray() but couldn't get any joy. Any ideas?
  3. I have assigned values for a multidimensional array as following (in opencart): $this->data['price_list'][0]['115']['296']= "8.99"; $this->data['price_list'][1]['115']['396']= "8.99"; $this->data['price_list'][2]['115']['446']= "8.99"; $this->data['price_list'][3]['115']['496']= "8.99"; $this->data['price_list'][4]['115']['596']= "12.99"; $this->data['price_list'][5]['115']['796']= ""; $this->data['price_list'][6]['115']['996']= ""; $this->data['price_list'][7]['115']['1196']= ""; $this->data['price_list'][8]['140']['296']= "12.99"; $this->data['price_list'][9]['140']['396']= "12.99"; $this->data['price_list'][10]['140']['446']= "12.99"; I have two variables: $height and $width. I want to check in the above array as following: let, $height= 140, $ width= 396. I want the value 12.99. How I'll check this?
  4. So i am trying to get a list of items out of my MySql database but my loop is not creating the array correctly, its dropping multiple rows when the key is the same name. <?php $search = 'item1'; $con = require_once('./dbconnect.php'); mysql_select_db("packages", $con); $package = mysql_query("SELECT * FROM $search", $con); while($item = mysql_fetch_row($package)){ $arr[$item[1]] = array('description' => $item[2], 'image' => $item[3]); } echo json_encode($arr); mysql_close($con); ?> So the loop works and does actually create the array with the various 'packages' in their respective array. The issue that i am having is that mysql_fetch_row($package)) can and will contain multiple keys that are the same name but with different things in the array. For example, the database will be something like this: PACK LEV DESCRIPTION IMAGES item1 A1 description for stuff1 image1 item1 A2 description for more stuff image2 item1 B1 more stuff here image3 item2 A1 description here for item2 image1 item2 B1 description contents here image2 --------------------------------------------------------------------------------- My Above code will produce an array like this: Array ( [item1] => Array ( [description] => more stuff here [image] => image3 ) [item2] => Array ( [description] => description contents here [image] => image2 ) ) What i want the array to be is: Array ( [item1] => Array ( [A1] => Array ( [description] => description for stuff1 [image] => image1 ) [A2] => Array ( [description] => description for more stuff [image] => image2 ) [b1] => Array ( [description] => more stuff here [image] => image3 ) [item2] => Array ( [A1] => Array ( [description] => description here for item2 [image] => image1 ) [b1] => Array ( [description] => description contents here [image] => image2 ) ) ) I hope this makes sense..Thanks for looking
  5. Here is a row from my merchandise table: <tr> <td>Product ID</td> <td>Artist</td> <td>Title</td> <td>Album Art</td> <td>Label</td> <td>Year</td> <td>Price</td> <td> <input type='text' name='cart[dawn of chaos][]' value='0' size='2'/> </td> </tr> I would like to direct your attention to the input row. In my PHP, the input field actually says "name=cart[$title][]" etc... Then I would like to reference the multidimensional array with PHP. To begin I just added a simple foreach. foreach($_POST['cart'] as $item){ echo $item; } PHP says this: Array ( ) Notice: Undefined index: cart in /you/probably/don't/need/my/filepath/cart.php on line 6 Warning: Invalid argument supplied for foreach() in....
  6. I have this array which has been pulled from a MySQL database (did a var_dump) array(2) { [0]=> array( { ["id"]=> string(1) "3" [0]=> string(1) "3" ["title"]=> string(23) "New Venture Coming Soon" [1]=> string(23) "New Venture Coming Soon" ["archived"]=> string(1) "y" [2]=> string(1) "y" ["postdate"]=> string(14) "7th March 2009" [3]=> string(14) "7th March 2009" } [1]=> array( { ["id"]=> string(1) "4" [0]=> string(1) "4" ["title"]=> string(22) "Visit To Headley Court" [1]=> string(22) "Visit To Headley Court" ["archived"]=> string(1) "y" [2]=> string(1) "y" ["postdate"]=> string(14) "7th March 2009" [3]=> string(14) "7th March 2009" } } I want to loop through the array and display the information but if $not_archived equals to zero then it displays a message. The problem is that it displays the message becuase $not_archived is equal to zero $z = 0; $not_archived = 0; foreach($news as $newsitem) { if($newsitem['archived'] == 'N') { if($z % 2==0) { //<tr class="yellow"> $z++; } else { //<tr class="white"> $z++ } $not_archived++; echo $newsitem['id']; echo $newsitem['title']; } } if($not_archived == 0) { //No active news in database }
×
×
  • 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.