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);
Related
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 3 years ago.
Improve this question
I have the following array:
Array (
[ids] => Array (
[0] => e348ae92
[1] => 193ba701
[2] => 58695854
)
[name] => Array (
[0] => Customers
[1] => Suppliers
[2] => Users
)
[subs] => Array (
[0] => 614
[1] => 65
[2] => 99
)
)
I want to take each array key and turn it into a it's own array e.g.
array(
[0] = array(
[0] => e348ae92
[1] => custommers
[2] => 614
)
[1] = array(
[0] => 193ba701
[1] => Suppliers
[2] => 65
)
[2] = array (
[0] => 58695854
[1] => Users
[2] => 99
)
)
I have looked at array_merge, array_combine and a few other things but I have been unsuccessful so far, any pointers in the right direction would be appreciated.
You can use array_map
$f = array_map(null, $a['ids'], $a['name'], $a['subs']);
print_r($f);
Working example:https://3v4l.org/dDG0Y-
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a PHP array like the following one and I would like to combine a multi array to one array:
Array
(
[0] => ADB_DW2017
[1] => LM9
[2] => MS_OF2013
)
Array
(
[0] => NK
[1] => PV
[2] => NK
)
Array
(
[0] => 15
[1] => 25
[2] => 10
)
Array
(
[0] => 250
[1] => 111
[2] => 150
)
Array
(
[0] => 450
[1] => 123
[2] => 250
)
Array
(
[0] => 0
[1] => Mien thue
[2] => 5
)
Array
(
[0] => 200
[1] => 12
[2] => 100
)
Array
(
[0] => 6.750
[1] => 3.075
[2] => 2.500
)
I want to combine like this, with the highest performance :
Array
(
[ADB_DW2017]=>array([suppiler]=>NK
[min_pro]=>15
[max_pro]=>250
[avg_pro]=>450
[tax]=>0
[com]=>200
[sum]=>6.750
)
)
My question is : How to combine a multi array to one array in PHP with the highest performance ?
I assume array names are $first_array,$second_array,...... (so change varabile names accordingly).
Do like below:-
$final_array = array();
foreach ($first_array as $key=> $arr){
$final_array[$arr] =array(
'suppiler'=>(isset($second_array[$key]))? $second_array[$key]: '',
'min_pro'=>(isset($third_array[$key]))? $third_array[$key]: 0,
'max_pro'=>(isset($fourth_array[$key]))? $fourth_array[$key]: 0,
'avg_pro'=>(isset($fifth_array[$key]))? $fifth_array[$key]: 0,
'tax'=>(isset($sixth_array[$key]))? $sixth_array[$key]: '',
'com'=>(isset($seventh_array[$key]))? $seventh_array[$key]: 0,
'sum'=>(isset($eigth_array[$key]))? $eigth_array[$key]: 0
);
}
echo "<pre/>";print_r($final_array);
Output:-https://eval.in/829938
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 6 years ago.
Improve this question
I need array merge with value concat if array title is same name. my array print is->
Array
(
[0] => Array
(
[id] => 7867867
[title] => Title1
)
[1] => Array
(
[id] => 3452342
[title] => Title2
)
[2] => Array
(
[id] => 1231233
[title] => Title2
)
[3] => Array
(
[id] => 5867867
[title] => Title1
)
[4] => Array
(
[id] => 7867777
[title] => Title1
)
)
and i want to this format like if title is same concat the array value in one array and other array is removing.
like that->
Array
(
[0] => Array
(
[id] => 7867867,5867867,7867777
[title] => Title1,Title1,Title1
)
[1] => Array
(
[id] => 3452342,1231233
[title] => Title2,Title2
)
)
If you know that how to solve it please help me!
Thanks
Try this,
foreach($array as $val)
{
$titlearray[] = $val['title'];
}
$titlearray = (array_unique($titlearray));
//print_r($titlearray);
foreach($array as $val)
{
$key = array_search($val['title'], $titlearray);
$newarray[$key]['id'][] = $val['id'];
$newarray[$key]['title'][] = $val['title'];
}
DEMO
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 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);
}