building a multidimensional array from mysql with php - php

I am trying to form a specific multidimensional array from a mysql result set.
I would like it to look like this:
array(
'product_name' => 'prod_1',
'categories' => array(1,2,3,4)
);
The db result return an array that looks something like this
Array
(
[0] => Array
(
[id] => 1
[product_name] => prod_1
)
[1] => Array
(
[id] => 2
[product_name] => prod_1
)
[2] => Array
(
[id] => 3
[product_name] => prod_1
)
[3] => Array
(
[id] => 4
[product_name] => prod_1
)
As you can see, i would like to group the product name and place the id into another array
Does anyone have any tips on how to do this?

$yourarray = array(array('id'=>1, 'product_name' => 'prod_1'), array('id'=>2, 'product_name' => 'prod_2'),array('id'=>3, 'product_name' => 'prod_3'));
$multiarray = array();
for($i=0; $i<count($yourarray);$i++){
$multiarray['product_name'][] = $yourarray[$i]['product_name'];
$multiarray['product_id'][] = $yourarray[$i]['id'];
}
print_r($yourarray); //original array
print_r($multiarray); //gives you multi array
Something similar to this?
Your original array:
Array
(
[0] => Array
(
[id] => 1
[product_name] => prod_1
)
[1] => Array
(
[id] => 2
[product_name] => prod_2
)
[2] => Array
(
[id] => 3
[product_name] => prod_3
)
)
The result would print:
Array
(
[product_name] => Array
(
[0] => prod_1
[1] => prod_2
[2] => prod_3
)
[product_id] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)

Did you try GROUP_CONCAT. It's something like:
SELECT name, GROUP_CONCAT(name) AS friends FROM friendships GROUP BY name;
Have a look for details here: http://forums.mysql.com/read.php?10,287931,287936#msg-287936

$yourarray = array(array('id'=>1, 'product_name' => 'prod_1'), array('id'=>2, 'product_name' => 'prod_1'), array('id'=>3, 'product_name' => 'prod_1'),array('id'=>4, 'product_name' => 'prod_1'));
$multiarray = array();
foreach ($yourarray as $value) {
if(!isset($multiarray['product_name'])) {
$multiarray['product_name'] = $value['product_name'];
}
$multiarray['categories'][] = $value['id'];
}
print_r($multiarray);

Related

merge two array values

How can I make this array:
Array
(
[0] => Array
(
[id] => 3412341233214
[number] => 21000
)
[1] => Array
(
[id] => 12121212121212
[number] => 18000
)
[2] => Array
(
[id] => 12121212121212
[number] => 17000
)
)
Look like the one below, where [1] and [2] have been merged into a single array based on having the same id and the number has been added together.
Array
(
[0] => Array
(
[id] => 3412341233214
[number] => 21000
)
[1] => Array
(
[id] => 12121212121212
[number] => 35000
)
)
Create a $tmp array the store the number property in it.
Only store it if the id doesn't exist yet using:
if(!isset($tmp[$obj['id']]))
To make each value unique
After that, count the $tmp number with the $array number.
$tmp[$obj['id']]['number'] += $obj['number'];
https://3v4l.org/UMJ4p
$array = array(
0 => array(
"id" => 3412341233214,
"number" => 21000
),
1 => array(
"id" => 12121212121212,
"number" => 18000
),
2 => array(
"id" => 12121212121212,
"number" => 17000
)
);
$tmp = Array();
foreach($array as $obj) {
if(!isset($tmp[$obj['id']])) {
$tmp[$obj['id']] = array_merge(Array('number'=>1),$obj);
continue;
}
$tmp[$obj['id']]['number'] += $obj['number'];
}
print_r(array_values($tmp));
The following code results into
(
[0] => Array
(
[number] => 21000
[id] => 3412341233214
)
[1] => Array
(
[number] => 35000
[id] => 12121212121212
)
)

How to group posted array names by index in PHP

I am currently working on a project where the fields scale when clicking on the "Add" button.
I am grouping each field like this: name="packaging[]", name="packaging[1]", name="packaging[2]" and so on. When I submit the form, this is how the data looks like when posted:
Array
(
[packaging] => Array
(
[0] => 1
[1] => 2
)
[quantity] => Array
(
[0] => 1
[1] => 2
)
[total-weight] => Array
(
[0] => 1
[1] => 2
)
[length] => Array
(
[0] => 1
[1] => 2
)
)
Using PHP I would like to convert the above code to look like this:
Array
(
[0] => Array
(
[packaging] => 1,
[quantity] => 1,
[total-weight] => 1,
[length] => 1,
)
[1] => Array
(
[packaging] => 2,
[quantity] => 2,
[total-weight] => 2,
[length] => 2,
)
)
Any help would be greatly appreciated.
Try this....
$array=array();
foreach($data as $key=>$value){
foreach($value as $k=>$val){
$array[$k][$key]=$val;
}
}
DEMO
Try this code:
$rows = array ('packaging' => array ('0'=> 1,'1' => 2),'quantity' => array('0'=> 1,'1' => 2),'total-weight' => array ('0'=> 1,'1' => 2),
'length' =>array ('0'=> 1,'1' => 2)
);
$res_array = array();
$total_records = count($rows['packaging']);
for($i=0;$i<$total_records;$i++)
{
$res_array[] = array('packaging'=>$rows['packaging'] [$i],'quantity'=>$rows['quantity'][$i],
'total-weight'=>$rows['total-weight'][$i],'length'=>$rows['length'] [$i]);
}
print_r($res_array);

Order array multi-dimensional

I have an array that looks like this:
$cars = array (
array(
'a' => 'audi',
'b' => 'a4'),
array(
'a' => 'peugeot',
'b' => '306'),
array(
'a' => 'audi',
'b' => 'a4'),
array(
'a' => 'audi',
'b' => 'a5'),
array(
'a' => 'peugeot',
'b' => '106'),
array(
'a' => 'peugeot',
'b' => '106'),
);
I need to order arrays like this to (id is the same as name):
name => audi
id=> audi
data => a4 => 2
a5 => 1
name => peugeot
id=> peugeot
data => 306 => 1
106 => 2
So the car brands need to be grouped an the car types counted.
I already have this code; but that is only for the group part and the count part is missing.
function mergeAndOrder($data){
// set group arrays
$i = 0; $group1 = array();
// loop trough array
$array = array(); $array2 = array();
if($data != null){
foreach($data AS $row){
// search and order level1
$search = array_search($row->a,$group1);
// this object is not found
if(is_int($search) == false){
$group1[$i] = $row->a;
$array[$i]['id'] = $row->a;
$array[$i]['name'] = $row->a;
$array[$i]['data'] = array();
$i++;
}
}
}
return $array;
}
Does somebody know an solution for this case? Thanks!
--- INPUT (part of) ---
a = lease company in this case
Array
(
[0] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
[1] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
[2] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
[3] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
--- OUTPUT (part of) ---
Array
(
[0] => Array
(
[id] => LPN
[name] => LPN
[data] => Array
(
)
)
[1] => Array
(
[id] => ARV
[name] => ARV
[data] => Array
(
)
)
[2] => Array
(
[id] => ARVB
[name] => ARVB
[data] => Array
(
)
)
[3] => Array
(
[id] => LPD
[name] => LPD
[data] => Array
(
)
)
)
Array
(
[0] => Array
(
[id] => LPN
[name] => LPN
[data] => Array
(
)
)
[1] => Array
(
[id] => ARV
[name] => ARV
[data] => Array
(
)
)
[2] => Array
(
[id] => ARVB
[name] => ARVB
[data] => Array
(
)
)
If I understand your question correctly, this should do what you want.
function mergeAndOrder ($data) {
$output = array();
foreach ($data as $item) {
$id = $item->a;
$value = $item->b;
if (!array_key_exists($id, $output)) {
$output[$id] = array('id' => $id, 'name' => $id, 'data' => array());
}
if (!array_key_exists($value, $output[$id]['data'])) {
$output[$id]['data'][$value] = 0;
}
$output[$id]['data'][$value]++;
}
// Order by name element
uasort($output, function ($a, $b) {
return strcasecmp($a['name'], $b['name']);
});
return $output;
}
Output:
Array
(
[audi] => Array
(
[id] => audi
[name] => audi
[data] => Array
(
[a4] => 2
[a5] => 1
)
)
[peugeot] => Array
(
[id] => peugeot
[name] => peugeot
[data] => Array
(
[306] => 1
[106] => 2
)
)
)

Get information for an "unique id" from a multidimensional array

I have an array with this structure:
Array
(
[mysite] => Array
(
[0] => Array
(
[0] => uniqueid
[1] => brand
[2] => horsepower
[3] => topspeed
)
[1] => Array
(
[0] => uniqueid
[1] => brand
[2] => horsepower
[3] => topspeed
)
[2] => Array
(
[0] => uniqueid
[1] => brand
[2] => horsepower
[3] => topspeed
)
)
)
Every car has an "uniqueid" followed by brand, horsepower, etc.
If I want to get the information for a random car, I do it like this:
$rkey = array_rand($sites['mysite'], 1); // get a random key
$car_info = $myarray['mysite'][$rkey];
Do you have any ideas on how to get information by using a certain "uniqueid"
$car_info = "get the information of a car with a certain uniqueid";
Ty!
try,
Pass unique id inside the for loop you will get the desird result
It may help you,
$array = array(
0 => Array
(
0 => 'uniqueid0',
1 => 'brand',
2 => 'horsepower',
3 => 'topspeed',
),
1 => Array
(
0 => 'uniqueid1',
1 => 'brand',
2 => 'horsepower',
3 => 'topspeed',
),
2 => Array
(
0 => 'uniqueid2',
1 => 'brand',
2 => 'horsepower',
3 => 'topspeed',
),
3 => Array
(
0 => 'uniqueid3',
1 => 'brand',
2 => 'horsepower',
3 => 'topspeed',
),
);
foreach($array as $arr){
if($arr[0] == 'uniqueid2'){
$result = $arr;
break;
}
}
print_r($result );
output:
Array ( [0] => uniqueid2 [1] => brand [2] => horsepower [3] => topspeed )
if the uniqueid is unique across cars then you can try restructure the array as follows
if possible
Array (
[mysite] => Array
(
[uniqueid1] => Array
(
[0] => uniqueid1
[1] => brand
[2] => horsepower
[3] => topspeed
)
[uniqueid2] => Array
(
[0] => uniqueid2
[1] => brand
[2] => horsepower
[3] => topspeed
)
[uniqueid3] => Array
(
[0] => uniqueid3
[1] => brand
[2] => horsepower
[3] => topspeed
)
)
)
this way you can directly access the car for uniqueid and can also apply random logic
if not #Jevgeni Bogatyrjov solution works well
foreach ($myarray['mysite'] as $car) {
if($car[0] == $uniqueid) {
$car_info = $car;
break;
}
}
$uniqueid = 12345;
foreach ($myarray['mysite'] as $car) {
if($car[0] == $uniqueid) {
$car_info = $car;
break;
}
}

How do i merge the arrays in a particular format?

I have following arrays:
1) for total placed
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totalplaced] => 8
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totalplaced] => 1
)
)
)
2) for total working
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totalworking] => 4
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totalworking] => 1
)
)
)
3) for total trained
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totaltrained] => 8
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totaltrained] => 1
)
)
)
I wanted to merge these arrays so that the resultant array should look like this
[newarray] => Array(
[0] => Array (
[centers] => Array
(
[name] => delhi
[id] => 1
[totalplaced] => 8
[totalworking] => 4
[totaltrained] => 8
)
)
[1]=> Array(
[centers] => Array
(
[name] => mumbai
[id] => 2
[totalplaced] => 1
[totalworking] => 1
[totaltrained] => 1
)
)
)
This is the tabular representation of the above data which i want to display
centername totalplaced totalworking totaltrained
delhi 8 4 8
mumbai 1 1 1
Please help me on this.
Thanks
Pankaj Khurana
The difficulty here is that PHP's functions such as array_merge() and array_merge_recursive() will not merge data into numeric keys, but rather will re-key any duplicate numeric key. So for example given two arrays:
array(
'test' => 'abc',
0 => 'xyz'
);
array(
'test' => 'def',
0 => 'uvw'
);
Merging them together with array_merge() will produce an array like:
array(
'test' => 'def',
0 => 'xyz',
1 => 'uvw'
);
So, you need a custom function to be "additive" on any key, regardless of whether it is a string or numeric key. Try this:
function mixed_key_array_merge() {
$args = func_get_args();
$result = array();
foreach ($args as $arg) {
// discard non-array arguments; maybe this could be better handled
if (!is_array($arg)) {
continue;
}
foreach ($arg as $key => $value) {
if (!isset($result[$key])) {
$result[$key] = $value;
} else if (is_array($result[$key])) {
$result[$key] = call_user_func_array('mixed_key_array_merge',array($result[$key],$value));
}
}
}
return $result;
}

Categories