How do I get the values from array of array? - php

I have array like below:
Array
(
[_edit_lock] => Array
(
[0] => 1309611144:1
)
[_edit_last] => Array
(
[0] => 1
)
[banner_url] => Array
(
[0] => http://apptivowp.apptivo.com/awp-content/10001/uploads/2011/07/gallery_banner.jpg
)
[_yoast_wpseo_focuskw] => Array
(
[0] =>
)
[_yoast_wpseo_title] => Array
(
[0] =>
)
)
How can I get the banner_url value?

As simple as:
$array['banner_url'][0]

Related

How to ordering or sort array by keys in ascending using PHP?

My problem is that I want to sort array keys by ascending and I am googling last 2 days what I am getting the solution is uksort, ksort but it's not give the exact solution what am I want.I feel great full for helping to solve this issue. My output array is:-
Array
(
[2020-06-22,13:00] => Array
(
[0] => 1663
[1] => 1664
)
[2020-06-22,14:00] => Array
(
[0] => 1665
[1] => 1666
)
[2020-06-24,13:00] => Array
(
[0] => 1715
)
[2020-06-24,8:30] => Array
(
[0] => 1714
)
[2020-06-23,13:00] => Array
(
[0] => 1724
)
[2020-06-23,8:30] => Array
(
[0] => 1727
)
)
and we need array is in:-
Array
(
[2020-06-22,13:00] => Array
(
[0] => 1663
[1] => 1664
)
[2020-06-22,14:00] => Array
(
[0] => 1665
[1] => 1666
)
[2020-06-23,8:30] => Array
(
[0] => 1727
)
[2020-06-23,13:00] => Array
(
[0] => 1724
)
[2020-06-24,8:30] => Array
(
[0] => 1714
)
[2020-06-24,13:00] => Array
(
[0] => 1715
)
)

How to remove first index of array in php

I have the following data
Array ( [0] => Array ( [operations] => Array ( [0] => Array ( [method] =>
POST)))
Array ( [1] => Array ( [operations] => Array ( [0] => Array ( [method] => POST))))
I want to dispay like this
Array ( [operations] => Array ( [0] => Array ( [method] => POST)))
How to i remove the first index of array
I am using the following code but its not work for multiple array
$key = array_shift($array);
I think I finally get what your after. Try this and see if it gets you what you want:
$array = Array (
array('0' => Array ( 'operations' => Array ( '0' => Array ( 'method' => POST)))),
array('1' => Array ( 'operations' => Array ( '0' => Array ( 'method' => POST))))
);
foreach($array as $key=>$value){
$results[$key]['operations'] = $array[$key][$key]['operations'];
}
print_r($results);
This will output:
Array
(
[0] => Array
(
[operations] => Array
(
[0] => Array
(
[method] => POST
)
)
)
[1] => Array
(
[operations] => Array
(
[0] => Array
(
[method] => POST
)
)
)
)

Merging array keys in multidimentional array does not give desired output

This is my generated array
i have tried array_merge_recrusive but not successful.
I am doing program it should generate this array
Array
(
[0] => Array
(
[competancy] => Array
(
[0] => 1
)
)
[1] => Array
(
[assistment] => Array
(
[0] => 0
)
)
[2] => Array
(
[competancy] => Array
(
[0] => 3
)
)
[3] => Array
(
[competancy] => Array
(
[0] => 4
)
)
[4] => Array
(
[assistment] => Array
(
[0] => 0
)
)
[5] => Array
(
[competancy] => Array
(
[0] => 6
)
)
required
[6] => Array
(
[competancy] => Array
(
[0] => 7
)
)
[7] => Array
(
[assistment] => Array
(
[0] => 0
)
)
)
I want following output array
I have tried lot of but not find this type of output
Array
(
[competancy] => Array
(
[0] => 1
)
[assistment] => Array
(
[0] => 0
)
[competancy] => Array
(
[0] => 3
[0] => 4
)
[assistment] => Array
(
[0] => 0
)
[competancy] => Array
(
[0] => 6
[0] => 7
)
[assistment] => Array
(
[0] => 0
)
)
Try to merge array with splat :
print_r(array_merge(...$array));

How do i set the key value after unset the array value?

I have array like below,
Array
(
[14289] => Array
(
[0] => Karthikeyan
[1] => Mathi
[2] => RAvi
)
[14279] => Array
(
[0] => Karthikeyan
[1] => Mathi
[2] => RAvi
)
[14288] => Array
(
[0] => Karthikeyan
)
[14291] => Array
(
[0] => Mathi
)
[14284] => Array
(
[0] => RAvi
)
)
if I unset the index[1] value from [14289] this one. after that the array display like below,
Array
(
[14289] => Array
(
[0] => Karthikeyan
[2] => RAvi
)
[14279] => Array
(
[0] => Karthikeyan
[1] => Mathi
[2] => RAvi
)
[14288] => Array
(
[0] => Karthikeyan
)
[14291] => Array
(
[0] => Mathi
)
[14284] => Array
(
[0] => RAvi
)
)
But I need [14289] values like below,
Array
(
[14289] => Array
(
[0] => Karthikeyan
[1] => RAvi
)
........
the key value should be [1] not [2]. how can I do this?
after
unset $example_array[14289][1];
do
$example_array[14289]= array_values($example_array[14289]);
Use array_values on the array.

PHP search key in array and return its value?

I would like to search key in multidimensional array and i would like to get corrosponding value associated with that key.
For e.g.
I would like to extract following texts from below array :
SENT AT 12.08ms
And the text
sample id 41962
following is an array print_r() output :
Array
(
[0] => Array
(
[VERSION] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
)
[value] => Array
(
[0] => Array
(
[0] => 3.0
)
)
)
)
[SAMPLE] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
)
[value] => Array
(
[0] => Array
(
[0] => sample id 41962
)
)
)
)
[TSAM] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
)
[value] => Array
(
[0] => Array
(
[0] => sample group 141
)
[1] => Array
(
[0] => ¯
)
[2] => Array
(
[0] => sample batch 81
)
[3] => Array
(
[0] =>
)
[4] => Array
(
[0] =>
)
)
)
)
[STATUS] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
[TYPE] => Array
(
[0] => CART
)
)
[value] => Array
(
[0] => Array
(
[0] => SENT AT 12.08ms
)
)
)
)
)
)
Can somebody provide me optimized code for above problem. The multidimensional array contains more than 5000 to 10000 arrays.
Please, see if my function works for you:
function get_value_by_key($array,$key)
{
foreach($array as $k=>$each)
{
if($k==$key)
{
return $each;
}
if(is_array($each))
{
if($return = get_value_by_key($each,$key))
{
return $return;
}
}
}
}
Use:
$array = array('array1'=>array('array2'=>array('find_some_key'=>'some_value')));
echo get_value_by_key($array,'find_some_key'); // outputs: some_value
If all the array keys have the same structure the following code should work:
foreach($array as $item){
$sentat = $item['STATUS'][0]['value'][0][0];
$sample = $item['SAMPLE'][0]['value'][0][0];
}
More detailed information would help us to provide you more tips :)

Categories