Friday, 31 August 2018

Create a multidimensional array from four arrays based on the parent ID

From these four arrays: $root = (FORD, FIAT, GM, KIA, FERRARI); $parentIsFiat = (Panda, Punto, Tipo); $parentIsPanda = (1, 13, 16, 20); $parentIs13 = (Red, Blue, White); How can I create a multidimensional array to give me this: FORD FIAT --Panda ----1 ----13 ------Red ------Blue ------White ----16 ----20 --Punto --Tipo GM KIA FERRARI Background: The current menu on my site has every link on the site in the HTML. I want to only have the HTML for menu items that are actually visible. At the moment I get each item in the path (FIAT >...

PHP Create a multidimensional array from the keys

Is there a way to create dynamicaly multidimensional array? I have stored in database "path" for each field=>value like that: field_name : [slideshow][slide][0][title] field_value : my title field_name : [slideshow][slide][0][desc] field_value : my desc field_name : [slideshow][slide][1][title] field value : my other title field_name : [slideshow][slide][1][desc] field value : my other desc field_name : [slideshow][settings][duration] field value : 300 and now I'm trying to figure out how to make it an array again. Obviously there...

Create a multidimensional array from a database table

I have a mysql table which contains is: folder_id (int(11), auto increment) folder_name (varchar) folder_class(varchar) folder_link (varchar) What I want to do is somehow loop through the table and store each row like this: $packs = array( array( 'Name' => 'DarkUIPack', 'Class' => 'ui', 'Link' => 'http://graphicriver.net/item/dark-minimalist-ui-pack/662762' ), array( 'Name' => 'MinimalistIcons', 'Class' => 'min', ...

How do you create a multidimensional array from SQL data stored hierarchically using the adjacency list method?

Hierarchical Data from SQL Adjacency List Model In my model I have a series of objects, each stored with their parent id. I am using the Adjacency list model as my hierarchy method. All examples of Adjacency list simply output there and then. None try to create a multi dimensional array from the result set. --------------- | id | parent | --------------- | 1 | NULL | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 2 | | 6 | 5 | --------------- Object I created an array variable in my class called 'children' and want...

PHP - Create a multidimensional array from a one-dimensional array

Ok, so i have this string array that looks something like this: myArray[0] = "a0,b0,c0,d0" myArray[1] = "a1,b1,c1,d1" myArray[2] = "a2,b2,c2,d2" myArray[3] = "a3,b3,c3,d3" etc... I want to create a multidimentional array that uses the first part of each element as "key". In short: myMultiArray: x/y| 0 | 1 | 2 | 3 | ------------------------ 0 | "a0" | "a1" | "a2" | "a3" | 1 | "b0" | "b1" | "b2" | "b3" | 2 | "c0" | "c1" | "c2" | "c3" | 3 | "d0" | "d1" | "d2" | "d3" | this is what I've got: private String[,] resultsParser(String[]...

Sort the multidimensional array from top to bottom in PHP

I have checked on here and cant find a similar result so if this has been asked I do apologies. Anyway, I am generating an multi array which has been generated by a nice little class. Anyway the array comes like this when output: array(2047) { [0]=> array(6) { ["name"]=> string(4) "name" ["gamesplayed"] => string(2) "26" ["points"] => string(2) "26" } [1]=> array(6) { ["name"]=> string(4) "name" ["gamesplayed"] => string(2) "26" ["points"] => string(2) "42" ...

How do I create an associative array from a foreach loop?

$array = ["farm"=> [ "horse"=> [ "rabbit"=> [ "fred1"=> "fred1", "fred2"=> "fred2", "fred3"=> "fred3", "fred4"=> "fred4" ], "raccoon"=> ["frida"=> "frida"] ] ] ]; I want to create an array from my for each loop: $keySearch = "o"; function createList($array,...

PHP putting four dates in an array from a while loop

Using the below while loop, I get four dates in the echo, which is what I need. $data = []; $start = Carbon::createFromDate('2016', '04', '01'); $end = Carbon::createFromDate('2017', '04', '01'); while($start < $end) { $data[] = $start; echo $start; $start->addMonths('3'); } Output: 2016-04-01 2016-07-01 2016-10-01 2017-01-01 But when I dump the $data[] array, I just get four collections, each with the same date: 2017-04-01 What am I doing wrong? I just want to put the above four dates in an array. You're assigning...

Getting a random array of a while loop in PHP

I have a page set up for testimonials for a company website. Just the standard message first name, last name, message, blah blah..anyways I can pull the message up and have a single entry show up however, I would like it to randomly generate a 'testimonial' I know there is random_array functions already included in PHP but how would I go about it in a while loop? Thats the only way really I have learned to pull information from a MySQL database. So if there is a simpler way I am all ears. <?php mysql_select_db("test", $link); $sql=...

To create a multidimensional array from a while loop in PHP

I am losing my mind with this. I have a while loop that I am making into an array. I then want to pass that array to another function but it keeps failing. I know that it is failing for some formatting error, but I can't for the life of me figure out how to get it to format properly. Here is the format I NEED: array( 'id' => 'some-id', 'field1' => 'somefield', 'field2' => 'someotherfield', 'title' => 'title', 'subarray' => array( 'sub1' => 'subonevalue', 'sub2' => 'sub2value' ...

The php code does not convert to html

I have a php code <select style="width: 200px;" name="location" id="myselect" onchange="window.location='enable1.php?id='+this.value+'&pos='+this.selectedIndex;"> <option value="All">All</option> <?php $sql="select * from location"; var_dump($sql); $query=mysqli_query($conn,$sql); echo "1"; while($row=mysql_fetch_array($query)) { echo "<option value='$row[loc]'>'$row[loc]'</option>"; } ?> </select> In this code i echo"options" but instead of that when I view source...

The PHP code does not work, does not connect to the mysql database

I'm trying to get simple form data added to mysql database via php. This is my php code.. $dbc = mysqli_connect('localhost','root','sahunihal123321','aliendatabase') or die('Error connecting to MySQL server.'); $query="INSERT INTO aliens_abduction (first_name,last_name,when_it_happened,how_long, " . "how_many,alien_description,what_they_did,fang_spotted,other,email)" . "VALUES ('$first_name','$last_name','$when_it_happened','$how_long','$how_many', " . "'$alien_description','$what_they_did','$fang_spotted','$other','$email')"; $result=mysqli_query($dbc,$query) or...

The code does not print the Arabic text of the database

The code below that I am using will not print text that is of arabic form from the database although the collation of the table is uft8_general_ci and the database is of collation uft8_general_ci. code: <?php // Create connection $con = mysqli_connect("localhost", "", "", ""); mysqli_set_charset('utf8', $con); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // This SQL statement selects ALL from the table 'Locations' $sql...