Array
(
[0] => Array
(
[ADADCD] =>
)
[1] => Array
(
[ADADCD] => ?
)
[2] => Array
(
[ADADCD] => HOSP1
)
[3] => Array
(
[ADADCD] => HOSP2
)
[4] => Array
(
[ADADCD] => H1
)
)
We have an array like this ,I want to search a specific value like HOSP2,What is the process to get the value at index.
Just try with array_search
$key = array_search(array('ADADCD' => 'HOSP1'), $inputArray);
Loop trough the array and return the index on which you find the value you are looking for.
$searchIndex = -1;
foreach ( $yourArray as $k => $v ) {
if ( $v['ADADCD'] == 'search value' ) {
$searchIndex = $k;
break;
}
}
You can use a combination of foreach() and in_array().
So, first looping through all the indices of the array using foreach().
foreach ($array as $key => $subarray)
if (in_array($string, $subarray))
return $key;
So, now for an array like this:
Array
(
[0] => Array
(
[ADADCD] =>
)
[1] => Array
(
[ADADCD] => ?
)
[2] => Array
(
[ADADCD] => HOSP1
)
[3] => Array
(
[ADADCD] => HOSP2
)
[4] => Array
(
[ADADCD] => H1
)
)
Output
2
Fiddle: http://phpfiddle.org/main/code/t6b-g9r
<?php
$array = your array;
$key = array_search('HOSP2', $array);
echo $key;
?>
Output: ADADCD
http://php.net/manual/en/function.array-search.php
Related
In PHP I have this structure of Array (some are empty, some not, some are multiple items):
Array
(
[0] => Array
(
)
[1] => Array
(
[0] => 16534
)
[2] => Array
(
)
[3] => Array
(
[0] => 16532
[1] => 16533
)
[4] => Array
(
)
[5] => Array
(
[0] => 14869
)
}
I want to loop through this array, so as the result I get only the numbers (all of them).
I tried it this way:
foreach ($myarray as $item) {
echo '<pre>' . print_r($item) . '</pre>';
// $result[] = $this->myMethod($item);
}
So in foreach I want to use all the items from array in my method.
However when I echo the $item in the loop, I have something like this:
Array ( )
Array ( [0] => 16534 )
Array ( )
Array ( [0] => 16532 [1] => 16533 )
Array ( )
Array ( [0] => 14869 )
So still arrays (also the empty ones), and not numbers.
Can you please help with this?
UPDATE: I just noticed, some of the arrays looks like this:
[6] => Array
(
[0] => Array
(
[id] => 269
[hours] => 21.0
)
)
[7] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 2
[hours] => 12.0
)
[1] => Array
(
[id] => 7
[hours] => 24.0
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[hours] => 5.0
)
[1] => Array
(
[id] => 7
[hours] => 0.583
)
)
)
but here the solution works not.
This one seems working but it is now brutal foreach in foreach solution:
foreach ($myarray as $item2) {
foreach ($item2 as $key2 => $val2) {
if (isset($val2)) {
foreach ($val2 as $key4 => $val4) {
echo $val4['id'].',';
}
}
}
}
Just merge the array which will also remove the empties:
foreach(array_merge(...$myarray) as $item) {
// echo '<pre>' . print_r($item) . '</pre>';
$result[] = $this->myMethod($item);
}
This will also work and may be faster:
$result = array_map([$this, 'myMethod'], array_merge(...$myarray));
If you have an old PHP version you'll have to use array() instead of [] and:
call_user_func_array('array_merge', $myarray)
You are only looping through the main array.That's the reason why you are getting an array when you are printing the result set.(because those values are stored in sub arrays and you are not looping them.)
And to remove the sub arrays with empty values I'll use isset() so that you will get only the values.
Change your code into this.
foreach ($myarray as $item) {
foreach($item as $key=>$val){
if(isset($val){
$values[] = $val;
// $result[] = $this->myMethod($values);
}
}
}
I have a multidimensional associative array which has a set of array. I want to change my array index value from some array value.
I already tried some array functions but my array also contains some null array so laravel function keyBy not give me wanted result.
$arr1=array(0 =>array(),1=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
2 =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
My expected result array must be like this
$arr2=array(0 =>array(),'baroque'=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
'adidas' =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
You can use the classic foreach. Check if the handle on element 0 exists using isset, if it does, use that as the key.
$arr1 = //...
$result = array();
foreach($arr1 as $key => $val) {
if (is_array($val) && isset($val[0]["handle"])) $result[ $val[0]["handle"] ] = $val;
else $result[$key] = $val;
}
$result will be:
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
You can use without condition by grouping at the handle as key directly.
$result = [];
foreach ($arr as $key => $value) {
if (!empty($value)) {
foreach ($value as $key1 => $value1) {
$result[$value1['handle']][] = $value1;
}
} else {
$result[] = $value;
}
}
Demo
Output:-
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
Try this..
$res = [];
foreach($x as $key => $value)
{
if(empty($value))
{
$res[] = $value;
}
else
{
foreach($value as $v => $k)
{
if(array_key_exists($k['handle'],$res))
{
$res[$k['handle']][] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
else
{
$res[$k['handle']][0] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
}
}
}
The result is going to be like this.
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
I want to fetch the first element of an array which is inside another array.
My array is:-
Array (
[0] => Array (
[0] => 5a3a13f237715637629.jpeg
)
[1] => Array (
[0] => 5a3b602654cfd527057.jpg
)
)
I want to get:-
Array (
[0] => 5a3b602654cfd527057.jpg
)
only from it.
Any help is welcome.
try like this
<?php
$arr=array( 0 => array ( 0 => "5a3a13f237715637629.jpeg" ) ,1 => array ( 0 => "5a3b602654cfd527057.jpg" ) );
// print_r($arr);
echo $arr[1][0];
?>
you should try with current function,Return the current element in an array
$value = current(current($array));
reference http://php.net/manual/en/function.current.php
Try this if your array is :
$arr = Array (
[0] => Array (
[0] => 5a3a13f237715637629.jpeg
)
[1] => Array (
[0] => 5a3b602654cfd527057.jpg
)
)
Then do :
$result_arr = array();
foreach($arr as $key => $value)
{
$result_arr[] = $value[0];
}
echo "<pre>";
print_r($result_arr);
die;
you will get :
Array (
[0] => 5a3a13f237715637629.jpeg,
[1] => 5a3b602654cfd527057.jpg
)
<?php
$array = Array (
0 => Array (
0 => '5a3a13f237715637629.jpeg'
),
1 => Array (
0 => '5a3b602654cfd527057.jpg'
)
);
foreach($array as $val)
{
echo $val[0];
}
//OR
echo $array[0][0];
?>
It seems like you are trying to find a variable in an array. To do this you may try array_column(). It might work.
I am new in PHP and love to learn it.
I want to merge two or more arrays having same keys only. And neglect the arrays whose keys are not in both the Arrays. Like
Here is the First Array :
Array
(
[1] => Array
(
[111] => 36265
)
[2] => Array
(
[222] => 36265
)
[3] => Array
(
[333] => 36265
)
)
and Second Array as :
Array
(
[1] => Array
(
[444] => 36265
)
[2] => Array
(
[555] => 36265
)
[4] => Array
(
[666] => 36265
)
)
And i want my result to be as :
Array
(
[1] => Array
(
[111] => 36265
[444] => 36265
)
[2] => Array
(
[222] => 36265
[555] => 36265
)
)
Neglecting the rest Array with key [3] & [4]....
So Please anyone tell me how to get this. I try out "array_merge_recursive()" but this one displays all keys.
Thanks in advance.
You'd have to loop over one of the arrays, check for the existence of the current key in the other array, if it exists, merge them, eg:
$output = array();
foreach ($array1 as $key => $value) {
if (array_key_exists($key, $array2)) {
$output[$key] = $value + $array2[$key];
}
}
Here's a demo
You're probably looking for array_intersect_key.
You could also use a foreacah loop and create a new one. While in this loop you can use one of the arrays keys to match them both. Consider this example:
$array1 = array( 1 => array(111 => 36265), 2 => array(222 => 36265), 3 => array(333 => 36265), ); $array2 = array( 1 => array(444 => 36265), 2 => array(555 => 36265), 4 => array(666 => 36265), );
$new_array = array();
foreach($array1 as $key => $value) {
if(isset($array2[$key])) {
$new_array[$key][key($array1[$key])] = reset($array1[$key]);
$new_array[$key][key($array2[$key])] = reset($array2[$key]);
}
}
echo '<pre>';
print_r($new_array);
echo '</pre>';
Should yield something like this:
Array
(
[1] => Array
(
[111] => 36265
[444] => 36265
)
[2] => Array
(
[222] => 36265
[555] => 36265
)
)
The following code is converting XML to array. I want to get a sting by the value of [name]. So for example: $..[offerid].. should give 8b5f7fd3806a42ccb0ade9f8309c5587
Who can help me?? :)
$xmldata = file_get_contents($XMLURL);
$arr= xml2ary($xmldata);
foreach($arr['m4n']['_c']['data']['_c']['record'] as $result){
echo "<pre>"; print_r($result);
}
The echo result is:
Array
(
[recordHash] => Array
(
[_v] => -652572603
)
[column] => Array
(
[0] => Array
(
[_a] => Array
(
[name] => url
)
[_v] => http://ad.dd.com/ppc/?20910868C187884459&zpar6=DF_1&ULP=[[http%3A%2F%2F]]
)
[1] => Array
(
[_a] => Array
(
[name] => title
)
[_v] => This is the title
)
[2] => Array
(
[_a] => Array
(
[name] => description
)
[_v] => Aanbod
)
[3] => Array
(
[_a] => Array
(
[name] => offerid
)
[_v] => 8b5f7fd3806a42ccb0ade9f8309c5587
)
)
)
Try looping through all of the items using a php foreach loop.
$name_to_find = 'offerid';
$value = FALSE;
foreach ($result['column'] as $item) {
if ($item['_a']['name'] == $name_to_find) {
$value = $item['_v'];
break;
}
}
// Now $value has the value of the entry with the name $name_to_find
If you end up finding an entry with the given $name_to_find, then $value will not be FALSE (granted that, you don't have any FALSE values :D).