Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've got an array like the one below, and I would like to count how many times an agentName occurs.
Basically I want to find out how many times hello#url.com and BYE#url.com are occurring. Awnser for the below should be hello#url.com = 3 and BYE#url.com = 2
Array
(
[0] => stdClass Object
(
[text_date] => 2013-12-04 19:27:29
[name] => hello#url.com
)
[1] => stdClass Object
(
[text_date] => 2013-12-07 19:18:32
[name] => hello#url.com
)
[2] => stdClass Object
(
[text_date] => 2013-12-08 09:59:30
[name] => hello#url.com
)
[3] => stdClass Object
(
[text_date] => 2013-12-04 12:23:24
[name] => BYE#url.com
)
[4] => stdClass Object
(
[text_date] => 2013-12-04 13:10:18
[name] => BYE#url.com
)
)
Any ideas if this is possible?
If you are using PHP 5.5, you could do like this
$new_array = array_count_values(array_values($yourarray,'name'));
foreach($new_array as $k=>$v)
{
echo "$k occurred $v times\n";
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
I want the array to be processed into my final array how am I supposed to achieve this? Below is the array that I have which I got from the loop.
Array
(
Array
(
[part_id] => 2338117
[supplier] => COOLDRIVE DISTRIBUTION
[quantity] => 12
)
Array
(
[part_id] => 2338117
[supplier] => ROLAN
[quantity] => 20
)
Array
(
[part_id] => 51154
[supplier] => ROLAN
[quantity] => 20
)
)
into the final array.
Array
(
[COOLDRIVE DISTRIBUTION] => Array
(
[proudctID] => Array
(
[0] => 2338117
)
)
[ROLAN] => Array
(
[productID] => Array
(
[0] => 2338117
[1] => 51154
)
)
)
You can use something like this in your case. Considering your old array as $oldarr
$newarr = array();
foreach($oldarr as $value){
$newarr[$value['supplier']]['productID'][] = $value['part_id'];
}
print_r($newarr);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a shipping API which produces a variable called $quotereply.
print_r ($quotereply) gives me the following output:
Array ( [replycode] => 200 [replymessage] => success [replytype] => quote [quote] => Array ( [services] => Array ( [noofservices] => 2 [service1] => Array ( [name] => TestService1Before12am [description] => Test Service 1 Before 12am [carrier] => Camel [price] => 10 [vat] => 0 [vat_rate] => 0 [insurance_cost] => 0 [insurance_cover] => 0 ) [service2] => Array ( [name] => TestService2Anytime [description] => Test Service 2 Anytime [carrier] => Pigeon [price] => 5 [vat] => 0 [vat_rate] => 0 [insurance_cost] => 0 [insurance_cover] => 0 ) ) ) [custom] => Array ( [data] => [orderid] => ) )
My question is, how can I extract a value such as [noofservices] from this? I can't really get my head round what I'm looking at, is it an array within an array?
Thanks in advance!
Just try with:
$quotereply['quote']['services']['noofservices']
it is multidimensional array so you should follow array index keys]
echo $quotereply["quote"]["services"]["noofservices"];
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i want to merge this two arrays together
Array
(
[0] => Array
(
[type] => Person
[relevance] => 0.700000
[count] => 300
[text] => Chris
)
)
Array
(
[0] => Array
(
[type] => Person
[relevance] => 0.900000
[count] => 400
[text] => Chris
)
[1] => Array
(
[type] => Person
[relevance] => 0.500000
[count] => 200
[text] => Tom
)
)
so i might have a result like this
Array
(
[0] => Array
(
[type] => Person
[relevance] => 0.900000
[count] => 400
[text] => Chris
)
[1] => Array
(
[type] => Person
[relevance] => 0.500000
[count] => 200
[text] => Tom
)
[2] => Array
(
[type] => Person
[relevance] => 0.700000
[count] => 300
[text] => taye
)
)
Like this?
$array_final = array_merge($array_1, $array_2);
refer array_merge at official documentation site
Try using foreach
// $array1 is your original array
foreach($array2 as $val) {
array_push($array2,$val)
}
Use array_merge docs and is faster to check documentation:D
Did you search the PHP Doc ?
what about array_merge_recursive ?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this cart object array
Array
(
[16] => Array
(
[count] => 1
[data] => CartItem Object
(
[_itemID] => 16
[_itemData] =>
)
)
[14] => Array
(
[count] => 1
[data] => CartItem Object
(
[_itemID] => 14
[_itemData] =>
)
)
[18] => Array
(
[count] => 1
[data] => CartItem Object
(
[_itemID] => 18
[_itemData] =>
)
)
[15] => Array
(
[count] => 1
[data] => CartItem Object
(
[_itemID] => 15
[_itemData] =>
)
)
)
From this array I want to get these key values 16, 14, 18, 15.
How can I get this ?
array_keys will give the keys of a particular array:
$keys = array_keys($yourArray);
print_r($keys);
You can use the array_keys function.
http://php.net/array_keys
Also you could do that in a foreach loop
foreach($array as $key=>$nextArray){
//Process
}
To return all the keys of the array use (http://php.net/manual/en/function.array-keys.php):
array_keys($array);
Check out the php foreach and array_push documentation that should do what you want.
$numbers = new array();
foreach ($cart as $key => $value) {
array_push($numbers, $key);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array I returned via json_decode and want to extract only the image_url below. What php commands would I run from this, to get the image_url below?
[0] =>
stdClass Object (
[id] => 36210508
[name] => Untitled
[description] =>
[times_viewed] => 2
[rating] => 0
[created_at] => 2013-05-31T15:08:36-04:00
[category] => 0
[privacy] =>
[width] => 275
[height] => 183
[votes_count] => 0
[favorites_count] => 0
[comments_count] => 0
[nsfw] =>
[license_type] => 0
[image_url] => http://pcdn.500px.net/36210508/5de3b4bb9cdee9f429e2a329a1d0619dd5b28ce1/4.jpg
[images] =>
Array (
[0] =>
stdClass Object (
[size] => 4
[url] => http://pcdn.500px.net/36210508/5de3b4bb9cdee9f429e2a329a1d0619dd5b28ce1/4.jpg
)
)
[store_download] =>
[store_print] =>
[user] =>
stdClass Object (
[id] => 3677956
[username] => jsniff12
[firstname] => Jacob
[lastname] => Sniff
[city] =>
[country] =>
[fullname] => Jacob Sniff
[userpic_url] => /graphics/userpic.png
[upgrade_status] => 0
[followers_count] => 0
[affection] => 0
)
)
)
Please try to make some effort before posting questions here. You can read about php arrays and objects.
You will be able to get image_url the following way:
echo $array[0]->image_url;
Note: this kind of question is very likely to be closed since it's too specific and too basic.