Let's say I have an array like this:
[["code1": '528'], ["code2": '292'], ["code1": '108']]
I am looping through the array to check if a specific key exists. If it doesn't then I am adding it to another array:
$arr[$codename] = $code
However, if the key already exists, then I want to append the $code value to the existing key's values. I'm not sure how to do this part.
I want the new array to look like this:
[["code1": '528', '108'], ["code2", '292']]
You can try:
$array = [['code1' => 528], ['code2' => 292], ['code1' => 108]];
$newArray = [];
foreach ($array as $value) {
$newArray[array_key_first($value)][] = $value[array_key_first($value)];
}
print_r($newArray);
Result:
Array
(
[code1] => Array
(
[0] => 528
[1] => 108
)
[code2] => Array
(
[0] => 292
)
)
Related
I have an array structure like this
Array
(
[0] => Array
(
[C:/xampp/htdocs/rosoka/file] => Array()
)
[1] => Array
(
[C:/xampp/htdocs/rosoka/file/2018-03-02] => Array()
)
[2] => Array
(
[C:/xampp/htdocs/rosoka/file/2018-03-03] => Array()
)
)
how to add some an array on key specific like
[C:/xampp/htdocs/rosoka/file] => Array('duck','buffalo')
1.You can do it like below:-
$array[0]['C:/xampp/htdocs/rosoka/file'] = ['duck','buffalo'];
Output:-https://eval.in/984253
2.Or if some values are already present there:-
$array[0]['C:/xampp/htdocs/rosoka/file'][] = 'buffalo';
Output:-https://eval.in/984254
3.Or if you want to search key first and then try to add:-
foreach($array as $key=>$value){
if(array_keys($value)[0] == 'C:/xampp/htdocs/rosoka/file'){
$array[$key]['C:/xampp/htdocs/rosoka/file'][] = 'buffalo';
}
}
Output:-https://eval.in/984255
For your requirement, you can assign an Array value to key specific like below:
$array[0]['C:/xampp/htdocs/rosoka/file'] = Array('duck','buffalo');
$array[1]['C:/xampp/htdocs/rosoka/file/2018-03-02'] = Array('duck','buffalo');
If you use $array[0]['C:/xampp/htdocs/rosoka/file'][] at left side like this it will again become an Array, hence accessing values will be changed. There is no need of having [] again.
I have a question for you, I need to through an array with other arrays in php but i through only the last array, my array is:
Array
(
[0] => Array
(
[syn_id] => 17070
[syn_label] => fd+dfd
)
[1] => Array
(
[syn_id] => 17068
[syn_label] => fds+dsfds
)
[2] => Array
(
[syn_id] => 17069
[syn_label] => klk+stw
)
)
My php:
$a_ddata = json_decode(method(), true);
foreach ($a_ddata as $a_data)
{
$a_data['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
}
With this code I through only the last array [2], but how to through array?please help me
I need to get the array:
Array
(
[0] => Array
(
[syn_id] => 17070
[syn_label] => fd dfd
)
[1] => Array
(
[syn_id] => 17068
[syn_label] => fds dsfds
)
[2] => Array
(
[syn_id] => 17069
[syn_label] => klk stw
)
)
$a_ddata = json_decode(method(), true); $i=0;
foreach ($a_ddata as $a_data)
{
$a_data_f[$i]['syn_id'] = $a_data['syn_id'];
$a_data_f[$i]['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
$i++;
}
This should be your answer..
When you iterate through something using foreach, by default PHP makes a copy of each element for you to use within the loop. So in your code,
$a_ddata = json_decode(method(), true);
foreach ($a_ddata as $a_data)
{
// $a_data is a separate copy of one of the child arrays in $a_ddata
// this next line will modify the copy
$a_data['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
// but at the end of the loop the copy is discarded and replaced with a new one
}
Fortunately the manual page for foreach gives us a way to override this behavior with the reference operator &. If you place it between the as keyword and your loop variable, you're able to update the source array within your loop.
$a_ddata = json_decode(method(), true);
foreach ($a_ddata as &$a_data)
{
// $a_data is now a reference to one of the elements to $a_ddata
// so, this next line will update $a_ddata's individual records
$a_data['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
}
// and you should now have the result you want in $a_ddata
This should help:
$a_data['syn_label'][] = urldecode(utf8_decode($a_data['syn_label']));
For each iteration you are only replacing $a_data['syn_label']. By adding [] you are making it a multi dimension array which increments for every iteration.
I've got an multi-dimensional array at the moment and want to remove the second-level of arrays and have the value of that second level as the new index value on the parent array. My current array is:
Array ( [0] => Array ( [connectee] => 1 ) [1] => Array ( [connectee] => 6 ) )
And want from that:
Array ( [0] => 1, [1] => 6 )
I was poking around the usort function but couldn't get it to work (where $current_connections is my array as above:
function cmp($a, $b) {
return strcmp($a["connectee"], $b["connectee"]);
}
$current_connections = usort($current_connections, "cmp");
The key doesn't need to be maintained (should be destroyed in the process).
foreach ($array as &$value) {
$value = $value['connectee'];
}
Note: Please note that the question statement is very confusing and contradicting, but this answer is based upon your statement for expected output
Array ( [0] => 1, [1] => 6 )
You could do
<?php
$values=array();
$values[0]=array("connectee"=>1);
$values[1]=array("connectee"=>6);
foreach($values as $index=>$value)
{
$values[$index]=$value["connectee"];
}
print_r($values);
?>
This is my code:
foreach ($all_orders as $order){//the $all_orders array contains a number of arrays, so it's a multidimensional array
$order["Order Rank"]=$order[0];
unset($order[0]);
}
after renaming the key with the new key and when i print the array:
print_r($all_orders);
i got the old key name (which is 0):
Array
(
[0] => Array
(
[0] => 1
)
why it's not :
Array
(
[0] => Array
(
["Order Rank"] => 1
)
am i missing something? thanx in advance.
You're modifying a copy of the element.
Use references:
foreach ($all_orders as &$order) {
//...
}
You are working with the $order variable, which is not the same as the array. You want to:
foreach ($all_orders as $key => $order){//the $all_orders array contains a number of arrays, so it's a multidimensional array
$all_orders[$key]["Order Rank"]=$order[0];
unset($all_orders[$key]);
}
I'm new to working with arrays so I need some help. With getting just one vaule from an array. I have an original array that looks like this:
$array1= Array(
[0] => 1_31
[1] => 1_65
[2] => 29_885...)
What I'm trying to do is seach for and return just the value after the underscore. I've figured out how to get that data into a second array and return the vaules as a new array.
foreach($array1 as $key => $value){
$id = explode('_',$value);
}
which gives me:
Array ( [0] => 1 [1] => 31 )
Array ( [0] => 1 [1] => 65 )
Array ( [0] => 29 [1] => 885 )
I can also get a list of the id's or part after the underscore by using $id[1] I'm just not sure if this is the best way and if it is how to do a search. I've tried using in_array() but that searches the whole array and I couldn't make it just search one key of the array.
Any help would be great.
If the part after underscore is unique, make it a key for new array:
$newArray = array();
foreach($array1 as $key => $value){
list($v,$k) = explode('_',$value);
$newArray[$k] = $v;
}
So you can check for key existence with isset($newArray[$mykey]), which will be more efficient.
You can use preg_grep() to grep an array:
$array1= array("1_31", "1_65", "29_885");
$num = 65;
print_r(preg_grep("/^\d+_$num$/", $array1));
Outputs:
Array
(
[1] => 1_65
)
See http://ideone.com/3Fgr8
I would say you're doing it just about as well as anyone else would.
EDIT
Alternate method:
$array1 = array_map(create_function('$a','$_ = explode("_",$a); return $_[1];'),$array1);
echo in_array(3,$array1) ? "yes" : "no"; // 3 being the example
I would have to agree. If you wish to see is a value exists in an array however just use the 'array_key_exists' function, if it returns true use the value for whatever.