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:
Array
(
[0] => Array
(
[id] => 31299
[name] => 37322426212
[ips] =>
[tech_prefix] =>
[password] =>
[id_voip_hosts] =>
[proxy_mode] =>
[auth_type] => ani
[ani] => 37322426212
[accname] =>
[protocol] =>
[port] =>
[orig_enabled] => 1
[term_enabled] =>
[orig_capacity] =>
[term_capacity] =>
[orig_rate_table] => 7
[term_rate_table] =>
[id_dr_plans] =>
[orig_groups] =>
[term_groups] =>
[notes] =>
)
[1] => Array
(
[id] => 4373
[name] => 37322983029
[ips] =>
[tech_prefix] =>
[password] =>
[id_voip_hosts] =>
[proxy_mode] =>
[auth_type] => ani
[ani] => 37322983029
[accname] =>
[protocol] =>
[port] =>
[orig_enabled] => 1
[term_enabled] =>
[orig_capacity] =>
[term_capacity] =>
[orig_rate_table] => 7
[term_rate_table] =>
[id_dr_plans] =>
[orig_groups] =>
[term_groups] =>
[notes] =>
)
[2] => Array
(
[auth_type] => ani
[name] => 37322983029
[ani] => 37322983029
[orig_enabled] => Array
(
[0] => on
)
[orig_rate_table] => 7
)
)
Items with 0,1,2 may be more(3,4...10...and so on).
What I am trying to do is to find array with key ani and name = 37322983029 and replace
name with '###' and ani to empty:
[mane]=>"###",//where name = '37322983029'
[ani]=> //where ani = '37322983029'
I tried with str_replace but no success.
How can this be done?
I think, this is what you are looking for:
foreach($yourArray as &$a)
{
if($a["ani"] == 37322983029 and $a["ani"] == $a["name"])
{
$a["name"] = "###";
$a["ani"] = "";
}
}
Not sure how you were using str_replace, but the following should work (assuming that ani and mane (or name?) are actually string indices into your array - not obvious from your example):
foreach($myArray as &$val) {
if($val['ani'] == 37322983029) {
$val['ani'] = '';
$val['name'] = "###";
}
}
important Note the use of & to pass the value by reference so you are working with actual elements of the array, not copies... In that way, when you modify $val you are actually modifying the original array. Without the ampersand, your initial array would be unchanged by the loop.
Also note - it is not clear from your example whether you want to test for just ani==37322983029 or also for name=37322983029. I am sure you can modify the code above to have both conditions (or see Alexxus's answer)
A complete code example (tested, working):
<?php
$a = Array(Array('a'=>4, 'b'=>5),Array('a'=>6, 'b'=>7));
echo '<pre>';
print_r($a);
foreach($a as &$v){
if($v['a']==6) {
$v['a'] = 12;
$v['b'] = '';
}
}
print_r($a);
echo '</pre>';
?>
Produces output
Array
(
[0] => Array
(
[a] => 4
[b] => 5
)
[1] => Array
(
[a] => 6
[b] => 7
)
)
Array
(
[0] => Array
(
[a] => 4
[b] => 5
)
[1] => Array
(
[a] => 12
[b] =>
)
)
Related
This question already has answers here:
PHP append one array to another (not array_push or +)
(11 answers)
Closed 3 years ago.
I have two array, i want to convert it with one array with whole data.
My array format is here
$fromData=array('id' => '004','shapeid' => 'circle','x' =>'360','y' => '560', 'tooltext' => 'vivek','labelpos' => 'bottom');
$ToData=array('id' => '005','shapeid' => 'triangle','x' =>'480','y' => '980', 'tooltext' => 'kimi','labelpos' => 'top');
I wanna got whole data in one array.
Thanks
you can use array_merge_recursive
$fromData=array('id' => '004','shapeid' => 'circle','x' =>'360','y' => '560', 'tooltext' => 'vivek','labelpos' => 'bottom');
$ToData=array('id' => '005','shapeid' => 'triangle','x' =>'480','y' => '980', 'tooltext' => 'kimi','labelpos' => 'top');
$newdata= array_merge_recursive($fromData,$ToData);
output will be
Array
(
[id] => Array
(
[0] => 004
[1] => 005
)
[shapeid] => Array
(
[0] => circle
[1] => triangle
)
[x] => Array
(
[0] => 360
[1] => 480
)
[y] => Array
(
[0] => 560
[1] => 980
)
[tooltext] => Array
(
[0] => vivek
[1] => kimi
)
[labelpos] => Array
(
[0] => bottom
[1] => top
)
)
This can be done based on what #Ajith is suggesting in above comment
$result[] = $fromData;
$result[] = $ToData;
You can check this implementation here, https://3v4l.org/0QR56
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I have an php array
Array
(
[results] => Array
(
[0] => Array
(
[birthday] => 11-Apr-2014
[category] => Array
(
[0] => 204
[1] => 300
[2] => 304
)
[city] => Dhaka
[country] => Bangladesh
[email] => javaorjava#gmail.com
[fullName] => biplob
[gender] => Male
[inspirational] => Run to dream
[phone] => aapbd1
[photo] => Array
(
[__type] => File
[name] => 8bef9bc3-ee64-45df-9698-0466e255c1bd-profilePhoto.jpg
[url] => http://files.com/c2dcf728-e2a3-4b2d-a8c8-6ec9b3c6502a/8bef9bc3-ee64-45df-9698-0466e255c1bd-profilePhoto.jpg
)
[username] => aapbd
[website] => http://ss.com
[createdAt] => 2014-04-10T19:01:16.396Z
[updatedAt] => 2014-04-28T07:36:18.459Z
[objectId] => IQSCdXE2hI
)
[1] => Array
(
[birthday] => 09-Apr-1982
[category] => Array
(
[0] => 204
[1] => 307
[2] => 311
[3] => 313
[4] => 102
[5] => 103
[6] => 105
[7] => 107
)
[city] => Madrid
[country] => Spain
[coverPhoto] => Array
(
[__type] => File
[name] => aa53cf65-47af-464d-aa49-88202f91388f-coverPhoto.jpg
[url] => http://files.com/c2dcf728-e2a3-4b2d-a8c8-6ec9b3c6502a/aa53cf65-47af-464d-aa49-88202f91388f-coverPhoto.jpg
)
[description] => a lazy man
[email] => skio#yahoo.com
[fullName] => Sjun
[gender] => Male
[inspirational] => Honesty is the best policy
[phone] => 135469
[photo] => Array
(
[__type] => File
[name] => a1aec283-f3c7-484c-a8b2-a0b09c5f3023-profilePhoto.jpg
[url] => http://files.com/c2dcf728-e2a3-4b2d-a8c8-6ec9b3c6502a/a1aec283-f3c7-484c-a8b2-a0b09c5f3023-profilePhoto.jpg
)
[username] => asa
[website] =>
[createdAt] => 2014-04-09T07:58:19.043Z
[updatedAt] => 2014-05-07T11:13:40.671Z
[objectId] => iVb6olefaT
)
)
)
I'm trying to practice/learn foreach loops in PHP. I understand basic foreach. But I struggle with multi-dimensionals.
I want to get my every array birthday,photo name,photo url,category.
But I cant retrieve those correctly with foreach loop
Use a simple foreach() for iterating over MD arrays..
foreach($yourarray['results'] as $k=>$arr)
{
echo $arr['birthday'];
echo $arr['photo']['name'];
echo $arr['photo']['url'];
}
Let's say your array is $data,
foreach ($data['results'] as $key => $value)
echo $value['birthday'];
echo $value['photo']['name'];
echo $value['photo']['url'];
}
use $key as index value.
This is how you can access your nested array, regardless the amount of categories:
// Loop over all elements of main array element
foreach($array['results'] as $arr_key => $arr_value) {
// Loop over children
foreach($arr_value as $key => $value) {
// Element is array (like 'category' or 'photo')
if(is_array($value)) {
foreach($value as $sub_key => $sub_value) {
echo $key."[".$sub_key."] = ".$sub_value;
}
// Element is no array
} else {
echo $key." = ".$value;
}
}
}
(Example)
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 8 years ago.
Improve this question
I have array
Array
(
[0] => Array
(
[question_summary] =>
[answer1] => 18
[answer2] => 3
[PercentEVQ] => 10.8000
[PercentEVQ2] => 11
[driver_display_name] => Position
[cluster_name] => Personal Impact
)
[1] => Array
(
[question_summary] =>
[answer1] => 51
[answer2] => 3
[PercentEVQ] => 30.6000
[PercentEVQ2] => 31
[driver_display_name] => Position
[cluster_name] => Personal Impact
)
)
how to create new array become
Array
(
[Personal Impact] => Array
(
[Position] => array
(
[0] => array
(
[question_summary] =>
[answer1] => 18
[answer2] => 3
[PercentEVQ] => 10.8000
[PercentEVQ2] => 11
[driver_display_name] => Position
[driver_name] => Position
[cluster_name] => Personal Impact
)
[1] => Array
(
[question_summary] =>
[answer1] => 51
[answer2] => 3
[PercentEVQ] => 30.6000
[PercentEVQ2] => 31
[driver_display_name] => Position
[driver_name] => Position
[cluster_name] => Personal Impact
)
)
)
)
Is it possible?
Yes it's possible you can just foreach(your values as key => value) {} it or something like this:
Sample Fiddle
<?php
$new_values = array();
foreach($old_values as $key => $values) {
$new_values[$values['cluster_name']][$values['driver_display_name']][] = array(
'answer1' => $values['answer1'],
'answer2' => $values['answer2'],
'PercentEVQ' => $values['PercentEVQ'],
'PercentEVQ2' => $values['PercentEVQ2'],
'driver_display_name' => $values['driver_display_name'],
'cluster_name' => $values['cluster_name'],
);
}
Are you defining a new array?
$new_array['Personal Impact']['Position'] = $old_array;
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 ) )
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a array containing other arrays(sub arrays). I need to get index of subarray that consist specific value of a key. For example. Here is my array:
Array
(
[0] => Array
(
[id] => 353
[name] => account_2
[ips] =>
[tech_prefix] =>
[password] =>
[id_voip_hosts] =>
[proxy_mode] =>
[auth_type] => ani
[ani] => 526466
[accname] =>
[protocol] =>
[port] =>
[orig_enabled] => 1
[term_enabled] =>
[orig_capacity] =>
[term_capacity] =>
[orig_rate_table] =>
[term_rate_table] =>
[id_dr_plans] =>
[orig_groups] =>
[term_groups] =>
[notes] =>
)
[1] => Array
(
[id] => 352
[name] => account_3
[ips] =>
[tech_prefix] =>
[password] =>
[id_voip_hosts] =>
[proxy_mode] =>
[auth_type] => ani
[ani] => 1345436
[accname] =>
[protocol] =>
[port] =>
[orig_enabled] => 1
[term_enabled] =>
[orig_capacity] =>
[term_capacity] =>
[orig_rate_table] =>
[term_rate_table] =>
[id_dr_plans] =>
[orig_groups] =>
[term_groups] =>
[notes] =>
)
[2] => Array
(
[id] => 354
[name] => account_4
[ips] =>
[tech_prefix] =>
[password] =>
[id_voip_hosts] =>
[proxy_mode] =>
[auth_type] => ani
[ani] => 472367427
[accname] =>
[protocol] =>
[port] =>
[orig_enabled] => 1
[term_enabled] =>
[orig_capacity] =>
[term_capacity] =>
[orig_rate_table] =>
[term_rate_table] =>
[id_dr_plans] =>
[orig_groups] =>
[term_groups] =>
[notes] =>
)
)
What I need. For example I need to delete from the array subarray [2]. I know a way with unset($myarray[2]) but how can I get this index [2]. If I know only [ani] key value 472367427. How to get this "[2]" in var to insert it in unset command.
If I would need to delete subarray that have a key [ani] that is = 1345436(it is in array [1]). Is there a way to search index of array by value of a key.
Again, how to find this index [2] or [1] by [ani] key in multidimensional array?
Thanks!
I think this should work (not tested - but you get the idea)
foreach ($arrays as $key => $item)
{
if ($item['ani'] === '472367427')
{
unset($arrays[$key]);
}
}
There are several ways of going about this, but array_filter is probably the most scalable. You will need to create a callback function which searches for values you want to get rid of and then use it as the filter for the array:
function filterCallback($value) {
if($value['ani'] == "472367427") {
return false;
} else {
return true;
}
}
$array = array_filter($array,'filterCallback');
The advantage to this is that you can abstract your filtering logic (and make it more complex) without having to do it within a foreach loop.
See http://www.php.net/manual/en/function.array-filter.php
$remove = 472367427;
foreach($your_array as $key => $values) {
if(!empty($values['ani'] && $values['ani'] == $remove) {
unset($your_array[$key]);
}
}
If you already know the ani value you are looking for:
$yourani = 'Your known ani value';
foreach($myarray AS $array){
if($array['ani'] == $yourani){
unset($array);
}
}
You can use array_search to get the index and unset the element like this
$a = array('one', 'two', 'three', 472367427, 'four', 'five');
unset($a[array_search(472367427, $a)]);