$original array [['type_of_activity'=>'م.ص','total'=>'0' ],['type_of_activity'=>'م.و','total'=>'0'],['type_of_activity'=>'م.ن','total'=>'0'],['type_of_activity'=>'م.خ','total'=>'0'],['type_of_activity'=>'م.ت','total'=>'0'],['type_of_activity'=>'و.ش','total'=>'0'],['type_of_activity'=>'ق.ع','total'=>'0'],
['type_of_activity'=>'م.و','total'=>'0'],['type_of_activity'=>'م.غ','total'=>'0'],['type_of_activity'=>'س.ن','total'=>'0'],['type_of_activity'=>'ح.ف','total'=>'0']]
that there areport based on this value
the return array from DB my be ['type_of_activity'=>'م.و','total'=>'5'],['type_of_activity'=>'م.غ','total'=>'10'],['type_of_activity'=>'س.ن','total'=>'15']
is there a way to map this array to the original array where the match keys
without losing any key or value
the result will be just 11 arrays within the original
[['type_of_activity'=>'م.ص','total'=>'0' ],
[['type_of_activity'=>'م.ع','total'=>'0' ],
['type_of_activity'=>'م.و','total'=>'5'],['type_of_activity'=>'م.ن','total'=>'0'],['type_of_activity'=>'م.خ','total'=>'0'],['type_of_activity'=>'م.ت','total'=>'0'],['type_of_activity'=>'و.ش','total'=>'0'],['type_of_activity'=>'ق.ع','total'=>'0'], ['type_of_activity'=>'م.غ','total'=>'10'],['type_of_activity'=>'س.ن','total'=>'15'],['type_of_activity'=>'ح.ف','total'=>'0']],
You can use array_merge,
$c = array_merge($a,$b);
print_r($c);
Demo
O/p
Array
(
[mw] => 0
[mg] => 5
[ma] => 0
[sn] => 0
[fa] => 0
[mn] => 10
)
Related
I want to need multiple array combaine in one array
I have array
array(
0=> test 1
)
array(
0=> test 2
)
array(
0=> test 3
)
I need expected output
`array(
0=>Test1
1=>Test2
2=>test3
)`
You can use the array_merge() for this. The array_merge() function merges one or more arrays into one array.If two or more array elements have the same key, the last one overrides the others.
Syntax:
array_merge(array ...$arrays): array
Example:
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
Result:
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
You can check more here.
$a = array('test_1');
$b = array('test_2');
$c = array('test_3');
print_r(array_merge($a,$b,$c));
O/P - Array ( [0] => test_1 [1] => test_2 [2] => test_3 )
Hope you are doing well and good.
So, as per your requirement i found solution to get result.
$array1 = [0 => "Test 1"]; $array2 = [0 => "Test 2"]; $array3 = [0 => "Test 3"];
print_r(array_merge($array1,$array2,$array3));
In the above example you have to merge the n number of array with single array, so for that you need to use array function which is array_merge(array ...$array).
What is array_merge()?
The array_merge() function merges one or more arrays into one array.
Tip: You can assign one array to the function, or as many as you like.
Note: If two or more array elements have the same key, the last one overrides the others.
How can I sort an array by value, but instead of changing the position of the values, change the position of the keys?
Array
(
[0] => 16
[1] => 12
[2] => 30
)
When I sort this array, I want to get the output like this:
Array
(
[1] => 16
[0] => 12
[2] => 30
)
Your starting array:
$a = [16, 12, 30];
First make a copy:
$b = $a;
Then use asort on one of them to sort it while maintaining the key association:
asort($a);
Then use array_combine with array_keys to create your result array using the the keys from the sorted array, and the values from the unsorted array.
$result = array_combine(array_keys($a), $b);
I have following array and getting this result:
Array
(
[0] => stdClass Object
(
[UnitIdx] => 10
[Title] => 순차
[Description] =>
[NumSteps] => 9
[ThumbnailPathName] => Level_1_Unit_thumbnail_Small.png
[Step_1] => 4
[Step_2] => 5
[Step_3] => 6
[Step_4] => 7
[Step_5] => 8
[Step_6] => 9
[Step_7] => 10
[Step_8] => 11
[Step_9] => 12
[Step_10] => 0
[Step_11] => 0
[Step_12] => 0
[Step_13] => 0
[Step_14] => 0
[Step_15] => 0
[Step_16] => 0
[Step_17] => 0
[Step_18] => 0
[Step_19] => 0
[Step_20] => 0
)
)
Now I want to find key form value. For example value is 11 so key is Step_8.
Any idea how to return key name from value?
Thanks.
You can search your key by value using array_search() and converting your Object into PHP array by typecasting, below is an example:
<?php
$object = array();
$object[0] = new StdClass;
$object[0]->foo = 1;
$object[0]->bar = 2;
echo "<pre>";
print_r($object);
echo "</pre>";
$key = array_search ('2', (array) $object[0]);
echo "<pre>";
print_r($key);
?>
Output:
Array
(
[0] => stdClass Object
(
[foo] => 1
[bar] => 2
)
)
bar
Take a look at this:
<?php
//this part of code is for prepare sample data which is similar to your data
$class = new stdClass;
$class->prop1 = 'prop1';
$class->prop2 = 'prop2';
$array = array($class);
//THIS IS METHOD FOR SEARCH KEY BY VALUE
print_r(array_search('prop1',json_decode(json_encode($array[0]), true))); //you are looking for key for value 'prop1'
Check working fiddle: CLICK!
Explanation:
1) json_decode(json_encode($array[0]), true) - because you have in your array stdClass object, you can't use array_search function. So this line converts $array[0] element, which is stdClass object to an array. Now we can use array_search function to search key by specific value.
2) print_r(array_search('prop1',json_decode(json_encode($array[0]), true))); - we are using array_search function to get key of array element which value is equal to prop1. Documentation of this function says:
array_search — Searches the array for a given value and returns the
corresponding key if successful
So we getting key corresponding to prop1 value, which is prop1. print_r function shows us result. Instead of that you can assign result of operation to a variable and use it in other parts of code, for example:
$variable = array_search('prop1',json_decode(json_encode($array[0]), true));
I'm trying to get all array elements, where the value only occurs once in the array.
I tried to use:
array_unique($array);
But this does only remove the duplicates, which is not what I want.
As an example:
$array = 0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
5 => 2
6 => 3
7 => 4
8 => 5
Expected output:
array(
0=>1
)
As you can see only the value 1 occurs once in the array, all other values are more than once in the array. So I only want to keep that one element.
This should work for you:
First use array_count_values() to count how many times each value is in your array. This will return something like this:
Array (
[1] => 1
[2] => 2
[3] => 2
[4] => 2
[5] => 2
// ↑ ↑
// Value Amount
)
After that you can use array_filter() to only get the values, which occurs once in your array. Means:
Array (
[1] => 1
[2] => 2
[3] => 2
[4] => 2
[5] => 2
)
And at the end simply use array_keys() to get the value from the original array.
Code:
<?php
$arr = [1,2,3,4,5,2,3,4,5];
$result = array_keys(array_filter(array_count_values($arr), function($v){
return $v == 1;
}));
print_r($result);
?>
output:
Array (
[0] => 1
)
You can use array_count_values to get the number of times each value exists in the array. You can use this to get all the values that occur only once by looking at the value in the returned array.
If you already have an array and it is in the structure described above, you should be able to just array_search(1, $array) and it will give you the key of the array with the value of 1. Or if you expect to have multiple keys with the value of 1, you can use array_keys($array, 1) and it will return an array of keys that have the value of 1. Hope this helps.
I have an array in PHP that looks like
Array ( [123654] => Array ( [0] => 123456789123456789 [1] => 1 [2] => 06/24/2011 [3] => 06/24/2012 [4] => 12355.44 [5] => 55321.55 ) )
I know in javascript I could access the data I need by doing array[0][0], how would I go about doing this in PHP. It is the 123456789123456789 value that I'm looking at getting.
Try this
array_slice($array, 0, 1);
http://php.net/array_slice
If you don't know the exact keys, you could do something like this:
$a = array_values($my_array);
$b = array_values($a[0]);
echo $b[0];
array_values replaces the keys by simple numbers from 0 to n-1 (where n is the count of values), by that you can access your desired value with the indexes [0][0]. See more here
http://codepad.org/YXu6884R
Here you go. See above for proof. The methodology from #azat is not explicit enough and is prone to risk if the elements of the array or sub array are re-arranged or if the key value for the super array changes.
$my_array = array( 123654 => array( 0 => '123456789123456789', 1 => '1', 2 => '06/24/2011', 3 => '06/24/2012', 4 => '12355.44', 5 => '55321.55' ) );
echo $my_array['123654'][0];
Try
$first = array_shift(array_values($array));
http://php.net/manual/en/function.array-shift.php