Am trying to arrange data's that i retrieved from database in an an array, but it can't get the array structure the way i want it to look like.
<?php
$ujahdb->prepare("SELECT * FROM productcomment WHERE code = :code");
$ujahdb->bind(":code", $GetProductCode);
$ujahdb->execute();
$productComments = $ujahdb->getAll();
$ujahdb->free();
if(!empty($productComments)){
//require_once('ini.config/ProductStarRating.php');
$rateing['comments'] = '';
foreach($productComments as $com){
$rateing[]['name'] = $com->commenter_name;
$rateing['star'] = $com->comment_rate;
print_r($rateing);
}
}
?>
This is the array am getting
Array ( [comments] => [0] => Array ( [name] => Peter ) [star] => 3 )
Array ( [comments] => [0] => Array ( [name] => Jonh ) [star] => 2 [1]
=> Array ( [name] => Peter ) )
I need something like this-
Array([comments] => Array([0] => Array([name] => Peter [star] => 3)
[1] => Array([name] => Jonh [star] => 2)))
Any idea how to make this right?
I think this should do it
<?php
$ujahdb->prepare("SELECT * FROM productcomment WHERE code = :code");
$ujahdb->bind(":code", $GetProductCode);
$ujahdb->execute();
$productComments = $ujahdb->getAll();
$ujahdb->free();
if(!empty($productComments)){
//require_once('ini.config/ProductStarRating.php');
foreach($productComments as $com){
$rateing = [];
$rateing["comments"] = [
"name" => $com->commenter_name,
"star" => $com->comment_rate,
];
print_r($rateing);
}
}
?>
Related
I have a multidimensional array in php and i need to change the column order with the order of a second simple array.
EDIT:
Although both arrays are the same in regard of values and keys, im using this for export with phpexcel, and it generates the xls file with the order of the given array. I need to change that order so the xls file looks that way.
The array looks like this:
Array
(
[0] => Array
(
[name] => Name1
[sn] => Sn1
[somenumber] => 43234234
)
[1] => Array
(
[name] => Name2
[sn] => Sn2
[somenumber] => 4564564
)
[2] => Array
(
[name] => Name3
[sn] => Sn3
[somenumber] => 6575647456745
)
)
And the second array is this:
Array
(
[0] => sn
[1] => name
[2] => somenumber
)
What i need is the first array to be ordered based on the second so it looks like this:
Array
(
[0] => Array
(
[sn] => Name1
[name] => Sn1
[somenumber] => 43234234
)
[1] => Array
(
[sn] => Name2
[name] => Sn2
[somenumber] => 4564564
)
[2] => Array
(
[sn] => Name3
[name] => Sn3
[somenumber] => 6575647456745
)
)
this is how you can sort your array:
$template array:
//template array
$reference = array('sn', 'name', 'somenumber');
$array_to_sort = Array
(
"0" => Array
(
"somenumber" => "Name1",
"sn" => "Sn1",
"name" => "43234234"
),
"1" => Array
(
"sn" => "Name2",
"somenumber" => "4564564",
"name" => "Sn2"
),
"2" => Array
(
"sn" => "Name3",
"name" => "Sn3",
"somenumber" => "6575647456745"
)
);
$ordered_array = [];
foreach ($array_to_sort as $key => $value) {
$ordered_array[] = array_replace(array_flip($reference), $value);
}
print_r($ordered_array);
If all keys always present
// Make template array with correct order of keys
$template = array_flip($second);
foreach($array as &$x) {
// replace values in template
$x = array_replace($template, $x);
}
demo
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);
I have a query using a join on 2 tables which returns something like this :
Array
(
[0] => Array
(
[id] => 1
[description] => 'Test'
[image] => '1.jpg'
)
[1] => Array
(
[id] => 1
[description] => 'Test'
[image] => '2.jpg'
)
[2] => Array
(
[id] => 2
[description] => 'Test 2'
[image] => '11.jpg'
)
)
Is there a way to get an Array like this one :
Array
(
[0] => Array
(
[id] => 1
[description] => 'Test'
[image] => array('1.jpg', '2.jpg')
)
[2] => Array
(
[id] => 2
[description] => 'Test 2'
[image] => '11.jpg'
)
)
I want to group some indexes. For now, I use a loop and a if condition. But I want to know if someone use an other way.
As far as I know, there is no other way.
You can use Mysql GROUP_CONCAT, which will help you get this array :
Array
(
[0] => Array
(
[id] => 1
[description] => 'Test'
[image] => '1.jpg 2.jpg'
)
[2] => Array
(
[id] => 2
[description] => 'Test 2'
[image] => '11.jpg'
)
)
You might be able to split the image string using array_map :
$formattedResults = array_map($results, function($result){
$result['image'] = explode(' ', $result['image']);
return $result;
});
(Code might need some tuning to suit your need)
Try to implement this
$result = [];
foreach ($array as $key => $value) {
$hash = $value['id'];
if(isset($result[$hash])){
$temp[] = "{$value['image']}";
$result[$hash]['image'] = $temp;
}else{
$temp = array();
$temp[] = $value['image'];
$result[$hash] = $value;
}
}
for me its working..
I have to admit that understanding the tables are big challenge for me so please dont judge me so hard...
This is my array:
Array
(
[names] => Array
(
[0] => Name1
[1] => Name2
[2] => Name1
)
[ids] => Array
(
[0] => 1
[1] => 2
[2] => 1
)
[quantities] => Array
(
[0] => 255
[1] => 2
[2] => 467
)
)
And i wish to sum "quantities" where names or ids are the same.
Example output should be:
Array
(
[names] => Array
(
[0] => Name1
[1] => Name2
)
[ids] => Array
(
[0] => 1
[1] => 2
)
[quantities] => Array
(
[0] => 722
[1] => 2
)
)
I know there is a function like "array_reduce" but don't know how to use it.
Thanks for help!
try this
$result = [];
foreach($array['ids'] as $key=>$val ){
if(array_key_exists($val, $result)){
$result[$val]['sum_quantity'] += $array['quantities'][$key];
}
else{
$result[$val]['sum_quantity'] = $array['quantities'][$key];
$result[$val]['name'] = $array['names'][$key];
$result[$val]['id'] = $array['ids'][$key];
}
}
and output will be like this
Array
(
[1] => Array //array key = id
(
['name'] => Name1,
['sum_quantity'] => 722,
['id'] => 1
)
[2] => Array
(
['name'] => Name2,
['sum_quantity'] => 2,
['id'] => 2
)
)
You can do this way :
$testArray['names'][0]='name1';
$testArray['names'][1]='name2';
$testArray['names'][2]='name1';
$testArray['ids'][0]=1;
$testArray['ids'][1]=2;
$testArray['ids'][2]=1;
$testArray['quantities'][0]=255;
$testArray['quantities'][1]=2;
$testArray['quantities'][2]=467;
echo "<pre>";
print_r($testArray);
$unqArray['names']=array();
$unqArray['ids']=array();
$unqArray['quantities']=array();
foreach($testArray['ids'] as $key=>$value)
{
if(!in_array($value,$unqArray['ids']))
{
$unqArray['names'][]=$testArray['names'][$key];
$unqArray['ids'][]=$testArray['ids'][$key];
$quantity=0;
foreach($testArray['ids'] as $keyId=>$valueId)
{
if($valueId==$value)
{
$quantity+=$testArray['quantities'][$keyId];
}
}
$unqArray['quantities'][]=$quantity;
}
}
print_r($unqArray);
echo "</pre>";
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);