I need a help
This is my session array:
Array
(
[menu] =>
[id] => 3
[products] => Array
(
[0] => Array
(
[id] => 1
[name] => Produkt 1
[code] => 1
[varianta] =>
[pocet] => 1
[price] => 20
[pricepredtym] => 40
)
[1] => Array
(
[id] => 2
[name] => Produkt 1
[code] => 1
[varianta] =>
[pocet] => 1
[price] => 20
[pricepredtym] => 40
)
)
)
I would need about something like, if ($_GET [id] == $ _SESSION ['products'] [id]) and only change this "[pocet]" where [id] = 2
$_GET [id] = 2;
$pocet=5;
[1] => Array
(
[id] => 2
[name] => Produkt 1
[code] => 1
[varianta] =>
[pocet] => 5
[price] => 20
[pricepredtym] => 40
)
You could index your products array by product id. Then updating would be simply:
if(isset($_SESSION['products'][$prod_id])) {
$_SESSION['products'][$prod_id]['pocet'] = $pocet;
}
Otherwise, use a foreach loop:
foreach ($_SESSION['products'] as $i => $prod) {
if ($prod['id'] == $prod_id) {
$_SESSION['products'][$i]['pocet'] = $pocet;
break;
}
}
Compare your GET value with 2 and use it as key of SESSION array.
if ($_GET['id'] == '2'){
$_SESSION['products'][$_GET['id']]['pocet'] = '5';
}
Related
code
$result = [];
foreach ($getAllAgent as $rkey => $rvalue) {
$found = -1;
for ($i = 0; $i < count($result); $i++) {
if ($result[$i]["brokerid"] == $rvalue["brokerid"]) {
$found = $i;
$result[$i]['get_agent_details'][] = $rvalue['get_agent_details']; //here to combine
break;
}
}
// if not found, create new
if ($found == -1) {
$result[] = $rvalue;
}
results
Array
(
[0] => Array
(
[id] => 2
[brokerid] => 2
[agentid] => 3
[addedby] => 1
[get_agent_details] => Array
(
[id] => 3
[name] => kenang
[ic] => 932132923
[phone] => 2313123
[0] => Array
(
[id] => 4
[name] => ivan
[ic] => 32992131
[phone] => 31231
)
)
)
)
I have one set of an array, and I loop it and restructure match the data based on ID. After that I will try to merge the same data into one array, I able add into one array. But it will not combine as one. The correct result should be as below.
[get_agent_details] => Array
(
[0] => Array(
[id] => 3
[name] => kenang
[ic] => 932132923
[phone] => 2313123
),
[1] => Array
(
[id] => 4
[name] => ivan
[ic] => 32992131
[phone] => 31231
)
)
Your problem is in this line:
$result[] = $rvalue;
Consider the case where you only have one item; this will result in:
Array
(
[0] => Array
(
[id] => 2
[brokerid] => 2
[agentid] => 3
[addedby] => 1
[get_agent_details] => Array
(
[id] => 1
[name] => Chesney Hawkes
[ic] => 932132923
[phone] => 2313123
)
)
)
But to be consistent, you need get_agent_details to be a list of items, that happens to have one entry:
Array
(
[0] => Array
(
[id] => 2
[brokerid] => 2
[agentid] => 3
[addedby] => 1
[get_agent_details] => Array
(
[0] => Array
(
[id] => 1
[name] => Chesney Hawkes
[ic] => 932132923
[phone] => 2313123
)
)
)
)
So you need to re-arrange your data, for instance by writing:
$rvalue['get_agent_details'] = [0 => $rvalue['get_agent_details']];
$result[] = $rvalue;
Then, since get_agent_details will already be a list when you encounter a second matching item, your existing code in the inner loop will do the right thing:
// Add an item to the list
$result[$i]['get_agent_details'][] = $rvalue['get_agent_details'];
I am trying to parse the following php array, somehow I am getting the result, but I couldn't get the expected output.
Array:
Array (
[0] => Array (
[countNos] => 2
[question_id] => 1
[question] => Is Service Best?
[rating] => 4
)
[1] => Array (
[countNos] => 1
[question_id] => 2
[question] => How much you benifitted?
[rating] => 5
)
[2] => Array (
[countNos] => 1
[question_id] => 2
[question] => How much you benifitted?
[rating] => 2
)
)
Current code:
foreach ($ratings as $rating) {
if (!in_array($rating['question_id'], $ratingArr)) {
$ratingArr[$rating['question_id']]['question_id'] = $rating['question_id'];
$ratingArr[$rating['question_id']]['question'] = $rating['question'];
}
for ($i = 5; $i >= 1; $i--) {
if (!in_array($rating['rating'], $ratingArr[$rating['question_id']]['stars'])) {
if ($i == $rating['rating']) {
$ratingArr[$rating['question_id']]['stars'][$i] = $rating['countNos'];
}
}
}
}
This is the output I am getting:
Notice: Undefined index: stars in C:\xampp\htdocs\mibs\module\Survey\src\Survey\Service\SurveyService.php on line 153
Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\mibs\module\Survey\src\Survey\Service\SurveyService.php on line 153
Array (
[1] => Array (
[question_id] => 1
[question] => Is Service Best?
[stars] => Array (
[4] => 2
)
)
[2] => Array (
[question_id] => 2
[question] => How much you benifitted?
[stars] => Array (
[5] => 1
[2] => 1
)
)
)
But I am expecting the following output:
Array(
[1] => Array(
[question_id] => 1
[question] => Is Service Best?
[stars] => Array(
[5] => 0
[4] => 2
[3] => 0
[2] => 0
[1] => 0
)
)
[2] => Array(
[question_id] => 2
[question] => How much you benifitted?
[stars] => Array(
[5] => 1
[4] => 0
[3] => 0
[2] => 1
[1] => 0
)
)
)
How can I parse this array, I am always having this type of issue, whenever I am parsing, how can I overcome this.
You're never creating a stars sub-array in $ratingArr when you initialize a new entry, that's causing the two warnings. To get all the zero entries, you should initialize this to an array of 5 zeroes. Then you don't need a loop to add the stars, you can just fill in the appropriate entry directly.
And you need to use array_key_exists, not in_array, to check whether there's already an entry for $rating['question_id'] in $ratingArr.
foreach ($ratings as $rating) {
if (!array_key_exists($rating['question_id'], $ratingArr)) {
$ratingArr[$rating['question_id']]['question_id'] = $rating['question_id'];
$ratingArr[$rating['question_id']]['question'] = $rating['question'];
$ratingArr[$rating['question_id']]['stars'] = array_fill(1, 5, 0);
}
$ratingArr[$rating['question_id']]['stars'][$rating['rating']] = $rating['countNos'];
}
Loop through your array and check if you already have a subArray with the question id in the result array. If not, initialize the subArray. And then just add the rating.
Code:
<?php
$result = [];
foreach($array as $v){
if(!isset($result[$v["question_id"]])){
$result[$v["question_id"]] = [
"question_id" => $v["question_id"],
"question" => $v["question"],
"stars" => array_fill_keys(range(5, 1), 0),
];
}
$result[$v["question_id"]]["stars"][$v["rating"]] += $v["countNos"]
}
?>
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.
and i got a problem (its big for me) :(
ok, i have some array like ...
Array(
[0] => Array
(
[id] => 1
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
[3] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[4] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
how to merge them if the array have same key value ... ?
the result that i need got is like this ...
Array(
[0] => Array
(
[id] => 1
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
)
)
[2] => Array
(
[id] => 2
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
)
)
im very need some help here, and im working on PHP ... what method should i do for this case?
i try too searching on google and in here ... but i dont know the keyword >.<
Many thanks before :)
regard, Stecy
Try like this:
<?php
$result = array();
foreach ($my_array as $v) {
$id = $v['id'];
$result[$id]['id'] = $id;
$result[$id]['detail'][] = array(
'order_sn' => $v['order_sn'],
'total' => $v['total'],
);
}
You can just loop over the array and build a resulting one:
// $a is your array
$r=array();
foreach($a as $v)
$r[$v['id']][]=array('order_sn'=>$v['order_sn'], 'total'=>$v['total']);
echo'<pre>';
var_dump($r);
Since you do the paring by ID, it is wise to have it as the key and all the data associated with it as the value. There's no need to also have id and detail.
foreach($origianlArray as $key => $value){
$newArray[$value['id']]['id'] = $value['id'];
$newArray[$value['id']]['detail'][] = array('order_sn' => $value['order_sn'], 'total' => $value['total']);
}
Check out the PHP array_merge function.
I have my array
Array
(
[0] => Dusche
[1] => Mobliert
)
And I have second array which is composed and looks like this:
[0] => Array
(
[id] => 1002
[attribute_id] => 65
[value_id] => 26815
[name] => Garten/-mitbenutzung
[order] => 0
)
[1] => Array
(
[id] => 1003
[attribute_id] => 65
[value_id] => 26811
[name] => Etagenheizung
[order] => 1
)
[2] => Array
(
[id] => 1004
[attribute_id] => 65
[value_id] => 26829
[name] => Balkon/Terrasse
[order] => 2
How can I search this second array with values from the first array and retrieve attribute_id from elements which have the same names?
PHP way:
filteredArray = array();
foreach ($secondArray as $type) {
if (in_array($type['name'], $firstArray)) {
$filteredArray[] = $type['attribute_id'];
}
}
Cake Set way, something along the lines of:
$filteredArray = array();
foreach ($firstArray as $keyword) {
$filteredArray = array_merge($filteredArray, Set::extract("/.[name=$keyword]/attribute_id", $secondArray));
}