how to mix multiple array in to one array in php - php

I am getting array from database but it creating multiple array. I need only one array from database. Now simply I want to create one array from multiple array.
I got array from database -
Array(
[0] => Array(
[0] => 1
[pro_ref_id] => 1
)
[1] => Array(
[0] => 1
[sale_ref_id] => 1
)
[1] => Array(
[0] => 1
[item_id] => 1
)
)
Actually I want -
Array(
[0] => 1
[pro_ref_id] => 1
[1] => 1
[sale_ref_id] => 1
[2] => 1
[item_id] => 1
)
My PHP code is -
$resultData = array();
$re=mysql_query("select pro_ref_id,pro_qty,pro_item from proforma_details where pro_ref_id IN($piid_str)");
while($re1=mysql_fetch_array($re))
{
$rq=mysql_fetch_array(mysql_query("select sale_ref_id,proforma_invoice_no from proforma_invoice where pro_invoice_id='".$re1['pro_ref_id']."'"));
$rq1=mysql_fetch_array(mysql_query("select sale_order_no from sale_order where sale_id='".$rq['sale_ref_id']."'"));
array_push($resultData,$re1);
array_push($resultData,$rq);
array_push($resultData,$rq1);
}

You can iterate over the result set to create another array:
$res = array();
foreach ($array as $row) {
$res = array_merge($res, $row);
}

Related

Split the array in to sub arrays based on array key value [duplicate]

This question already has answers here:
How to group subarrays by a column value?
(20 answers)
Closed 1 year ago.
I am facing one issue while splitting array by key value. My array looks like below :-
Array
(
[0] => Array
(
[product_id] => 6
[brand_id] => 2
)
[1] => Array
(
[product_id] => 1
[brand_id] => 1
)
[2] => Array
(
[product_id] => 5
[brand_id] => 1
)
)
Now i want to filter split the array based on brand_id. My expected output is like below:-
Array(
[0] => Array(
[0] => Array
(
[product_id] => 6
[brand_id] => 2
)
)
[1] => Array(
[0] => Array
(
[product_id] => 1
[brand_id] => 1
)
[1] => Array
(
[product_id] => 5
[brand_id] => 1
)
)
)
My Input array is stored in $proArray variable
My attempt below:-
$brands = array();
foreach ($proArr as $key => $pro) {
$brands[] = $pro['brand_id'];
}
$brands = array_unique($brands);
$ckey = 0;
foreach($brands as $brand){
}
One way to do it with simple foreach() loop to push values based on your brand_id like below-
$key = 'brand_id';
$return = array();
foreach($array as $v) {
$return[$v[$key]][] = $v;
}
print_r($return);
WORKING DEMO: https://3v4l.org/bHuWV
Code:
$arr = array(
array(
'product_id' => 6,
'brand_id' => 2
),
array(
'product_id' => 1,
'brand_id' => 1
),
array(
'product_id' => 5,
'brand_id' => 1
)
);
$res = [];
foreach ($arr as $key => $value)
$res[$value['brand_id']][] = $value;
$res = [...$res];
print_r($res);
Output:
Array
(
[0] => Array
(
[0] => Array
(
[product_id] => 6
[brand_id] => 2
)
)
[1] => Array
(
[0] => Array
(
[product_id] => 1
[brand_id] => 1
)
[1] => Array
(
[product_id] => 5
[brand_id] => 1
)
)
)

How to add multiple values to an array in php

I want to create a new array using loop(foreach).
My array is looking like this :
$q_list = Array(
[0] => Array
(
[id] => 2
[subject_id] => 1
[question] => Question No One
[recordstatus] => 1
)
[1] => Array
(
[id] => 3
[subject_id] => 1
[question] => Question No Two
[recordstatus] => 1
)
[2] => Array
(
[id] => 4
[subject_id] => 1
[question] => Question No Three
[recordstatus] => 1
)
)
I have done like this but not working :
foreach ($q_list as $key => $q) {
$question[] = $q['question'];
$question[] = $q['subject_id'];
}
This will group your array on subject_id.
I use subject_id as the key in a multidimensional array that way it will just add the question arrays to the correct subarray.
foreach($q_list as $q){
$res[$q['subject_id']][] = $q;
}
var_dump($res);
https://3v4l.org/HnlYW

Multidimensional array to single array in PHP

I am working in PHP so I have an array like this, from this array I want filter take user_id to another array like I given below.
Array
(
[0] => Array
(
[user_id] => 66
[distance] => 0
)
[1] => Array
(
[user_id] => 68
[distance] => 0
)
[2] => Array
(
[user_id] => 81
[distance] => 0
)
[3] => Array
(
[user_id] => 65
[distance] => 0.00010218008081861118
)
)
I want an array like this,
$user_id=array(66,68,81,65);
Use array_column()
Returns an array of values representing a single column from the input array.
<?php
$user_array = array(
0 => array('user_id' => 1, 'name' => 'Bob'),
1 => array('user_id' => 2, 'name' => 'John'),
2 => array('user_id' => 3, 'name' => 'Mary')
);
$users = array_column($user_array, 'user_id');
print_r($users);
Output :
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Where $array is the multidimensional array you provided above:
$data = array();
foreach ($array as $item) {
$data[] = $item['user_id'];
}
print_r($data);

Multi associate array to single array using php

This is may array and i want to make a single array from it.
Following is my current array and i want array in single array.
Array
(
[0] => Array
(
[item_name1] => First Name
[quantity1] => 1
[mc_gross1] => 189.00
)
[1] => Array
(
[item_name2] => Last Name
[quantity2] => 1
[mc_gross2] => 22.00
)
[2] => Array
(
[item_name3] => Email Addres
[quantity3] => 3
[mc_gross3] => 21.00
)
)
ANd i Want like
Array
(
[item_name1] => First Name
[quantity1] => 1
[mc_gross1] => 189.00
[item_name2] => Last Name
[quantity2] => 1
[mc_gross2] => 22.00
[item_name3] => Email Addres
[quantity3] => 3
[mc_gross3] => 21.00
)
I have tried by following way but i can not get result that i want.
<?php
$result = array();
foreach($result as $inner) {
$result[key($inner)] = current($inner);
}
?>
Someone please help me how could i resolve this ?
Try like below
<?php
$final_result = array();
foreach($result as $inner) {
$final_result = array_merge($final_result,$inner);
}
?>

how to count the array values with key

I have array like this i need to count by array values
Array ( [Cop] => Array ( [0] => Dozen [1] => Dozen [2] => Akls [3] => Akls ) [MSN] => Array ( [0] => Dozen ) [NeK] => Array ( [0] => Suhan [1] => Ebao ) [NetSE] => Array ( [0] => SuZhan [1] => Guhang ) )
For example
Array ( [Cop] => Array ( [0] => Dozen [1] => Dozen [2] => Akls [3] => Akls ))
In the Cop key i have two different values for cop so i need cop should be 2
Cop - 2
MSn - 1
NeK - 2
NetSE - 2
I need the count like above how can i do this ?
Try simply using array_map,count,& array_unique like as
array_map(function($v) {
return count(array_unique($v));
}, $arr);
Use array_unique() and then count.
count(array_unique($array['Cop']));// output 2
If you want to print for every key do following:
$array = array('Cop'=>array('Dozen','Dozen','Akls','Akls'), 'MSN'=> array('Dozen'), 'NeK'=> array('Suhan','Ebao'));
foreach($array as $key => &$value) {
$value = count(array_unique($array[$key]));
}
print_r($array);
Output:
Cop = 2
MSN = 1
NeK = 2
You should use array_count_values() for that, here's an example:
$data = array('cop' => array(0 => 'test', 1 => 'test', 2 => 'test2'));
foreach($data as $item){
$result = array_count_values($item);
print_r($result);
}
Outputs:
Array
(
[test] => 2
[test2] => 1
)

Categories