Array value as index [closed] - php

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');

Related

I have two array in php. Need to combing in one at following way [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I have two arrays:
$array1 = array('104', '104', '104', '51', '228', '228');
$array2 = array('12121', '12120', '12119', '11821', '11788', '11787');
I need to create an array with two dimensions consisting of the elements of these two arrays in a specific way:
$array3 = array('104'=>array('12121', '12120', '12119'),'51'=>array('11821'),'228'=>array('11788', '11787'));
Array1 and Array2 always have the same number of elements.
How can I do this?
You can do it this way:
<?php
$array1 = ['104', '104', '104', '51', '228', '228'];
$array2 = ['12121', '12120', '12119', '11821', '11788', '11787'];
$result = [];
$index = 0;
foreach( $array1 as $key => $value ){
$result[$value][] = $array2[$index];
$index++;
}
Result:

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

How build nested associative array based of a one-dimensional 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 7 years ago.
Improve this question
I have one-dimensional array, e.g.
$arr = ['foo', 'bar', 'baz'];
I want convert it as follow(var_dump output):
array (size=1)
'foo' =>
array (size=1)
'bar' =>
array (size=1)
'baz' => string '' (length=0)
I can use only loop(for and/or foreach). Recursive functions is not allowed. PHP as primary programming language.
Please, help me write code for this transformation.
$r = array('a', 'b', 'c');
$res = array();
foreach (array_reverse($r) as $i) {
$tmp = $res;
$res = array();
$res[$i] = $tmp;
}
echo '<pre>', print_r($res);

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

Create multidimentional array from SQL table [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 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;
}
?>

Categories