I have an array list like
[1] => Array
(
[name] => Linda
[age] => 23
[country] => USA
)
[2] => Array
(
[name] => Fleur
[age] => 16
[country] => France
)
How do I remove the keys [1], [2] from the array so I get an output like
Array
(
[name] => Linda
[age] => 23
[country] => USA
)
Array
(
[name] => Fleur
[age] => 16
[country] => France
)
Any help would be appreciated. I know it seems simple but I'm new to this.
If you want just to print the output you want, you can just do this:
print_r($arr[1]);
print_r($arr[2]);
There's really not a "removal" option since the "1" and "2" keys you have there are exist for sorting your other subarrays which have similar keys (like name).
It's a kind of overwriting..
Even the simplest array like:
$arr = array(5,8);
is in fact:
Array
(
[0] => 5
[1] => 8
)
loop though and do each one individually
foreach($array as $val) {
print_r($val);
}
Any element of an array is a key-value pair.
So you can't have an item without key.
If it's just a matter of output, please do not use var_dump() as a way to output things to your end-user.
You can simply iterate through your main array with a foreach loop, and display items in a user-friendly way.
Related
This question already has answers here:
Can I get all keys of an multi level associative arrays in php
(5 answers)
Closed last year.
I am trying to create an single-dimension array of 'id's' extracted from a multidimensional array that will vary in depth. I need to extract the value from EACH array (no matter how deep). Each array has been indexed (see below) with the same keys. I have tried flattening so I could use 'array_column' (doesn't work because of the number of keys in each array), as well as methods like print_r(array_keys($data[0])[0]) (doesn't work for unknown dimension depth). It seems simple enough but I'm not finding any examples that are like this. Any direction is appreciated. Thank you.
Array
(
[0] => Array
(
[id] => 1000005
[first_name] => James
[last_name] => Smith
[position_root] => CHF CUST EX
[position_area] => Customer Operations
[items] => Array
(
[0] => Array
(
[id] => 1000134
[first_name] => Brandt
[last_name] => Jones
[position_root] => BS APL PJCTS
[position_area] => Customer Executive Support
[items] => Array
(
)
)
[1] => Array
(
[id] => 1000149
[first_name] => Daniel
[last_name] => Brown
[position_root] => CUST PROG
[position_area] => CUSTOMER PROGRAMS
[items] => Array
(
[0] => Array
(
[id] => 1000060
[first_name] => Duane
[last_name] => Pearson
[position_root] => CUST PRG IN
[position_area] => Customer Program Design
[items] => Array
(
)
)
What I am hoping for is:
[0] => 1000005
[1] => 1000134
[2] => 1000149
[3] => 1000060
... and so on ...
As suggested, you can use array_walk_recursive to achieve this.
<?php
$ids = [];
array_walk_recursive($data,function($value,$key) use (&$ids){
if($key == 'id') $ids[] = $value;
});
print_r($ids);
I have a structure as below. However there are 10,000 arrays under [data] and each array has about 100 keys
Array
(
[data] => Array
(
[0] => Array
(
[0] => Joe Bloggs
[1] => 01234
[2] => Manager
[3] => Male
[4] => 12
)
[1] => Array
(
[0] => Claire Bloggs
[1] => 456
[2] => Manager
[3] => Male
[4] => 12
)
[2] => Array
(
[0] => Mary Bloggs
[1] => 765756
[2] => Manager
[3] => Male
[4] => 12
)
My question is given a string, how would I quickly search this array and bring back the key value of 0, as an example, where I search for the 2nd key with a value of 01234. I have 10,000 numbers to go through
Only way I can think of is to loop though and check i.e.
foreach ($array['data'] as $k=>$v) {
if (if $v[1]=='01234') {
// FOUND THE KEY which is 0
}
}
$key = array_search('01234', array_column($initialArray['data'], 1));
var_dump($initialArray['data'][$key]);
is most likely the fastest solution.
in worst case (where your searched element is the last element) on 10000 elements (my machine, your results may vary):
array_filter (worst time)
0.0418s
foreach (worst time)
0.0451s
array_search + reference (worst time):
0.0034s
so array_search seems to be the fastest (out of those 3 at least)
EDIT: added modified GIST here for you to check times yourself:
https://gist.github.com/janmyszkier/2974796128aa017aa9d4fd38465bede8
I'm writing a code for looping out data from an multi dimensional array.
While looping I got confused while getting the details from an array. I have tried several ways for getting but in vain.
Now what I want is to get the values from the key 4 provided in the array.
Array
(
[match1] => Array
(
[4] => Array
(
[0] => Array
(
[0] => Sanjay
[1] => Delhi
[2] => 23
)
[1] => Array
(
[0] => Ram
[1] => Mumbai
[2] => 26
)
)
[5] => Array
(
[0] => Array
(
[0] => Sanjay
[1] => Delhi
[2] => 23
)
[1] => Array
(
[0] => Ram
[1] => Mumbai
[2] => 26
)
)
)
)
Thanks
In order to access the multi dimensional array you need to access via foreach() or directly by using the keys that you have in the print_r() function.
Hence as per your Example you can directly access the variable the you need using the
first array variable along with the key that the first array has.
Consider this array and you need to fetch the first value you can process like this.
print_r($var); resulting in this
Array
(
[match1] => Array
(
[4] => Array
(
[0] => Array
(
[0] => Sanjay
[1] => Delhi
[2] => 23
)
[1] => Array
(
[0] => Ram
[1] => Mumbai
[2] => 26
)
)
)
)
You can retrieve the variable in two methods as follows
Method One:
As the variable that contains the array is $var hence you need to access like this.
In order to fetch the value that the key has you can have $var['match1'][4] and you can apply foreach over to the variable and get the value that it has.
foreach($var['match1'][4] as $inner_value)
{
// Do what ever stuff you need
}
Method Two:
Getting key value 0 - 0 in the array that it has you can code as - $var['match1'][4][0]
Getting key value 1 - 1 in the array that it has you can code as - $var['match1'][4][1]
You can get as much value inside the array as you can with the help of the above two points
Output for both it will be the same as follows
Sanjay Delhi 23
Ram Mumbai 26
It's really very simple. Let's assume the name of your main array is $mainarray. So here is how you can get the array of key 4.
$key4array=$mainarray['match1'][4];
foreach($key4array as $arrayele) {
echo $arrayele[0]." ".$arrayele[1]." ".$arrayele[2]."<br>";
}
Output will be,
Sanjay Delhi 23
Ram Mumbai 26
Access the first level array by match1 key and then 4 as index to get the second level array.
Dear stackoverflow members,
I want to join 2 arrays in random as mentioned below, i tried different methods but i was not able to produce the result that i exactly wanted.
I have 2 arrays which looks like this. Lets call this names.
Array
(
[bill] => Array
(
)
[steve] => Array
(
)
[Ritchie] => Array
(
)
)
now these names are generated from another function, it's output looks something like this.
Array
(
[1] => Array
(
[email] => info#bill.com
[name] => bill
[web] => http://bill.com
)
[2] => Array
(
[email] => rich#steve.com
[name] => steve
[web] => http://steve.com
)
[3] => Array
(
[email] => god#linux.com
[name] => Ritchie
[web] => http://linux.com
)
[4] => Array
(
[email] => dummy#dummy.com
[name] => Ritchie
[web] => http://linux.com
)
)
and the 2nd array, lets call it countries.
Array
(
[0] => USA
[1] => UK
[2] => Netherlands
[3] => Australia
[4] => Germany
)
Now here is the exact problem. i want the 1st array names and the 2nd array countries to be joined and form another associative array in random order. But please note that the function which returns the name array returns the key : Ritchie twice, it has 2 occurrences. So the final array should be something like this.
Array
(
[Ritchie] => Array
(
[0] => USA,
[1] => Germany
)
[bill] => Array
(
[0] => Netherlands
)
[steve] => Array
(
[0] => UK
)
)
Since the key Ritchie has 2 occurrences, 2 unique values from the country array should be added. The number of keys or occurrences in names and the keys in countries will always be the same. If their is anything unclear, let me know i'll clear it out.
I did heavy research on the internet and stackoverflow and i was only able to come up with this. The current solution i have is this. Please be kind to help me to improve the current lines of code to suit my need or may be this might not be elegant, if so, please be kind to suggest a better solution.
$jumble = array();
foreach ($names as $name) {
$jumble[$name] = $countries[array_rand($countries)];
}
Thank you.
Try this code:
shuffle($countries);
$n = count($countries); $i = 0;
$jumble = array();
foreach ($names as $name) {
if (!isset($jumble[$name])) $jumble[$name] = array();
$jumble[$name][] = $countries[$i++ % $n];
}
Demo
I've got an array that looks like this:
Array
(
[0] => Array
(
[id] => abc
[name] => Charlotte
[state] => NC
)
[1] => Array
(
[id] => def
[name] => Tampa
[state] => FL
)
)
What I am trying to do is pull two of the values from each nested array ('id' and 'name'), run a function on them, and return an array that is then nested. So, for each 'id' and 'name,' pass that to "function work($id,$name)," which returns an array, such that the resulting array looks like this:
Array
(
[0] => Array
(
[id] => abc
[name] => Charlotte
[state] => NC
[restaurants] => Array (
[rname] => Good Burger
[rname] => McD
)
)
[1] => Array
(
[id] => def
[name] => Tampa
[state] => FL
[restaurants] => Array (
[rname] => BK
[rname] => White Castle
)
)
)
My searches on here found a few ways of pulling the values from the original arrays (foreach() loop), but I am unsure of the best way to pass these values to a function (array_walk doesn't appear to be an option in this case?), and especially of how to return a nested array into another nested array.
Am glad to provide clarification is need be.
foreach ($array as $key => $value){
$array[$key]['restaurants'] = work($value['id'],$value['name']);
}
function work($id,$name){
$results = array();
///process data
return $results;
}
Try something like this:
foreach($cities as &$city){
$city['restaurants'] = work($city['id'],$city['name']);
}
Demo with a dummy function.
The &$city tells PHP that you want to be able to modify the record in your loop (passes the array by reference instead of as a copy).
After this, you can simply set the restaurants value to the array returned by the work function.