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
Related
This question already has answers here:
How to re-index all subarray elements of a multidimensional array?
(6 answers)
Closed 2 years ago.
I use a PHP array to store data about all the people a user is following on a website. Here is an example of how I have it set up:
$data = array(
['user1'] => array(
[0] => 'somedata',
[1] => 'moredata',
[2] => array(
[0] => 'Jim',
[1] => 'Bob',
[2] => 'Nick',
[3] => 'Susy',
)
),
);
As you can see, it is $data[user][2] that lists all the friends. The array has this exact appearance with [0] and [1] for keys because that is how var_export() does it. Now my problem is this. When someone unfollows somebody, I use unset() to delete that friend from the array. So if I want to unfollow Bob in the example above, it would be left with Jim, Nick, and Susy.
The only issue now is that the array keys do not renumber properly when they rename. So once Bob is gone it goes from 0 to 2 rather than Nick taking on the array key of 1. Now I can think of ways to do this myself but I would highly prefer if there were some PHP function specifically for solving this issue, that is, renaming these array keys to the proper numerical order. I checked out the sort() function but that seems for alphabetizing array values not keys.
You can use array_values to re index the array numerically.
$newArray = array_values($array);
If you just want to re-index the array at that level, you could simply use array_values();
For example, assuming you are removing the "bob" entry, just call array_values at the level directly above bob after removing it.
unset($data['user1'][2][1]);
$data['user1'][2] = array_values($data['user1'][2]);
I'd use array_values like this:
$data['user1'][2]=array_values($data['user1'][2]);
Here's the full code:
$data = array(
'user1' => array(
'somedata',
'moredata',
array(
'Jim',
'Bob',
'Nick',
'Susy',
)
),
);
unset($data['user1'][2][1]);
var_export ($data['user1'][2]);
echo "\n\n";
$data['user1'][2]=array_values($data['user1'][2]);
var_export($data['user1'][2]);
Result
array (
0 => 'Jim',
2 => 'Nick',
3 => 'Susy',
)
array (
0 => 'Jim',
1 => 'Nick',
2 => 'Susy',
)
See it in action here:
Sandbox
You could use array_splice
$removedElement = array_splice($data['user1'][2], $indexOfUserToRemove, 1);
This alters the original array reindexing it, but only if the keys of the array are numeric.
In PHP Array index addition - different levels
is there a way to get array key names 'weighted', 'unweighted' , 'weighted_sum', 'unweighted_sum' rather than array index numbers?
So in the required output rather than [0] => Array, [1] => Array, [2] => ,
[3] =>
is there a way to get 'weighted' => Array, 'unweighted' => Array, 'weighted_sum' => , 'unweighted_sum' =>
tia
Jas
$keys = array_keys($m_aggregate);
$tot_aggregate = sum($m_aggregate, $f_aggregate);
$tot_aggregate = array_combine($keys, $tot_aggregate);
$array=array(
'weighted' => 'x',
'unweighted' => 'y',
'weighted_sum' => 'z'
);
echo $array['weighted'];
Multidimensional arrays
http://webcheatsheet.com/PHP/multidimensional_arrays.php
I've researched topics similar to this but not exactly what I'm looking to do.
I have a multidimensional array like the following.
[code] => BILL
[assets] => Array
(
[en] => Array
(
[datatype] => My Assets
[data] => Array
(
[Corporate Equity] => 41
[Global Equity] => 24
[Fixed Income – Government] => 22
[Fixed Income – Corporate] => 8.1
[Other] => 3.57
)
)
)
I'd like to remove the first inner array, but preserve the values. Shift them up one level in the array so that it looks like this.
[code] => BILL
[assets] => Array
(
[datatype] => My Assets
[data] => Array
(
[Corporate Equity] => 41
[Global Equity] => 24
[Fixed Income – Government] => 22
[Fixed Income – Corporate] => 8.1
[Other] => 3.57
)
)
This is just the beginning of the array, there are other instances of the same key [en] at the same level.
I've tried unset, array_shift and others but I need to keep the contents of [en], just shift them up one level in the array.
You can use array_map which returns an array which contains all elements of the previous array after applying the function.
In this case it will simply take the array at index en and add it's contents to the new array.
http://php.net/manual/en/function.array-map.php
$arr = array('assets' => array(
'en' => array(
'datatype' => 'My Assets',
'data' => array(
'Corporate Equity' => 41,
'Global Equity' => 24,
'Fixed Income – Government' => 22,
'Fixed Income – Corporate' => 8.1,
'Other' => 3.57
)
)
));
$new_arr = array_map(function ($e) {
return $e['en'];
}, $arr);
A simple solution that assumes the key to always be en and the subkeys to always be (only) datatype and data:
$assets['datatype'] = $assets['en']['datatype'];
$assets['data'] = $assets['en']['data'];
unset( $assets['en'] );
This code could be problematic for you in the future if that array structure ever changes (it lacks extensibility), but it gets you what you want given the information you have provided.
array_shift is the opposite of array_pop. Used in stack/queue like structures for removing the fist element http://php.net/manual/en/function.array-shift.php
What you want to do is flatten the array. But if you want to keep all the other sub-arrays as you mentioned, you might look up array_merge.
I faced the same scenario after using reader to read xml file, the returned array was having inserted 0 key array in each level like the following one:
'config' =>
0 =>
'products' =>
0 =>
'media' =>
.
.
.
so I built a small function to get rid of a specific key and shift up its child's in a two dimensions array, in my case the key was 0. hopping this would help somebody also.
public function clearMaps(&$maps, $readerMaps, $omittedKey)
{
if (is_array($readerMaps)) {
foreach ($readerMaps as $key => $map) {
if ($key !== $omittedKey) {
$maps[$key] = [];
$this->clearMaps($maps[$key], $readerMaps[$key], $omittedKey);
} else {
$this->clearMaps($maps, $readerMaps[$key], $omittedKey);
}
}
} else {
$maps = $readerMaps;
}
}
// $maps: cleaned array, will start as empty array
// $readerMaps: array needs to be cleaned
// $omittedKey: array key to git rid of.
// first call is clearMaps([], $readerMaps, 0);
Just a quick one i need to get a value from an array the array is made like this
$resultOfAdd[“CaseAndMatterResult”][“ResultInfo”][“ReturnCode”];
and it gives an output of this
Array (
[AddCaseAndMatterResult] => Array (
[ResultInfo] => Array (
[ReturnCode] => OK
[Errors] =>
[Warnings] =>
)
[CaseID] => 4880062
[MatterID] => 4950481
[LeadID] => 0
[CustomerID] => 0
)
)
All i want to do is put the part "MatterID" into a variable. how would I achieve this.
i have tried
$matterID = array($resultOfAdd["MatterID"]);
and this does not work
Regards
This is a multi-dimensional, associative array. Think of it like floors of a building. The key MatterID does not live in the first dimension (floor), rather on the second, in the AddCaseAndMatterResult sub-array.
$matterID = $resultOfAdd['AddCaseAndMatterResult']['MatterID']
Successive dimensions of an array are specified with successive square-brackets, each naming the key to look in (this is true of most languages).
$matterID = $yourArray['AddCaseAndMatterResult']['MatterID'];
Use this way:
$matterID = $resultOfAdd['AddCaseAndMatterResult']['MatterID'];
I have an array with some value like this:
[Organization_id] => Array
(
[0] => 4
[1] => 4
[2] => 4
)
but i want some thing like this:
[Organization_id] => Array
(
[0] => 4
)
Thanks in advance..
If you don't care about the key to value association possibly messing up, you can use this:
$array = array_unique($array);
Although array_unique was mentioned twice now, I feel the answers failed to point out that you have to use the function on the nested array and not the array itself, so here is a usage example
$array = array( 'Organization_id' => array(4,4,4) );
$array['Organization_id'] = array_unique( $array['Organization_id'] );
print_r($array);
which will do what you want.