How can get array key value from object using php [closed] - php

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);
}

Related

How to process the multi dimensional array into specific array? [closed]

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);

Restructure array data and fill in empty value based on the existing data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Data
Array
(
[0] => Array
(
[member_id] => 1
[total] => 0
[numrow] => 1
)
[1] => Array
(
[member_id] => 16
[total] => 0
[numrow] => //should fill in 3
)
[2] => Array
(
[member_id] => 5
[total] => 3
[numrow] => 0
)
[3] => Array
(
[member_id] => 6
[total] => 5
[numrow] => //should fill in 4
)
[4] => Array
(
[member_id] => 92
[total] => 15
[numrow] => 2
)
)
Sorry for asking stupid questions. Currently, I have stuck in this logic, I want to fill in the empty data value inside the array, it will loop all the data and get the current numrow data and then +1 and fill into the empty value. Any expert can help with this ya?
Following logic might help you on your way - the source array is called $arr. You can get the max for column numrow using max(array_column()):
$max = max(array_column($arr, 'numrow'));
foreach($arr as $key0 => &$value0) {
foreach($value0 as $key1 => &$value1) {
if($key1 === 'numrow' && $value1 === '') {
$value1 = ++$max;
}
}
}
working demo

Create an array where key is unique and duplicate key's value will be sum of keys [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
Improve this question
I have created an array:
Array
(
[0] => Array
(
[product_price] => 88.0000
[category_id] => 13
)
[1] => Array
(
[product_price] => 180.0000
[category_id] => 13
)
[2] => Array
(
[product_price] => 180.0000
[category_id] => 13
)
[3] => Array
(
[product_price] => 150.0000
[category_id] => 13
)
[4] => Array
(
[product_price] => 200.0000
[category_id] => 14
)
)
The above array will be create only tow index(0 and 1). Here I want to remove remove duplicate arrays category_id wise and will add all duplicate value's product_price will be add.I want to like as :
Array
(
[0] => Array
(
[product_price] => 598.0000
[category_id] => 13
)
[1] => Array
(
[product_price] => 200.0000
[category_id] => 14
)
)
try this code:
Here $cart is your array name
$res_arr = array();
foreach($cart as $p)
{
$found = false;
foreach($res_arr as $k => $r)
{
if($r["category_id"]==$p["category_id"])
{
$res_arr[$k]["product_price"] += $p["product_price"];
$found = true;
}
}
if(! $found)
$res_arr[] = $p;
}
print_r($res_arr);
Create a map with category_id as key. Define custom insert method for map which checks that if key already exists then add product_price to old one
Here is the code-
<?php
$arr=array(
0 => array
(
'product_price' => 88.0000,
'category_id' => 13
),
1 => array
(
'product_price' => 180.0000,
'category_id' => 13
),
2 => array
(
'product_price' => 180.0000,
'category_id' => 13
),
3 => array
(
'product_price' => 150.0000,
'category_id' => 13
),
4 => array
(
'product_price' => 200.0000,
'category_id' => 14
)
);
//logic here
$cat_arr=array();
$reduced_arr=array();
//Fetching unique category_id.
foreach($arr as $sub_arr)
{
if(!in_array($sub_arr['category_id'], $cat_arr))
$cat_arr[]=$sub_arr['category_id'];
}
//Getting the final result
foreach($cat_arr as $cat)
{
$price=0;
foreach($arr as $prod)
{
if($prod['category_id']==$cat)
$price+=$prod['product_price'];
}
$reduced_arr[]= array('product_price' => $price, 'category_id' => $cat);
}
print_r($reduced_arr);
?>
O/P
Array ( [0] => Array ( [product_price] => 598 [category_id] => 13 ) [1] => Array ( [product_price] => 200 [category_id] => 14 ) )

PHP count array by unique value [closed]

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";
}

merging mutlidimensional array together to have a continous key [closed]

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 ?

Categories