php save array mutlidimensional - php

I develop a module under prestashop and I am looking to record a multidimensional table in a bdd.
I have a vendor column with a unique ID and row groups with a unique ID.
I have to fill in information in each box:
when I fill in some input the var_dump returns me this:
The first number is the group ID and the second number is the supplier ID.
I am trying to save this information in a table that looks like this:
To save it I would go through a class that I created new Objectif()
Here is the code I have already done. but I'm blocking on the recordings:
public function postProcess()
{
$obj = new Objectif();
if (Tools::isSubmit('objectif')) {
foreach ($_POST as $k => $item) {
$explo = explode('_', $k);
$group_id = $explo[0];
$supplier_id = $explo[1];
if ($group_id != '' && $supplier_id != '' && $item != '') {
$obj->id_group = $group_id;
$obj->id_supplier = $supplier_id;
$obj->objs = $item;
$obj->save();
}
}
}
}
Thank you for help.

Assuming that you always have this pattern group_id _ supplier_id:
foreach ($_POST as $k => $item) {
$obj = new Objectif();
$explo = explode('_', $k);
$group_id = $explo[0];
$supplier_id = $explo[1];
if ($group_id != '' && $supplier_id != '' && $item != '') {
$obj->id_group = $group_id;
$obj->id_supplier = $supplier_id;
$obj->objs = $item;
$obj->save();
}
}
Now you have all information you need.

Related

How to add blank rows to separate between data in PHPexcel

I want add a blank rows between the data to separated it on PHPExcel
the data is like this picture
i want to separate jhonny and barry with blank rows
How's the code to do that ?
A very rough example, but hope you'll understand the logic.
$user_id = 'no_possible_duplicate_user_id';
foreach(){
if($user_id != $current_iteration_user_id){
//code for new row here as a separator
}
// here is the code for displaying each record on an excel row
$user_id = $current_iteration_user_id;
}
check unique ID of user and separate by push blank array or null value.
$id= null;
foreach ($variable as $key => $value) {
if ($id == $value->id || $id == null) {
$insert[] = array(); //insert data
}esle{
$insert[] = array();//balank array
$insert[] = array(); //insert data
}
$id = $value->id;
}

Incrementing the value in multidimensional array in php

I couldn't understand the multidimensional array in PHP properly. I have a CSV file having two columns as shown below:
I am trying to create an array of array, in which each key is a cataegory. However, the value of each key is an array. In this array, each key is company and value is the count of the product. See below the code:
<?php
//array contains value
function contains_value($my_array, $value_search){
foreach ($my_array as $key => $value) {
if ($value === $value_search)
return true;
}
return false;
}
//array contains key
function contains_key($my_array, $key_search){
foreach ($my_array as $key => $value) {
if ($key === $key_search)
return true;
}
return false;
}
$handle = fopen("product_list.csv", "r");
$products = array();
if ($handle) {
while (($line = fgets($handle)) !== false) {
$product = explode(",", $line);
$category = $product[0];
$company = $product[1];
if (contains_key($products, $category)) {
if (contains_value($products, $company)) {
//increase the count of category by 1
$products[$category][$company] = $products[$category][$company] + 1;
} else {
//append new company with count 1
array_push($products[$category], array(
$company,
1
));
}
} else {
//initialize new company with count 1
$products[$category] = array(
$company,
1
);
}
}
fclose($handle);
}
var_dump($products);
?>
I noticed that the var_dump($products) is not showing correction information. I am expecting following kind of result:
I haven't enough reputation to reply, but I think he need counts.
To complete the answer of Alive to Die, more something like this:
if (!array_key_exists($category, $products)) {
products[$category] = [];
}
if (!array_key_exists($company, $products[$category])) {
products[$category][$company] = 0;
}
++$results[$cataegory][$company];
But cleaner ;)
Edit:
If I remember well, his first idea was this:
$products[$category][] = $company;
The code is shorter. Maybe you can combine the two ideas.

Find array with a specific data string and return another data string it has

I have this JSON(Cant change it)
every array in bbData contains["Username","ID","Age"]
{"bbData":[
["Peter","/id/5423","42.4"],
["Bob","/id/5355","32.1"],
["Dolan","/id/5113","22.6"]
]]}
I know an id for a user, let's say "/id/5423".
How can i then make PHP find the array with that id and return the age data which lays in the same array?
PHP >= 5.5.0
$data = json_decode($myJsonString);
$searchId = "/id/5355";
$key = array_search($searchId, array_column($data->bbData, 1));
if ($key === false) {
$found = $searchId, ' not found';
} else {
$found = $data->bbData[$key];
}
var_dump($found);
EDIT
For earlier versions of PHP, replace
$key = array_search($searchId, array_column($data->bbData, 1));
with
$key = array_search(
$searchId,
array_map(
function($value) {
return $value[1];
},
$data->bbData
)
);
Decode the array into PHP first -
$users = json_decode($jsonString,true);
Then (there will be a better way of doing this with array_search), but this will suffice for you -
$knownUserId = 123;
$userDetails = false;
foreach($users AS $user) {
if($user[1] == '/id/'.$knownUserId) {
$userDetails = $user;
break;
}
}
You now have the user details in $userDetails

Entity metadata wrapper

i'm getting error with metadata wrapper.
i have a field test => entity reference multiple which is a selection list.I get the following Error EntityMetadataWrapperException : Invalid data value given. Be sure it matches the required data type and format.
$account = entity_load_single('user', $user->uid);
$acc_wrapper = entity_metadata_wrapper('user', $account);
$list = $acc_wrapper->test->value();
$exists = FALSE;
if (!empty($list)) {
foreach ($list as $item) {
if ($item->nid == $form_state['storage']['node']->nid) {
$exists = TRUE;
break;
}
}
}
if (!$exists) {
if (!$list) {
$list = array();
$list[] = $form_state['storage']['node']->nid;
}
$acc_wrapper->test->set($list);
$acc_wrapper->save();
1rst quick tips
$account = entity_load_single('user', $user->uid);
$acc_wrapper = entity_metadata_wrapper('user', $account);
You don't need to load the entity unless you need it loaded after (Or it's already loaded). All you need is the id, and let entity_metadata_wrapper magic operate.
$acc_wrapper = entity_metadata_wrapper('user', $user->uid);
I think your error is here
if (!$list) {
$list = array();
$list[] = $form_state['storage']['node']->nid;
}
$list is always initiated because of "$list = $acc_wrapper->test->value();", so you never fullfill the condition, and then you are trying to set it back and save it (because you are missing a '}' )... Makes no sense...
Could try this version ?
$acc_wrapper = entity_metadata_wrapper('user', $user->uid);
$list = $acc_wrapper->test->value();
$exists = FALSE;
if (!empty($list)) {
foreach ($list as $item) {
if ($item->nid == $form_state['storage']['node']->nid) {
$exists = TRUE;
break;
}
}
}
if (!$exists && !$list) {
$list = array($form_state['storage']['node']->nid);
$acc_wrapper->test = $list;
$acc_wrapper->save();
}

Check value on all fields in while loop w/out using a separate if statement on each field

I am trying to find a non-redundant way to check if my returned fields are blank '' ..
For example, I am currently do this to check all fields:
while ($row = mysql_fetch_array($result)) {
if ($row['yr'] == '') {
$row['yr'] = "Unavailable";
}
if ($row['work_cmt'] == '') {
$row['work_cmt'] = "Unavailable";
}
I have about 20 fields I need to check and this just seems so redundant. I have not been able to find a php function that fits this and not sure exactly what the best approach is to this.
You could loop over the items with foreach, tetsing each one in turn :
foreach ($row as $key => $value) {
if ($value == '') {
$row[$key] = 'Unavailable';
}
}
Use a foreach loop.
while ($row = mysql_fetch_array($result)) {
foreach($row as $key->$value) {
if ($value == '') {
$value = 'unavailable';
}
}

Categories