How to remove first index of array in php - 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
)
)
)
)

Related

Push in array if key exists

I have the following multidimensional array:
Array
(
[0] => Array
(
[name] => Viettel
[data] => Array
(
[0] => 1559881644000,500
)
)
[1] => Array
(
[name] => FPT
[data] => Array
(
[0] => 1559994465000,172
)
)
[2] => Array
(
[name] => MobiFone
[data] => Array
(
[0] => 1559997314000,11164
)
)
[3] => Array
(
[name] => Vietnamobile
[data] => Array
(
[0] => 1559993699000,1246
)
)
[4] => Array
(
[name] => Viettel
[data] => Array
(
[0] => 1560000096000,47886
)
)
[5] => Array
(
[name] => VinaPhone
[data] => Array
(
[0] => 1559997553000,11132
)
)
[6] => Array
(
[name] => VNPT
[data] => Array
(
[0] => 1559993066000,84
)
)
)
I'm currently using a foreach loop to extract the values from the array:
$result = [];
$nameData = [];
foreach($data as $key => $itemData)
{
}
I want array look like this:
Array
(
[0] => Array
(
[name] => Viettel
[data] => Array
(
[0] => 1559881644000,500
[1] => 1560000096000,47886
)
)
[1] => Array
(
[name] => FPT
[data] => Array
(
[0] => 1559994465000,172
)
)
[2] => Array
(
[name] => MobiFone
[data] => Array
(
[0] => 1559997314000,11164
)
)
[3] => Array
(
[name] => Vietnamobile
[data] => Array
(
[0] => 1559993699000,1246
)
)
[4] => Array
(
[name] => VinaPhone
[data] => Array
(
[0] => 1559997553000,11132
)
)
[5] => Array
(
[name] => VNPT
[data] => Array
(
[0] => 1559993066000,84
)
)
)
It seems you are looking for groupby the array by name.
You can use foreach with array_key_exists and array_push
$groupBy = [];
foreach($a as $v){
array_key_exists($v['name'], $groupBy) ?
array_push($groupBy[$v['name']]['data'], $v['data'][0])
:
($groupBy[$v['name']] = $v)
;
}
If you want to rearrange the keys of array, you can use array_values
print_r(array_values($groupBy));
Working DEMO :- https://3v4l.org/ASWDV
You can just loop over the input array, adding values to the output if they don't exist and merging that data values if they do:
$result = array();
foreach ($data as $itemData) {
if (($k = array_search($itemData['name'], array_column($result, 'name'))) !== false) {
$result[$k]['data'] = array_merge($result[$k]['data'], $itemData['data']);
}
else $result[] = $itemData;
}
print_r($result);
Output is as you desire (too long to reproduce here)
Demo on 3v4l.org
If you are adding a single value to the array you can just access to it with something like this:
$result[0]['data'][1] = 1560000096000,47886

how to get values a long array with size zero

I have following array from google api and wanted to get id only. How do i get id's from following. when i try to get size it gives me size zero.
Google_Service_Drive_FileList Object ( [collection_key:protected] => items
[internal_gapi_mappings:protected] => Array ( ) [etag] => [itemsType:protected]
=> Google_Service_Drive_DriveFile [itemsDataType:protected] => array [kind] =>
[nextLink] => [nextPageToken] => [selfLink] => [modelData:protected] => Array (
[items] => Array ( [0] => Array ( [id] => 0B0OnHwH_cQckeWZPdXFyRU5aMGs ) [1] =>
Array ( [id] => 0B0OnHwH_cQckaUVORkZaM2NoRXM ) [2] => Array ( [id] =>
1kCQLhEgzgeKO-L57ISWjQL4ctkxT4Gq2wrdzDFbrcac ) [3] => Array ( [id] =>
0B0OnHwH_cQckc3RhcnRlcl9maWxl ) [4] => Array ( [id] => 1-Yhs92vZnvUNArwAcZJZ9xa-
fXZ7ZgRrADyF-ikG1gU ) ) ) [processed:protected] => Array ( ) ) Array ( )
if you mean the object attributes, you can do
$values = array();
$keys = array();
foreach($object as $key=>$value)
{
array_push($keys,$key);
array_push($values,$value);
}
This will give you each object attribute and its corresponding value

How do I get the values from array of array?

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]

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

Conditionally replace values in multidimensional PHP array

There has been a few other questions regarding replacing values in multidimensional array on here, but I didn't find anything regarding what I was trying to do, exactly, per se.
I have an array that I get from an API and I need to update a few values based on other values in the array tree before sending the API payload to the browser.
In the array when the sale[0] === true I am looking for some logic to then replace the [price][0] value with a corresponding new sale price.
Using the foreach, I can easily loop through the each of the nodes in the array, but I am unsure once I loop through the array, how I can update the original array with new price if/when sale node === true.
Array
(
[response] => Array
(
[0] => Array
(
[results] => Array
(
[0] => Array
(
[items] => Array
(
[0] => Array
(
[id] => Array
(
[0] => 846471605959
)
[title] => Array
(
[0] => Test Item 846471605959
)
[imageURL] => Array
(
[0] => https://foo/bar/images/846471605959.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/846471605959
)
[price] => Array
(
[0] => 799.00
)
[sale] => Array
(
[0] => true
)
)
[1] => Array
(
[id] => Array
(
[0] => 414953260545
)
[title] => Array
(
[0] => Test Item 414953260545
)
[imageURL] => Array
(
[0] => https://foo/bar/images/414953260545.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/414953260545
)
[price] => Array
(
[0] => 199.00
)
[sale] => Array
(
[0] => false
)
)
[2] => Array
(
[id] => Array
(
[0] => 684865199812
)
[title] => Array
(
[0] => Test Item 684865199812
)
[imageURL] => Array
(
[0] => https://foo/bar/images/684865199812.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/684865199812
)
[price] => Array
(
[0] => 699.00
)
[sale] => Array
(
[0] => false
)
)
[3] => Array
(
[id] => Array
(
[0] => 987800965761
)
[title] => Array
(
[0] => Test Item 987800965761
)
[imageURL] => Array
(
[0] => https://foo/bar/images/987800965761.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/987800965761
)
[price] => Array
(
[0] => 499.00
)
[sale] => Array
(
[0] => true
)
)
[4] => Array
(
[id] => Array
(
[0] => 005457536677
)
[title] => Array
(
[0] => Test Item 005457536677
)
[imageURL] => Array
(
[0] => https://foo/bar/images/005457536677.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/005457536677
)
[price] => Array
(
[0] => 99.00
)
[sale] => Array
(
[0] => false
)
)
)
)
)
}
)
)
Use a reference variable for the foreach iteration variable, then you can update the element in place.
foreach $data['response'][0]['results'][0]['items'] as &$item) {
if ($item['sale'][0]) {
$item['price'][0] = $new_price;
}
}
If you also need to loop through all the elements in the sale array, add a nested loop.
foreach $data['response'][0]['results'][0]['items'] as &$item) {
foreach ($item['sale'] as $i => $sale) {
if ($sale) {
$item['price'][$i] = $new_price;
}
}
}

Categories