Here's the array structure (only the first element in the array):
Array
(
[1] => Array
(
[pageid] => 1
[step_order] => 1
[pageurl] => http://www.domain.com/
[in_links] => Array
(
[domains] => Array
(
[Direct Entry] => 1520
[www.google.com] => 387
[www.google.co.in] => 14
[search.yahoo.com] => 10
[All other] => 27
)
[impressions] => Array
(
[Direct Entry] => Array
(
[0] => 10654
[1] => 10728
[2] => 10772
)
[www.google.com] => Array
(
[0] => 10991
[1] => 12455
[2] => 12466
[3] => 10757
)
[www.google.co.in] => Array
(
[0] => 9839
[1] => 9837
[2] => 9845
)
[search.yahoo.com] => Array
(
[0] => 12087
[1] => 10864
)
)
)
[out_links] => Array
(
[domain] => Array
(
[Left site] => 1752
[http://www.domain.com/#] => 102
[http://www.domain.com/contact] => 102
[http://www.domain.com/#basic_inline_div] => 2
)
[impressions] => Array
(
[Left site] => Array
(
[0] => 7680
[1] => 9728
[2] => 10496
)
[http://www.domain.com/#] => Array
(
[0] => 259
[1] => 11013
)
[http://www.domain.com/contact] => Array
(
[0] => 12802
[1] => 10757
)
[http://www.domain.com/#basic_inline_div] => Array
(
[0] => 11
[1] => 51
)
)
)
[visitors] => 1958
)
)
I'm trying to loop to get the elements from domains, impressions (and sub elements). I managed to get the first parts: pageid, step_order, page_url. I'm having trouble with in_links and out_links and their child arrays. Anyone have ideas on how to pull that data?
Here's how you get the domains from in_links. The others are similar (but I'm not sure what the indexes in the impressions sub-array represent).
foreach ($array as $element) {
foreach ($element['in_links']['domains'] as $domain => $count) {
echo "Domain: $domain, Count: $count\n";
}
}
Related
Could you please help me? I need to know how to access the values of this response from an API call using PHP.
Array ( [queryIds] => Array ( [0] => lHLLapn7EH ) [timedOutRealtimeData] => [realtimeDataMissing] => [cacheFreshness] => FRESH [prunedResult] => [novaCost] => 0 [novaRequestDuration] => 19 [wasCached] => 1 [minSampleRate] => 1 [timeComputed] => 1558794556710 [novaRuntime] => 199 [hitChunkGroupByLimit] => [throttleTime] => 0 [data] => Array ( [series] => Array ( [0] => Array ( [0] => Array ( [setId] => [value] => 843 ) ) [1] => Array ( [0] => Array ( [setId] => [value] => 22 ) ) ) [seriesLabels] => Array ( [0] => Array ( [0] => 0 [1] => C5241; GB ) [1] => Array ( [0] => 0 [1] => C9999; GB ) ) [seriesMeta] => Array ( [0] => Array ( [eventGroupBys] => Array ( [0] => C5241 [1] => GB ) [segmentIndex] => 0 [eventIndex] => 0 [eventGroupBy] => C5241; GB ) [1] => Array ( [eventGroupBys] => Array ( [0] => C9999 [1] => GB ) [segmentIndex] => 0 [eventIndex] => 0 [eventGroupBy] => C9999; GB ) ) [seriesCollapsed] => Array ( [0] => Array ( [0] => Array ( [setId] => [value] => 843 ) ) [1] => Array ( [0] => Array ( [setId] => [value] => 22 ) ) ) [xValues] => Array ( [0] => 2019-05-25 ) ) [partialMergedAndNewUserInformation] => [transformationIds] => Array ( [0] => 2696 ) [backend] => novaV2 )
You can access them using
Array ( [0] => C9999 [1] => GB ) -> $arr['data']['seriesMeta'][0]['eventGroupBys']
Array ( [setId] => [value] => 20) -> $arr['data']['seriesMeta'][0][0]
This is the array which i get from post
Array
(
[data] => Array
(
[Invoice] => Array
(
[itemNo] => Array
(
[0] => rtgrg
[1] => 4t4t
[2] => ththt
[3] => thth
)
[itemName] => Array
(
[0] => rtgrt
[1] => 4t5t5
[2] => hthtyh
[3] => gnghnn
)
[itemDiscription] => Array
(
[0] => 5tr5t
[1] => t45t4
[2] => tyhthtyh
[3] => gnghnh
)
[price] => Array
(
[0] => 2
[1] => 10
[2] => 9
[3] => 12
)
[itemQuantity] => Array
(
[0] => 2
[1] => 12
[2] => 9
[3] => 9
)
[itemDiscount] => Array
(
[0] => 11
[1] => 0.14
[2] => 0.13
[3] => 0.1
)
[itemTotal] => Array
(
[0] => 333333
[1] => 34535
[2] => 55555555555555
[3] => 666666666
)
[itemStartDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] => 2016-06-17
)
[itemEndDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[itemCustomcol] => Array
(
[0] => 4t4t4
[1] => t5trgr
[2] => htht
[3] => gngh
)
[itemCustomcol2] => Array
(
[0] => t4t4t
[1] => rtgtr
[2] => thth
[3] => gng
)
[itemttax2] => Array
(
[0] => tax1
)
[itemttax3] => Array
(
[0] => tax1
)
[itemttax4] => Array
(
[0] => tax1
)
[itemtCsTax] => Array
(
[0] => gngn
)
[itemtCsTaxPer] => Array
(
[0] => 0.1
)
[itemtDiscount] => Array
(
[0] => 0.18
)
[itemtInvReceived] => Array
(
[0] => gngn
)
[itemtInvToReturn] => Array
(
[0] => gngng
)
[itemInvDue] => Array
(
[0] => nghnghng
)
[itemInvComment] => Array
(
[0] => hngnhg
)
)
)
There is a separate table for dynamic input like itemName, itemDiscription to itemCustomcol2 and another table for itemttax2 to itemInvComment .
I tried foreach loop for multiple element array but this doent work for me as it returns a string. one of them is shown below
$capture_field_vals ="";
foreach($_POST["data"]["Invoice"]["itemNo"] as $key => $text_field){
$capture_field_vals .= $text_field .", ";
}
echo $capture_field_vals;
What i want is collect all element[0] and then insert it in a row mysql and then another element another row and so on.
Is there any reason you can't use the following format?
Array
(
[data] => Array
(
[Invoice] => Array
(
[0] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),
[1] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),...
with this method you can loop through retrieving all details of each item in the invoice?
If I have misunderstood your problem, I apologise
I have two array $array1 and $array2 which I get dynamically and look like
$array1 = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 2
[cnt] => 5
)
[1] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
$array2 = Array
(
[0] => Array
(
[id] => 1
[name] => Phone Calls
[readable] => 1
[status] => active
)
[1] => Array
(
[id] => 2
[name] => Meeting With Customer
[readable] => 1
[status] => active
)
[2] => Array
(
[id] => 3
[name] => Others Works
[readable] => 1
[status] => active
)
);
which i need to compare.
if $array2['id'] is not in $array1["activity"](i.e"activity_id") add array ['activity_id'=>$array2['id'],'cnt'=>0] to $array1['activity'].
My result must be like
$result = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 0
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 5
)
[2] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
What i have tried is
$finalArray = array();
foreach($array1 as $arr1) {
foreach($array2 as $arr2) {
if(!in_array($arr2['id'], $arr1['activity'])) {
$array = ['activity_id'=>$arr2['id'], 'cnt'=>0];
}
array_push($arr1['activity'], $array);
unset($array);
}
array_push($finalArray, $result);
}
print_r($finalArray);
in_array() function is not working as I excepted or I am trying to do it in the wrong way. Can someone helps me with this?
Sorry,finally i get what i did wrong.May be someone get helped.
everything is ok just change the line
if(!in_array($arr2['id'], $arr1['activity'])) {
into
if(!in_array( $readActivity['id'], array_column($result['activity'],'activity_id'))){
I need to merge a PHP array, this array has 2 arrays into it named "targetXX", I can have 2 or more. Each target have the same keys, for each key I have an array with 2 values a and b, a is always the same in both targets, but I need to merge both B values like this:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
)
[2] => Array
(
[0] => 600
[1] => 1392282320
)
[3] => Array
(
[0] => 200
[1] => 1392282380
)
[4] => Array
(
[0] => 400
[1] => 1392282440
)
[5] => Array
(
[0] => 600
[1] => 1392282500
)
)
)
[1] => Array
(
[target] => hitcount(stats.asdf.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 4321
[1] => 1392282200
)
[1] => Array
(
[0] => 76567
[1] => 1392282260
)
[2] => Array
(
[0] => 5556
[1] => 1392282320
)
[3] => Array
(
[0] => 7675
[1] => 1392282380
)
[4] => Array
(
[0] => 2344
[1] => 1392282440
)
[5] => Array
(
[0] => 0999
[1] => 1392282500
)
)
)
Result:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
[2] => 4321
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
[2] => 76567
)
[2] => Array
(
[0] => 600
[1] => 1392282320
[2] => 5556
)
[3] => Array
(
[0] => 200
[1] => 1392282380
[2] => 7675
)
[4] => Array
(
[0] => 400
[1] => 1392282440
[2] => 2344
)
[5] => Array
(
[0] => 600
[1] => 1392282500
[2] => 0999
)
)
)
Use array_merge() to achieve this:
$newArray = array();
foreach ($myArray['target2'] as $key => $innerArr1) {
$newArray['target'][$key] = array_merge(
$myArray['target1'][$key], /* 0th and 1st index */
array($innerArr1[1]) /* 2nd index */
);
}
print_r($newArray);
Output:
Array
(
[target] => Array
(
[0] => Array
(
[0] => 333333
[1] => 13
[2] => 99
)
[1] => Array
(
[0] => 444444
[1] => 15
[2] => 98
)
[2] => Array
(
[0] => 555555
[1] => 17
[2] => 97
)
)
)
Demo
The built-in function array_merge may do the work for you. You need to merge each subarrays in fact, as the array_merge_recursive function doesn't handle indexes.
$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
$newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If you have more than 2 keys to merge, you can loop on the algorithm multiple times.
I have two arrays that have common indexes (church and office). I need to "merge" the total of the first array into the second array to get the desired output (seen below the double line). I'm not sure how to do this with array_merge(). Any help would be greatly appreciated!
Array
(
[church] => Array
(
[total] => 77
)
[office] => Array
(
[total] => 202
)
)
Array
(
[church] => Array
(
[name] => Array
(
[0] => Bill
[1] => Sally
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 25
[1] => 50
)
)
[office] => Array
(
[name] => Array
(
[0] => Marta
[1] => Ruth
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 100
[1] => 100
)
)
)
====================================================
Array
(
[church] => Array
(
[total] => 77
[name] => Array
(
[0] => Bill
[1] => Sally
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 25
[1] => 50
)
)
[office] => Array
(
[total] => 202
[name] => Array
(
[0] => Marta
[1] => Ruth
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 100
[1] => 100
)
)
)
Try something like this:
$a = array('church' => array('total' => 5), 'office' => array('total' => 10));
$b = array('church' => array('name' => 'church'), 'office' => array('name' => 'office'));
foreach ( $b as $key => $value ) {
$b[$key] = array_merge($a[$key], $b[$key]);
}