In my CodeIgniter controller called Leads
I am having a code which retrieve data from model (database):
$data['leads'] = $this->model_leads->get_leads_by_person($person_id);
Now I can get some fileds like:
Array ( [0] => Array ( [lead_id] => 79 [lead_date] => 2014-07-15 12:34:41 [lead_priv] => 0 [1] => Array ( [lead_id] => 395 [lead_date] => 2014-07-15 12:34:41 [[lead_priv] => 0))
Now I want to add to every lead in $data['leads'] additional value, in my example lead_chance.
So let's say that I have a variable $lead_chance='100';
How to add this as an element to an array $data['leads']?
foreach($data['leads'] as $id => $lead) {
$data['leads'][$id]['lead_chance'] = '100';
}
Something like this?
foreach ($data['leads'] as $x) {
$x['lead_chance']=$lead_chance;
}
This would give you one lead_chance per item. If you want only one lead_chance for the whole array, it is much easier:
$data['leads']['lead_chance']=$lead_chance;
Related
I am trying to use the Hubspot API (http://developers.hubspot.com/docs/overview) to loop through all deals and find only those which are current and then do something with those deals.
No matter what I try to do I cannot get my head around how I access the data I need - below is an example of the output.
In the API there are lots of items like dealstage below and the value field under these is what I need to access - for example in this case the deal is closedlost. Another example would be amount which would also have an entry in value so I can then see the deal value.
I want to loop through all deals and for each deal get the dealstage, amount, last update, owner and so on. Each of these are contained in an array of the same layout as [dealstage] below with a value
I have gotten to where I can print the dealstage value for each deal but it doesn't really help - is there a better way of doing this?
foreach ($list['deals'] as $line) {
foreach ($line['properties'] as $row => $value) {
if ($row=="dealstage") {
$stage=$value['value'];
print $stage."<br>";
}
}
}
Example array:
Array
(
[deals] => Array
(
[0] => Array
(
[portalId] => 12345
[dealId] => 67890
[isDeleted] =>
[associations] => Array
(
[associatedVids] => Array
(
[0] => 4051
)
[associatedCompanyIds] => Array
(
[0] => 23456
)
[associatedDealIds] => Array
(
)
)
[properties] => Array
(
[dealstage] => Array
(
[value] => closedlost
)
[createdate] => Array
(
[value] => 1471334633784
)
[amount] => Array
(
[value] => 1000
)
Would something like this be what you are looking for. Loop through the array picking out the items you are interested in and place them in a nice simple array for you to use later when building your email.
$for_email = array();
foreach ($list['deals'] as $line) {
$t = array();
if (isset($line['properties']['dealstage']['value'])) {
$t['dealstage'] = $line['properties']['dealstage']['value'];
}
if (isset($line['properties']['amount']['value'])) {
$t['amount'] = $line['properties']['amount']['value'];
}
if (isset($line['properties']['createdate']['value'])) {
$t['createdate'] = $line['properties']['createdate']['value'];
}
// any other data you want to capture
// put this data in the new array
$for_email[] = $t;
}
// check what the new array looks like
print_r($for_email);
The output I have is this:
[price_hidden_label] => 1
[categories] => Array
(
[0] => 26
)
My current code to try and update is:
updateProduct($product->id, array('categories[108]'));
When I run this function, it overwrites the existing array, giving me an output like this:
[price_hidden_label] => 1
[categories] => Array
(
[0] => 108
)
What I am trying to achieve would look like this:
[price_hidden_label] => 1
[categories] => Array
(
[0] => 26
[1] => 108
)
But.... in the event that 108 already exists, I would hope that it would just ignore it. I don't want:
[price_hidden_label] => 1
[categories] => Array
(
[0] => 26
[1] => 108
[2] => 108
)
Any idea how I do that?
You can push elements to the array using
$arr[] = $new_var;
and check if a value already exists by
if(in_array($new_var, $arr))
In example (trying to follow your input-output):
$categories = array();
$categories[] = 26;
$input_value = 108;
if(!in_array($input_value, $categories)) {
$categories[] = $input_value;
}
edit. in functions (my PHP is bit rusty, so there might be small tweaks that need to be done but you get the basic idea)
function updateProduct($arr, $input) {
if(!in_array($input, $arr)) {
$arr[] = $input;
}
}
and then call it as
updateProduct($categories, $input_value);
You can use array_push to add items to an array, and you can use in_array to check if it already exists, or you can use array_unique to remove duplicates after your done the rest of your processing too.
Im trying to put multiple posts into one session. Heres the line of code that i use to do it.
$_SESSION['answers'][] = array_push($_SESSION['answers'], $_POST);
As i do so, the following array comes out once i try to add the second array to the session:
Array
(
[0] => Array
(
[checkbox] => Optie 2
[category] => Dieren en natuur
[subcategory] => Wilde dieren
[vraagid] => 116
[type] => 3afbeeldingen
[media] => image
[submit] =>
)
[1] => Array
(
[checkbox] => Optie 1
[category] => Dieren en natuur
[subcategory] => Wilde dieren
[vraagid] => 117
[type] => 3afbeeldingen
[media] => image
[submit] =>
)
[2] => 2
)
I am talking about the last array,
[2] => 2.
This is automattically added. Now when i try to get another array in to the session, it gives me the same issue, it adds the correct array, with checkbox and other vars, but also makes a
[4] => 4
Is this an issue with the array_push function? Because the array that i push is correct, i already checked that.
Either add the value to your array:
$_SESSION['answers'][] = $_POST;
or use array_push
array_push($_SESSION['answers'], $_POST);
You try to do to much at once :)
Won't it work, when you just do:
array_push($_SESSION['answers'], $_POST);
or
$_SESSION['answers'][] = $_POST;
array_push() is basically the same as $array[] = .... array_push() returns the new number of elements in the array, so basically you are adding the new element to your array and then you are adding the amount of elements in the array to the array again (hence the 2).
I have a variable called $gID, I also have a multi dimensional array with sets of group values.
eg....
[data] => Array
(
[0] => Array
(
[userId] => 3
[groupId] => 24
[status] => 1
[timestamp] => 2012-08-01 20:09:36
)
[1] => Array
(
[userId] => 3
[groupId] => 25
[status] => 1
[timestamp] => 2012-08-01 20:08:01
)
)
What I need to do is search the array and return true if the status = 1 if the [groupId] = $gID
What would the most efficient way to do this be? Any ideas? I thought of doing it in two foreach loops but thought there must be a better way forward.
Cheers Chris
I wouldn't worry so much about performance at first. Nonetheless, you can do this with one foreach.
// simple boolean function
function search_stuff($arr, $gID) {
foreach ($arr as $item) {
if ($item['gID'] == $gID && $item['status'] == 1) {
return false;
}
}
return false;
}
// sample call
var_dump(search_stuff($arr['data'], $gID));
I suggest making this a function, for flexibility and ease of maintenance. That way if you find a more performant solution, there's only one place to update.
Using PHP I'm trying to remove an element from an array based on the value of the element.
For example with the following array:
Array
(
[671] => Array
(
[0] => 1
[1] => 100
[2] => 1000
)
[900] => Array
(
[0] => 15
[1] => 88
}
)
I'd like to be able to specify a value of on of the inner arrays to remove. For example if I specified 100 the resulting array would look like:
Array
(
[671] => Array
(
[0] => 1
[2] => 1000
)
[900] => Array
(
[0] => 15
[1] => 88
}
)
My first thought was to loop through the array using foreach and unset the "offending" value when I found it, but that doesn't seem to reference the original array, just the loop variables that were created.
Thanks.
foreach($array as $id => $data){
foreach($data as $index => $offending_val){
if($offending_val === 100){
unset($array[$id][$index]);
}
}
}
You can use:
array_walk($your_array, function(&$sub, $key, $remove_value) {
$sub = array_diff($sub, array($remove_value));
}, 100);
Couple of ideas:
You could try array_filter, passing in a callback function that returns false if the value is the one you want to unset. Using your example above:
$new_inner_array = array_filter($inner_array, $callback_that_returns_false_if_value_100)
If you want to do something more elaborate, you could explore the ArrayIterator class of the SPL, specifically the offsetUnset() method.