Create multidimentional array from SQL table [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a table 'GOAL':
ID TYPE KEY VALUE
-- ---- --- -----
1 RED BASE 3
2 BLUE NAME ALLOR
3 RED MAIN _TTR
4 GREEN LOCAL PIN,SEC,BALL,UNI
5 BLUE ALT 2DFFRST34#HH&FR#
6 GREEN DOMAIN SITE.ORG,NSPL.EDU,ROAR.IN
I want to create a multidimentional array in PHP which will produce this array:
$GOAL = array (
'RED' => array (
'BASE' => 3,
'MAIN' => '_TTR'
),
'BLUE' => array(
'NAME' => 'ALLOR',
'ALT' => '2DFFRST34#HH&FR#'
),
'GREEN' => array(
'LOCAL' => 'PIN,SEC,BALL,UNI',
'DOMAIN' => 'SITE.ORG,NSPL.EDU,ROAR.IN'
)
);
What should be the query?

Someting like this should work
<?php
$query = mysql_query('SELECT * FROM GOAL');
$goal = array();
while($row = mysql_fetch_object($query)) {
if(!isset($goal[$row->type])) {
$goal[$row->type] = array();
}
$goal[$row->type][$row->key] = $row->value;
}
?>

Related

Make top of position from associative array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have this array:
$top = array( 'John' => '23.4', 'Andrew' => '12.3' , 'Eric' => '15', 'Will' => '10');
How I can get position by numeric value?
Ex: John will get position 4 because have high value
Eric get position 3.....
I want to find position of key by value!
Another way to do it like below,
<?php
function find_rank($name){
$top = array( 'John' => '23.4', 'Andrew' => '12.3' , 'Eric' => '15', 'Will' => '10');
asort($top);
return array_search($name,array_keys($top))+1; # array index starts from zero that's why added extra 1
}
echo find_rank('John');
WORKING DEMO: https://3v4l.org/8rp95
This is not correct way to handle this but if you really have to deal with it here is a dirty painful to read code
asort($top);
$top = array_flip(array_values(array_flip($top)));
echo $top['Eric']; // will result 2 (list starts from 0 maybe you would like to +1 to result)
Another option:
asort($top);
$top = array_combine(range(1, count(array_keys($top))), array_keys($top));
echo $eric= array_search('Eric', $top); // return 3

PHP: Recursively enhance array from set of strings [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have an array that needs to be enhanced dynamically from the values of several strings.
$paths = array(
'1/4/6',
'1/2/4/12/4'
);
// desired result
$target = array(
1 => array(
2 => array(
4 => array(
12 => array(
4 => 'somevalue'
)
)
),
4 => array(
6 => 'somevalue'
)
)
);
Question is: how would I get from $paths to $target?
Thank you
Explode on / for a path say '1/4/6'. Now, you have 1,4 and 6.
Keep assigning them iteratively to the previous parent key. In the below code, I have made use of & to edit the same address location of the child.
<?php
$paths = array(
'1/4/6',
'1/2/4/12/4'
);
$target = array();
foreach($paths as $path){
$temp = &$target;
foreach(explode("/",$path) as $key){
if(!isset($temp[$key])) $temp[$key] = array();
$temp = &$temp[$key];
}
$temp = 'some value';
}
print_r($target);
Demo: https://3v4l.org/P3VQB

Array value as index [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have an array
$term = array(
0 => array(
'id'=>'0902001',
'name'=>'bob',
'cgpa'=>'3.81',
),
1 => array(
'id'=>'0902002',
'name'=>'jhon',
'cgpa'=>'3.52',
),
);
I want to make a new array that the id will be the index for cgpa of new array.
$new_arr = array(
'0902001' => '3.81',
'0902002' => '3.52',
);
Thanks!
This should work for you
for ($i=0, $c = count($term); $i<$c; ++$i) {
$new_arr[$term[$i]['id']] = $term[$i]['cgpa'];
}
// for old php version
$new_arr = array();
foreach($term as $value){
$new_arr[$value['id']] = $value['cgpa'];
}
// for php 5.5+
$new_arr = array_column($term, 'cgpa', 'id');

Sort PHP Numerically & Identify which is closest to date [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've got an array for events on my site that looks like this:
array(
'title' => 'Name',
'link' => 'http://www.eventssite.com',
'image' => '_img/event_img.jpg',
'location' => 'Florida, US',
'year' => '2013',
'date' => 'Dec. 12-14',
'desc' => 'Description about the event.',
'dateid' => '1212013'
),
I'd like to sort the array before the foreach by the dateid so that they show in the proper Date order.
Additionally I'm trying to identify which one of the events is closest to the actual date, as I am using a carousel type system that needs to know which to display first.
I've researched usort and am not able to get it to go on my own, Thank you for any assistance on these!
Using this function: http://php.net/usort
An example would be something like:
<?php
//just an array of arrays with the date as one of the values of the array
$array = array(
array(
'date' => '05/02/1988',
'name' => 'Jacob'
),
array(
'date' => '12/12/1968',
'name' => 'Sherry'
),
array(
'date' => '05/15/1978',
'name' => 'Dave'
)
);
//usort is used for non conventional sorting.
//which could help in this case
//NOTICE - we are not setting a variable here!
//so dont call it like $array = usort(...) you will just be setting $array = true
usort($array,'sortFunction');
//display the results
var_dump($array);
//function called by usort
function sortFunction($a,$b){
//turn the dates into integers to compare them
//
$strA = strtotime($a['date']);
$strB = strtotime($b['date']);
//don't worry about sorting if they are equal
if($strA == $strB){
return 0;
}
else{
//if a is smaller than b, the move it up by one.
return $strA < $strB ? -1 : 1;
}
}
?>
(in case youre interested, line 40 is called a Ternary)
edited for clarity

Understanding variable assignment in a foreach loop [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a pretty basic question:
What is this statement actually doing (specifically the =>$p)?
foreach ($email->parts as $partno=>$p) {
I understand the the basics but the =>$p is not clear
In a foreach loop you can ask for both the key and value to be returned
$array = array('cat' => 'Tom', 'mouse' => 'Jerry');
foreach($array as $animal => $name) {
echo $name . ' is a ' . $animal . '<br>';
}
So the loop will output
Tom is a cat
Jerry is a mouse
This synthax is to assign the array key name or object property name to the variable $partno and it's value to $p.
This way you can do for instance $email->parts[$partno] = $p;.
It can be particularly useful if you have parallel arrays with different information bound by the key, so you need this information to obtain related data from the other array when iterating one of them.
For instance:
$person = array(
1 => 'Santa Claus',
2 => 'Homer Simpson',
3 => 'Papa Smurf'
);
$location = array(
1 => 'North Pole',
2 => 'Springfield',
3 => 'Smurf village'
);
foreach ($person as $id => $name)
echo "$name live in {$location[$id]}\n";
$partno is the key, $p is value
e.g. $email->parts = array("key" => "value");
Read this
http://www.php.net/manual/en/control-structures.foreach.php

Categories