Fetch the first element of an array which is inside another array - php

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.

Related

Sum values in an multi-dimensional array

I want to sum the values of the following array
I would like to group the same items by cumulating them, the output format would also be an array
Array
(
[0] => Array
(
[0] => Array
(
[0] => R421_FD03
[1] => 1
)
[1] => Array
(
[0] => R421_FD03
[1] => 1
)
)
[1] => Array
(
[0] => Array
(
[0] => R421_FD03
[1] => 1
)
[1] => Array
(
[0] => R421_FD02
[1] => 1
)
)
)
I tested this code but the result is not the one I'm waiting for:
$sumArray = array();
array_walk_recursive($data, function($item, $key) use (&$sumArray){
$sumArray[$key] = isset($sumArray[$key]) ? $item + $sumArray[$key] : $item;
});
The result I'm waiting for is this one
Array
(
[0] => Array
(
[0] => R421_FD03
[1] => 3
)
[1] => Array
(
[0] => R421_FD02
[1] => 1
)
)
)
You can apply simple foreach() with array_values()
$final_array = [];
foreach($array as $arr){
foreach($arr as $ar){
$final_array[$ar[0]][0] = $ar[0];
$final_array[$ar[0]][1] = (isset($final_array[$ar[0]][1])) ? $final_array[$ar[0]][1] + $ar[1] : $ar[1];
}
}
$final_array = array_values($final_array);
print_r($final_array);
Output:-https://3v4l.org/lABWG
You can use array_merge with splat operator and then iterate through the array
$merged = array_merge(...$arr);//splat operator
$r = [];
array_walk($merged, function($v,$k) use(&$r){
array_key_exists($v['0'], $r) ? ($r[$v[0]] += $v['1']) : ($r[$v[0]] = $v[1]);
});
print_r($r);
Result :-
Array
(
[R421_FD03] => 3
[R421_FD02] => 1
)

Converting a single array into multi array

I have an array which looks like this below:
Array ( [:status0] => 1 [:status1] => 2 )
I would want to convert it into something like this:
Array ( [:status0] => Array ( [0] => 1 [1] => 1 ) [:status1] => Array ( [0] => 2 [1] => 1 ) )
i want to do this with flexibility because the number of the array and names are random. I was thinking of using a for loop something like this:
foreach ($newParam as $row){
$newArray[$row['Continent']][$row['Country']][] = $row['City'];
}
But i cant use this in my case, please help
This code should do what you want:
$newParam = Array ( ':status0' => 1, ':status1' => 2 ) ;
foreach ($newParam as $key => $value) {
$newArray[$key] = array($value, 1);
}
print_r($newArray);
Output:
Array (
[:status0] => Array (
[0] => 1
[1] => 1
)
[:status1] => Array (
[0] => 2
[1] => 1
)
)
Demo on 3v4l.org

PHP - Merge two arrays on same key

I'd like to merge two arrays on same key.
Here's the 1st array :
Array
(
[2052] => Array
(
[495] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 79755
)
[1] => Array
(
[ID_RI_BELANJA] => 79755
)
)
)
[4566] => Array
(
[488] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 231610
)
[1] => Array
(
[ID_RI_BELANJA] => 231610
)
)
)
)
And this is the 2nd array
Array
(
[2052] => Array
(
[495] => Array
(
[TOTAL_RI] => 1000000
[TOTAL_ANGGARAN] => 500000
)
)
[4566] => Array
(
[488] => Array
(
[TOTAL_RI] => 2000000
[TOTAL_ANGGARAN] => 1000000
)
)
)
And i'd like merge that two arrays to be like this :
Array
(
[2052] => Array
(
[495] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 79755
)
[1] => Array
(
[ID_RI_BELANJA] => 79755
)
[TOTAL_RI] => 1000000
[TOTAL_ANGGARAN] => 500000
)
)
[4566] => Array
(
[488] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 231610
)
[1] => Array
(
[ID_RI_BELANJA] => 231610
)
[TOTAL_RI] => 2000000
[TOTAL_ANGGARAN] => 1000000
)
)
)
This is my first project and I don't know what to do.
Can anyone tell me how to do that?
Pls
If your arrays have same Key then:
$array1 = array(); //put your value in this array
$array2 = array(); //put your value in this array
$array3 = array();
$array3[] = $array1;
$array3[] = $array2;
Assuming your two arrays are $array1 and $array 2 respectively, try this:
foreach($array1 as $k1 => $v1) {
foreach($v1 as $k2 => $v2) {
foreach($v2 as $k3 => $v3) {
$new[$k1][$k2][$k3] = $array1[$k1][$k2][$k3];
$new[$k1][$k2] = array_merge($new[$k1][$k2], $array2[$k1][$k2]);
}
}
}
=> store the all value in this variable
$Arr1 //put your value in this array
$Arr2 //put your value in this array
=> And merge it
$ResponseDetails = array_merge( (array)$Arr1, (array)$Arr2);
array_replace_recursive should do the job:
// $arr1 is the 1st array, $arr2 - is your 2nd array
$result = array_replace_recursive($arr1, $arr2); // now the $result variable contains the expected merged result
http://php.net/manual/en/function.array-replace-recursive.php

Get a part from the array

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).

Search value in array object in PHP

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

Categories