For PHP, is it possible to do something like this:
array( $designationVar => $dataVar );
With the idea being that I can dynamically create an array based on the values present
Yes why not , this is equivalent to like this
$arr = array();
$arr[$keyname] = $value;
In your case
$arr = array();
$arr[$designationVar] = $dataVar;
Related
I'm trying to create an unknown number of arrays dynamically inside a foreach loop, merge them all at the end into one array, and use this in a JSON format for Google Analytics.
So far I have the following code which is throwing an error at the merge part:
$p=1;
foreach(...){
...
$arr = 'arr'.$p;
$name = $order->ProductGroupName;
$name = str_replace("'", "", $name);
$arr = array(
"name"=>$name,
"id"=>$order->ProductCode,
"price"=>$order->RRP,
"quantity"=>$order->Quantity
);
$p++;
}
for ($q = 1; $q<$p; $q++){
$arry = 'arr'.$q;
$merge = array_merge($arry, $merge);
};
How do I create the arrays dynamically and merge them at the end, please?
I'm relatively new to PHP and have tried my best to get this to work.
I think I understand what you're trying to do. Just dynamically append [] to the array and you don't need to merge:
foreach($something as $order) {
$arr[] = array (
"name"=>str_replace("'", "", $order->ProductGroupName),
"id"=>$order->ProductCode,
"price"=>$order->RRP,
"quantity"=>$order->Quantity
);
}
If you want to have string keys for whatever reason, then:
$p = 1;
foreach($something as $order) {
$arr["SomeText$p"] = array (
"name"=>str_replace("'", "", $order->ProductGroupName),
"id"=>$order->ProductCode,
"price"=>$order->RRP,
"quantity"=>$order->Quantity
);
$p++;
}
And that's it. Check with:
print_r($arr);
Things like $arry = 'arr'.$q; stink of variable variables (though not done correctly) and shouldn't be used.
in my PHP code I have many array like this:
0 = ['attr_id':1,'name':'qty','value':'100'];
1 = ['attr_id':1,'name':'qty','value':'200'];
2 = ['attr_id':1,'name':'qty','value':'500'];
3 = ['attr_id':2,'name':'price','value':'10$'];
I want merge this array like this:
0 = ['attr_id':1,'name':'qty','value':['100','200','500']];
1 = ['attr_id':1,'name':'price','value':'10$'];
can you guys help me please?
thanks
This should be simpler. Build the result using the attr_id as the index and append the value:
foreach($array as $values) {
$result[$values['attr_id']]['value'][] = $values['value'];
$result[$values['attr_id']] = $result[$values['attr_id']] + $values;
}
If you need to reindex, just use array_values() on the $result.
This is one method where you loop through an array_column array of the id's and use key to insert values from values array.
$value = array_column($arr, 'value');
$id = array_column($arr, 'attr_id');
$res =[];
Foreach($id as $key => $i){
If(!isset($res[$i])) $res[$i] = ['attr_id' => $i, 'name'=>'qty', 'value' => []];
$res[$i]['value'][] = $value[$key];
}
Var_dump($res);
https://3v4l.org/VXI5E
As you see I use the id as key to keep track of the result array.
If you want to clear this, meaning reset the counting from 0, use array_values.
$res = array_values($res);
Edit: also the 10$ is inside an array in my answer, this makes it easier to use the array later in my opinion.
If you must save it as an string I can fix it, but it will probably be harder to use the array later with a mixed item.
I don't know is there such thing as dynamic array_intersect? Anyway i have 3 arrays ( later there will be much more arrays)
$kaID = array();
$tgID = array();
$ciID = array();
I want to find matching values for all arrays using array_intersect
Arrays can be created and filled with values or not.
It can be only one populated array OR there can be all three. (later on there will be much more arrays.
How to iterate and create some kind of dynamic expression and get something like this:
array_intersect ($kaID, $tgID,$ciID,.... );
You can do something like this:
$collection = [];
//Dynamic
foreach($ids as $id) {
$collection[] = $id;
}
$result = call_user_func_array('array_intersect', $collection);
I want to create a multidimensional session array in PHP.
$_SESSION['basket'][$id] = $array
$array refers to an array.
When I run this for a new $id, it is overwriting the previous entry in the $_SESSION['basket'].
Means every time count($SESSION['basket']) is 1.
Actually, i want to achieve following structure.
$_SESSION['basket'] = array($id1 => array(), $id2 => array(), .....)
EDITED:
if(!isset($_SESSION['basket'])) {
$_SESSION['basket'] = array();
$_SESSION['basket'][$id] = $array;
}
else{
$_SESSION['basket'][$id] = $array;
}
when i do
$_SESSION['basket'][$id] = implode('/',$array);
it works. but not working for another array as value.
How can i do this?
thanks.
I am providing the answer for my question. It may help some other people who have same issue.
do not store any objects in $_SESSION varibles. I was storing
$array = array( $key=>object($value),....)
Therefore, I cast object to string
$array = array($key => (string)$value, ...)
and it works.
hope it will help someone.
Use it like this:
$tmp = $_SESSION['basket'];
$tmp[$id] = array();
$tmp[$id] = $array;
That'll work.
I have the following array:
$data['standard'][36][2] = 52.5;
$data['standard'][42][2] = 57.5;
$data['standard'][48][2] = 62.5;
$data['standard'][54][2] = 67.5;
$data['standard'][60][2] = 72.5;
$data['standard'][36][3] = 60.5;
$data['standard'][42][3] = 65.5;
$data['standard'][48][3] = 70.5;
$data['standard'][54][3] = 75.5;
$data['standard'][60][3] = 80.5;
$data['standard'][72][3] = 90.5;
i'm trying to return the keys of the third index where the first two match. e.g. for 'standard' and 48 need an array(2,3)
but for 'standard' and 72 i would return array(3)
Also I'm wondering if I should store this data in xml or something similar?
Try this:
$result = array_keys($data['standard'][48];
This just returns the keys of the $data['standard'][48] array: 2 and 3.
You can use something like this:
function findInArray(&$data,$param1,$param2)
{
return isset($data[$param1][$param2]) ? array_keys($data[$param1][$param2]) : array();
}
Example:
$keys = findInArray($data,"standard",48); // array(2,3);
$keys = findInArray($data,"standard",72); // array(3);
It is fine to use an array for storing data, if this is easier for you to handle.