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
Related
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 10 months ago.
I have nested array with key & value pair. i want to convert it in single array.
/* This is current array */
Array
(
[id] => Array
(
[0] => 1
[1] => 2
)
[qty] => Array
(
[0] => 1
[1] => 1
)
[price] => Array
(
[0] => 364.41
[1] => 300
)
[amount] => Array
(
[0] => 364.41
[1] => 300
)
)
/*Now, I want this type of array*/
Array
(
[0] => Array
(
[id] => 1
[qty] => 1
[price] => 364.41
[amount] => 364.41
)
[1] => Array
(
[id] => 2
[qty] => 1
[price] => 300
[amount] => 300
)
)
I have tried array_walk and some other solutions but i could not find any proper solution
Thanks in advance
Main array is your nested array and Collection is the result you want
foreach($mainArray["id"] as $key => $value ){
$collection[$key] = ['id' => $value];
foreach(array_keys($mainArray) as $arrayKeysOfmainArray){
if(!array_key_exists($arrayKeysOfmainArray, $collection)){
$collection[$key][$arrayKeysOfmainArray] = $mainArray[$arrayKeysOfmainArray][$key];
}
}
}
print_r($collection);
$mainarray['id'] = Array(1,2,3);
$mainarray['qty'] = Array(1,1,4);
$mainarray['price'] = Array(364.41,300,500);
$mainarray['amount'] = Array(364.41,300,600);
$new = [];
foreach($mainarray as $key=> $singleArray){
if(count($singleArray) > 0){
foreach($singleArray as $childKey=> $value){
$new[$childKey][$key] = $value;
}
};
}`
I have this array :
Array
(
[0] => Array
(
[0] => Array
(
[0] => 10
[id_list] => 1
[id] => 1
)
[1] => Array
(
[0] => 11
[id_list] => 1
[id] => 1
)
[2] => Array
(
[0] => 12
[id_list] => 1
[id] => 1
)
)
[1] => Array
(
[0] => Array
(
[0] => 11
[id_list] => 2
[id] => 2
)
[1] => Array
(
[0] => 12
[id_list] => 2
[id] => 2
)
)
[2] => Array
(
[0] => Array
(
[0] => 13
[id_list] => 4
[id] => 4
)
)
)
and this code (where $dataListe is the result of a fetchAll query) :
$result = [];
foreach($dataListe as $listeDiff){
$result[] = $listeDiff;
}
// $resultUnique = array_unique($result);
echo "<pre>".print_r($result, true)."</pre>";
as you can see, there's some contact similar in my first and my second array (but contact can be the same in the 1st and the 3rd array, is I choose to add my contact in my 3rd array).
I want to remove the duplicate of each element in the general array.
But when I use array unique, I get this result :
Array
(
[0] => Array
(
[0] => Array
(
[0] => 10
[id_list] => 1
[id] => 1
)
[1] => Array
(
[0] => 11
[id_list] => 1
[id] => 1
)
[2] => Array
(
[0] => 12
[id_list] => 1
[id] => 1
)
)
)
Please I need help to only keep 1 item of each array at the end !
EDIT : I have almost the good result with the code below, but the id 12 is missing
$result = [];
foreach($dataListe as $listeDiff){
foreach($listeDiff as $contact){
if(!in_array($contact,$result)){
$result[] = $contact;
}
break;
}
}
As the PHP docs says :
Note: Note that array_unique() is not intended to work on multi dimensional arrays. (http://php.net/manual/en/function.array-unique.php)
You can try this solution
$uniqueResult = array_map("unserialize", array_unique(
array_map("serialize", $result)
));
as suggested by #daveilers on this question How to remove duplicate values from a multi-dimensional array in 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);
}
I have an array similar to this below. I need to find a way to extract values by using a key/value pair for instance by using 'parent_id' => 3. I want to get all values for said array ( id = 2, label = Content Creation, link_url = '', parent_id = 3 ).
I've tried using array_intersect() without any success.
Thank You for your assistance.
Array (
[0] => Array ( [id] => 1 [label] => Web Development [link_url] => [parent_id] => 1 )
[1] => Array ( [id] => 2 [label] => Content Creation [link_url] => [parent_id] => 3 )
[2] => Array ( [id] => 3 [label] => PHP Jobs [link_url] => /simple_link.php [parent_id] => 1 )
[3] => Array ( [id] => 4 [label] => OSCommerce projects [link_url] => /another_link.php [parent_id] => 4 )
)
I thik you can loop in your array and match your desidered parent_id with an if condition
foreach($array as $data)
{
if($data['parent_id'] == '3')
{
echo $data['id'] . ' ' . $data['label'] . ' ' . $data['link_url'];
}
}
You can also use array_filter for these sort of problems
$matching_results = array_filter($data, function($item) {
return ($item['parent_id'] == 3);
});
It will cycle through the data and $matching_results will be an array of each $item that returns true
This question already has answers here:
create array tree from array list [duplicate]
(9 answers)
Closed 9 years ago.
I have a multidimensional associative array "$nav_menus" looks like this:
Array
(
[0] => Array
(
[id] => 1
[menu_name] => Media
[parent_id] => 0
[submenus] => 1
)
[1] => Array
(
[id] => 2
[menu_name] => Movies
[parent_id] => 1
[submenus] => 1
)
[2] => Array
(
[id] => 3
[menu_name] => English Movies
[parent_id] => 2
[submenus] => 1
)
[3] => Array
(
[id] => 4
[menu_name] => Action
[parent_id] => 3
[submenus] => 1
)
)
What I'm trying to accomplish is to loop the array and whenever the value of the key ['parent_id'] doesn't equal 0 is to loop the array again and move this one into whatever menu the value of its ['id'] key equal that parent_id ... here is what I want $nav_menus to look like at the end
Array
(
[id] => 1
[menu_name] => Media
[parent_id] => 0
[submenus] => 1
[0] => Array
(
[id] => 2
[menu_name] => Movies
[parent_id] => 1
[submenus] => 1
[0] => Array
(
[id] => 3
[menu_name] =>English Movies
[parent_id] => 2
[submenus] => 1
[0] => Array
(
[id] => 4
[menu_name] => Action
[parent_id] => 3
[submenus] => 1
)
)
)
)
Here is what I was tried to accomplish it, but with no luck:
foreach($nav_menus as $k => $v){
if($v['parent_id'] !== 0){
$x = $v;
foreach($nav_menus as $key => $value){
if($value['id'] == $v['parent_id']){
unset($v);
array_push($value, $x);
break;
}
}
}
}
print_r($nav_menus);
It gives me the same original array without any change :( ... Any suggestions ?
Try this recursive function.
function array_new($arr1,$tot_count,$parent=0,$new_arr=array(),$global_count=0)
{
$count=0;
foreach($arr1 as $arr_res)
{
if($arr_res['parent_id']==$parent)
{
$new_arr['id'] = $arr_res['id'];
$new_arr['menu_name'] = $arr_res['menu_name'];
$new_arr['parent_id'] = $arr_res['parent_id'];
$new_arr['submenus'] = $arr_res['submenus'];
//DebugBreak();
if($global_count<$tot_count-1)
{ $global_count++;
$new_arr[$count] = array_new($arr1,$tot_count,$arr_res['id'],$new_arr,$global_count);
$count++;
}
}
}
return $new_arr;
}
'$arr_unformated' is your array which you have now.
$tot_count = count($arr_unformated);
$arrnew = array_new($arr_unformated,$tot_count);
And '$arrnew' is your desire array.