I'm having trouble understanding how to search a multidimensional associative array with more than one value - I've seen multiple examples of how this can be done, but none of them seem to fit my exact scenario.
I have an array like this:
$locations = array(
ABC => array("loc1","loc2"),
DEF => array("loc2","loc3")
)
Note that 'loc2' is in both nested arrays.
I need to be able to search the array for a value that would match both the key and the value of each nested array using the value of another array that is generated by my application from an API, which looks like this:
Array (
[0] => Array (
[callnumber] => Test 8
[shelvinglocation] => loc1
[availability] => 1
[branch] => ABC
)
)
From this array, if both the branch (ABC) and the shelvinglocation (loc1) are found in the $locations array, then I want to output true.
Here's my code so far:
$instLine = "ABC";
$loc = "loc3";
if (array_key_exists($instLine, $locations)) {
foreach ($locations as $key => $value) {
if (in_array($loc, $value)) {
echo "match found";
} else {
echo "no match";
}
}
The output of this is "match found", because it's not specifically matching the array key to the list of locations, it's just searching all locations. The output should be "no match" because loc3 is found in the DEF array, not the ABC array.
How can I rewrite this so that for each given $instLine/$loc combination, the $loc is only looked for in the array that matches the $instline value?
I feel dumb because I'm sure the answer is simple and in the docs and I've just been using the wrong terminology to search for examples. I appreciate any pointers, even a 'here's the documentation you need, dummy'. :)
Thanks in advance!
$found=false;
foreach ($locations as $key => $value){
if($key == $instLine){
foreach($value as $k=>$v){
if($v == $loc){
$found=true;
}
}
}
}
if($found == true){
echo "match found";
} else {
echo "no match";
}
You should iterate over both arrays and set a boolean value to determine if the value exists or not. Your code is working as expected because you are just checking if the value of $loc exists in one of the arrays in your array (which it does). A conditional check for the matching key is also required
What you can do is store the array that matches the $instline value and then search it separately:
if (array_key_exists($instLine, $locations)) {
$arr = $locations[$instLine];
if (in_array($loc, $arr)) {
echo "match found";
} else {
echo "no match";
}
}
So I'm trying to find the details for a particular item in one array from a different array:
foreach($json_items['result']['items'] as $item)
{
foreach($items_all['items_game']['items'] as $schemaItem)
{
echo $Schema[$item['defindex']];
if($item['defindex'] == $Schema[$item['defindex']])
{
echo "works";
echo $schemaItem['name'].'<br />';
break;
} else {
//echo "not";
}
}
}
defindex is a unqiue ID for the item, Schema is a database type array of item info
but Schema is designed like this:
[1] => Array
(
[name] => Anti-Mage's Glaive
[prefab] => default_item
So 1 here would be the defindex for this item in the Schema array
What can I do so can compare them and get out the info such as name and prefab for the item? Thanks.
Is this inside of a loop through the array? If so, you could try:
foreach ($item as $key => $value){
// Do compare here, $key would be the array index $value the value.
}
You could access the array in $Schema
$item = $Schema[$item['defindex']];
$item_name = $item['name'];
$item_prefab = $item['prefab'];
i am having problems in exploding array and working with it.
i have array of serial numbers for books categorized as read and unread books.
when ever there is book that is read i place read in array after that serial number as follow.
array(
"1234567890","read",
"2345678901","read",
"2345678901",
"2345678901",
"1234561231",
"3333333333","read",
"3333333333"
)
status is unread for all others and in out put i want to loop through the array give me this result.
1234567890: 0 unread, 1 read
2345678901: 2 unread, 1 read
1234561231: 1 unread, 0 read
3333333333: 1 unread, 1 read
any help will be appreciated .
In the first time that array you have is not build in the best way, better would be :
$arr = array(
"1234567890","read",
"2345678901","read",
"2345678901","unread"
)
but you can do this:
$idx=count($arr);
$collector=array();
for($i=0;$i<$idx;$i++){
if($arr[$i]==='read') continue;
if(!isset($collector[$arr[$i]])){
$collector[$arr[$i]]=array(0,0);#array(read,unread)
}
if($arr[$i+1]==='read'){
$collector[$arr[$i]][0]++;
} else {
$collector[$arr[$i]][1]++;
}
}
#print it
foreach($collector as $id => $data){
print "$id: {$data[0]} read , {$data[1]} unread<br>".PHP_EOL;
}
But better try to collect the data in a better way!!!
Use Multidimensional array instead:
<?php
$arrBooks = array(
"1234567890" => array(
"read"=>'1',
"unread"=>'0'),
);
foreach ($arrBooks as $obj_key =>$book)
{
echo "$obj_key:<br>";
foreach ($book as $key=>$value){
echo "$key: $value<br>";
}
echo "<br>";
}
This way you can easily loop through the keys and get the data accordingly.
If you wish to update the arrays as well, here is the piece of code:
foreach ($arrBooks as $obj_key =>$book)
{
if($obj_key=="1234567890")
{
foreach ($book as $key=>$value){
if($key=="read")
{
$value+=1;
}
echo "$key: $value<br>";
}
}
}
I am trying to solve the following problem:
Note I am new to programming and PHP and working through SAMS PHP, MySQL and Apache (Julie C. Meloni).
There is an exercise where a multidimensional array has to be created. The outer array is an associative array listing the genres of movies. The genre arrays list movies of that genre. You are then asked to print out a list of genres with the list of films associated with that genre.
My code:
<?php
$genres = array(
'Science Fiction' => array('Star Trek', 'Star Wars', 'Alien'),
'Drama' => array('Les Amant de Pont Neuf', 'War & Peace', 'Bridehead Revisited'),
'Crime' => array('Heat', 'Pulp Fiction', 'Messerine')
);
$gKeys = array_keys($genres);
foreach ($gKeys as $genre) {
print $genre."<br/>";
}
?>
This works and prints out:
Science Fiction
Drama
Crime
Here's where I am running into a wall. When I try adding another foreach loop after
print $genre;
no results appear in the browser (except results of first loop). I have tried everything. For example:
starting by using the array_value() function applied to $genre and then try a foreach on the array returned.
In the textbook there is also a while (list($x) = ($y)) mechanism mentioned.
I thoroughly re-read the array chapter and have looked elsewhere to no avail.
perhaps I have structured the multidimensional array incorrectly? Do the second dimensional arrays need to be associative arrays also for consistency?
Your array is structured correctly. You're taking wrong array (array of just the keys) and adding another foreach loop after print $genre; hence it is not working.
No, it is not required for second dimensional arrays to be associative arrays as well.
<?php
$genres = array(
'Science Fiction' => array('Star Trek', 'Star Wars', 'Alien'),
'Drama' => array('Les Amant de Pont Neuf', 'War & Peace', 'Bridehead Revisited'),
'Crime' => array('Heat', 'Pulp Fiction', 'Messerine')
);
foreach ($genres as $genre => $movies)
{
print $genre . " - <br/>";
foreach ($movies as $movie)
{
print $movie . "<br/>";
}
print "</br>";
}
?>
Wait! Why are you doing:
$gKeys = array_keys($genres);
This just gives you a single array of keys.
Do your first loop on $genres, then inside:
foreach ($genre as $key=>$subgenre)
{
//do stuff here
}
foreach ($genres as $genrekey => $genrevalue) {
print $genrekey."<br/>";
foreach ($genrevalue as $value) {
print $value."<br/>";
}
}
function multiarray_keys($ar) {
foreach($ar as $k => $v) {
$keys[] = $k;
if (is_array($ar[$k]))
$keys = array_merge($keys, multiarray_keys($ar[$k]));
}
return $keys;
}
$gKeys = multiarray_keys($genres);
echo "<pre>";
print_r(multiarray_keys($array));
echo "</pre>";
Your outer array is an associative array with genres as keys.
To print out movies of that genre (the subarrays), use another foreach as follows:
print "<ul>";
foreach ($genres as $genre => $movies) {
print "<li>".$genre."<ul>";
foreach ($movies as $movie) {
print "<li>".$movie."</li>";
}
print "</ul></li>";
}
print "</ul>";
If you need this for seeing what data goes in an array for debugging purposes you could use PHPs var_export(), var_dump(), print_r() only to make sure the data is getting populated as you want it to. Otherwise if you want to show it in production you could do some like this:
foreach($multiDimentinalArray as $key => $value):
if(is_array($value)):
foreach($value as $k => $val):
print " $key.$k => $val";
else:
print "$key => $value";
endif;
endforeach;
I'm developing a php app that uses a database class to query MySQL.
The class is here: http://net.tutsplus.com/tutorials/php/real-world-oop-with-php-and-mysql/*note there are multiple bad practices demonstrated in the tutorial -- it should not be used as a modern guide!
I made some tweaks on the class to fit my needs, but there is a problem (maybe a stupid one).
When using select() it returns a multidimensional array that has rows with 3 associative columns (id, firstname, lastname):
Array
(
[0] => Array
(
[id] => 1
[firstname] => Firstname one
[lastname] => Lastname one
)
[1] => Array
(
[id] => 2
[firstname] => Firstname two
[lastname] => Lastname two
)
[2] => Array
(
[id] => 3
[firstname] => Firstname three
[lastname] => Lastname three
)
)
Now I want this array to be used as a mysql result (mysql_fetch_assoc()).
I know that it may be used with foreach(), but this is with simple/flat arrays. I think that I have to redeclare a new foreach() within each foreach(), but I think this could slow down or cause some higher server load.
So how to apply foreach() with this multidimensional array the simplest way?
You can use foreach here just fine.
foreach ($rows as $row) {
echo $row['id'];
echo $row['firstname'];
echo $row['lastname'];
}
I think you are used to accessing the data with numerical indices (such as $row[0]), but this is not necessary. We can use associative arrays to get the data we're after.
You can use array_walk_recursive:
array_walk_recursive($array, function ($item, $key) {
echo "$key holds $item\n";
});
With arrays in php, the foreach loop is always a pretty solution.
In this case it could be for example:
foreach($my_array as $number => $number_array)
{
foreach($number_array as $data = > $user_data)
{
print "Array number: $number, contains $data with $user_data. <br>";
}
}
This would have been a comment under Brad's answer, but I don't have a high enough reputation.
Recently I found that I needed the key of the multidimensional array too, i.e., it wasn't just an index for the array, in the foreach loop.
In order to achieve that, you could use something very similar to the accepted answer, but instead split the key and value as follows
foreach ($mda as $mdaKey => $mdaData) {
echo $mdaKey . ": " . $mdaData["value"];
}
Hope that helps someone.
Holla/Hello,
I got it! You can easily get the file name,tmp_name,file_size etc.So I will show you how to get file name with a line of code.
for ($i = 0 ; $i < count($files['name']); $i++) {
echo $files['name'][$i].'<br/>';
}
It is tested on my PC.
To get detail out of each value in a multidimensional array is quite straightforward once you have your array in place. So this is the array:
$example_array = array(
array('1','John','Smith'),
array('2','Dave','Jones'),
array('3','Bob','Williams')
);
Then use a foreach loop and have the array ran through like this:
foreach ($example_array as $value) {
echo $value[0]; //this will echo 1 on first cycle, 2 on second etc....
echo $value[1]; //this will echo John on first cycle, Dave on second etc....
echo $value[2]; //this will echo Smith on first cycle, Jones on second etc....
}
You can echo whatever you like around it to, so to echo into a table:
echo "<table>"
foreach ($example_array as $value) {
echo "<tr><td>" . $value[0] . "</td>";
echo "<td>" . $value[1] . "</td>";
echo "<td>" . $value[2] . "</td></tr>";
}
echo "</table>";
Should give you a table like this:
|1|John|Smith |
|2|Dave|Jones |
|3|Bob |Williams|
Example with mysql_fetch_assoc():
while ($row = mysql_fetch_assoc($result))
{
/* ... your stuff ...*/
}
In your case with foreach, with the $result array you get from select():
foreach ($result as $row)
{
/* ... your stuff ...*/
}
It's much like the same, with proper iteration.
Ideally a multidimensional array is usually an array of arrays so i figured declare an empty array, then create key and value pairs from the db result in a separate array, finally push each array created on iteration into the outer array. you can return the outer array in case this is a separate function call. Hope that helps
$response = array();
foreach ($res as $result) {
$elements = array("firstname" => $result[0], "subject_name" => $result[1]);
array_push($response, $elements);
}
I know this is quite an old answer.
Here is a faster solution without using foreach:
Use array_column
print_r(array_column($array, 'firstname')); #returns the value associated with that key 'firstname'
Also you can check before executing the above operation
if(array_key_exists('firstname', $array)){
print_r(array_column($array, 'firstname'));
}
Wouldn't a normal foreach basically yield the same result as a mysql_fetch_assoc in your case?
when using foreach on that array, you would get an array containing those three keys: 'id','firstname' and 'lastname'.
That should be the same as mysql_fetch_assoc would give (in a loop) for each row.
foreach ($parsed as $key=> $poke)
{
$insert = mysql_query("insert into soal
(pertanyaan, a, b, c, d, e, jawaban)
values
('$poke[question]',
'$poke[options][A]',
'$poke[options][B]',
'$poke[options][C]',
'$poke[options][D]',
'$poke[options][E]',
'$poke[answer]')");
}
If you need to do string manipulation on array elements, e.g, then using callback function array_walk_recursive (or even array_walk) works well. Both come in handy when dynamically writing SQL statements.
In this usage, I have this array with each element needing an appended comma and newline.
$some_array = [];
data in $some_array
0: "Some string in an array"
1: "Another string in an array"
Per php.net
If callback needs to be working with the actual values of the array,
specify the first parameter of callback as a reference. Then, any
changes made to those elements will be made in the original array
itself.
array_walk_recursive($some_array, function (&$value, $key) {
$value .= ",\n";
});
Result:
"Some string in an array,\n"
"Another string in an array,\n"
Here's the same concept using array_walk to prepend the database table name to the field.
$fields = [];
data in $fields:
0: "FirstName"
1: "LastName"
$tbl = "Employees"
array_walk($fields, 'prefixOnArray', $tbl.".");
function prefixOnArray(&$value, $key, $prefix) {
$value = $prefix.$value;
}
Result:
"Employees.FirstName"
"Employees.LastName"
I would be curious to know if performance is at issue over foreach, but for an array with a handful of elements, IMHO, it's hardly worth considering.
A mysql result set object is immediately iterable within a foreach(). This means that it is not necessary to call any kind of fetch*() function/method to access the row data in php. You can simply pass the result set object as the input value of the foreach().
Also, from PHP7.1, "array destructuring" allows you to create individual values (if desirable) within the foreach() declaration.
Non-exhaustive list of examples that all produce the same output: (PHPize Sandbox)
foreach ($mysqli->query("SELECT id, firstname, lastname FROM your_table") as $row) {
vprintf('%d: %s %s', $row);
}
foreach ($mysqli->query("SELECT * FROM your_table") as ['id' => $id, 'firstname' => $firstName, 'lastname' => $lastName]) {
printf('%d: %s %s', $id, $firstName, $lastName);
}
*note that you do not need to list all columns while destructuring -- only what you intend to access
$result = $mysqli->query("SELECT * FROM your_table");
foreach ($result as $row) {
echo "$row['id']: $row['firstname'] $row['lastname']";
}