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.
Related
how to get all values array but set all keys the same name
ex :
this is my array
but i need remove this key .. I have used array_values() .. but not working
enter image description here
Seems like you want to grab all the arrays inside the item keys? Using array_column and specifying the item as the key will return the arrays inside each item key in a new array. If that is what you're after? The question was kind of hard to understand IMHO.
array_column($array, 'item');
This will return an array on the following structure.
Array
(
[0] => Array
(
[name] => ttt
[price] => 100
[quantity] => 1
)
[1] => Array
(
[name] => sss
[price] => 100
[quantity] => 1
)
[2] => Array
(
[name] => vvv
[price] => 100
[quantity] => 1
)
)
Here's a demo showing it working.
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.
I have a problem visualizing arrays greater than one dimension. I did a query on a database table and stored the data in an array, then used mysqli_fetch_array() to create another array. Now this array has the table name and the table data in but I'm having trouble figuring out how to A) access just the data and B) visualize what is actually going on here.
This is the output of print_r($keystore);
Array ( [0] => Array ( [0] => 4 [key_projects] => 4 ) [1] => Array ( [0] => 26 [key_projects] => 26 ) [2] => Array ( [0] => 25 [key_projects] => 25 ) [3] => Array ( [0] => 52 [key_projects] => 52 ) [4] => Array ( [0] => 53 [key_projects] => 53 ) )
What exactly is going on here?
Sometimes it's helpful when developing/debugging code that has arrays, to insert the HTML <pre> tag before and after the print_r() command:
echo "<pre>";
print_r($keystore);
echo "</pre>";
This will force the output into a format similar to John Kugelman's answer (subject to CSS rules). I find, from experience, that each iteration of Array() will be indented, when viewing plain text (i.e. no HTML)
Array
(
[0] => Array ( [0] => 4 [key_projects] => 4 )
[1] => Array ( [0] => 26 [key_projects] => 26 )
[2] => Array ( [0] => 25 [key_projects] => 25 )
[3] => Array ( [0] => 52 [key_projects] => 52 )
[4] => Array ( [0] => 53 [key_projects] => 53 )
)
I've added some whitespace to make the structure of the nested arrays clearer. I didn't change anything aside from adding spaces and newlines.
The outer array contains five entries from [0] to [4]. Each of these entries represents one row from the SQL result set.
for ($keystore as $row) {
print_r($row[0]);
print_r($row['key_projects']);
}
You'll notice the two entries in each $row are redundant. They both have the same value (e.g. 53 for the final entry). What's happening is the data is being returned indexed both by column number (0) and column name (key_projects). You can access the values using whichever one you choose, number or name: $row[0] or $row['key_projects'].
I have an array (array01) that contains a bunch of sub arrays consisting of two pieces of data each, like:
Array ( [0] => Array ( [0] => 10CC [1] => Dreadlock Holiday )
[1] => Array ( [0] => 10CC [1] => I\'m Not In Love )
[2] => Array ( [0] => 10CC [1] => Dreadlock Holiday ) )
etc...
I have another array (array02) like:
Array ( [66] => Array ( [0] => 10CC [1] => Dreadlock Holiday )
[585] => Array ( [0] => 10CC [1] => I\'m Not In Love )
etc...
I'm successfully using foreach and then in_array to see what array01 elements are in array02. However, what I am struggling to figure out is how to get the id of what element in array2 the hit was on.
So for example, array01's 0 and 2 elements (both 10CC, Dreadlock Holiday), are matched in array02, but how do I get the ID of the element (in this case, 66)?
Thanks for your help.
Have you tried the array_search function?
look for documentation for the array_intersect() in PHP manual. Note that the original keys are preserved so it returns an array with the elements present on the array02 array with the original keys.
Hope it's what you're looking for
Cheers
this relates to my previous post.
how to create a collection of multi dimensional arrays and not overwrite origional values when new ones are pushed
i am thinking my problem has to do with how i am creating the array. what i'm trying to do is make an array that looks like this
Array
(
[10] => Array
(
[0] => 29
[1] => 36
)
)
into something like this
Array
(
[10] => Array
(
[0] => 29
[1] => 36
)
[20] => Array
(
[0] => 29
[1] => 36
)
[25] => Array
(
[0] => 29
[1] => 36
)
)
the 10, 20, and 25 is the product id where the numbers within those are the selections that were selected on that page (in the link i gave above). so each product would have its own collection selected.
when i use array_push instead of doing what i want it to do the first collection of array as in the first example keep reseting. so if i do my selections on say flyers and add to cart then i go to business cards and do my selections and add to cart the array resets and it becomes like the first example. whatever i try i cant get it to merge below a collection like the second example that i have. i have tried array_merge(),array_push but those dont really work.
Solution:-
If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator:
$a = array(10 => array(25,26));
$b = array(22 => array(45,66));
$c = $a + $b;
print_r($c);
Output:-
Array
(
[10] => Array
(
[0] => 25
[1] => 26
)
[22] => Array
(
[0] => 45
[1] => 66
)
)
Hope this helps.