find matching in multidimensional array - php
I have the following array:
$data['standard'][36][2] = 52.5;
$data['standard'][42][2] = 57.5;
$data['standard'][48][2] = 62.5;
$data['standard'][54][2] = 67.5;
$data['standard'][60][2] = 72.5;
$data['standard'][36][3] = 60.5;
$data['standard'][42][3] = 65.5;
$data['standard'][48][3] = 70.5;
$data['standard'][54][3] = 75.5;
$data['standard'][60][3] = 80.5;
$data['standard'][72][3] = 90.5;
i'm trying to return the keys of the third index where the first two match. e.g. for 'standard' and 48 need an array(2,3)
but for 'standard' and 72 i would return array(3)
Also I'm wondering if I should store this data in xml or something similar?
Try this:
$result = array_keys($data['standard'][48];
This just returns the keys of the $data['standard'][48] array: 2 and 3.
You can use something like this:
function findInArray(&$data,$param1,$param2)
{
return isset($data[$param1][$param2]) ? array_keys($data[$param1][$param2]) : array();
}
Example:
$keys = findInArray($data,"standard",48); // array(2,3);
$keys = findInArray($data,"standard",72); // array(3);
It is fine to use an array for storing data, if this is easier for you to handle.
Related
Looking for an element in an array in PHP
I don't know if I'm managing this array in the best way. The array I have is this: $bass = $_POST['bass']; $selected_scale = $_POST['scale']; $major_scales = array ( array("C","D","E","F","G","A","B","C","D","E","F","G","A","B",), array("C#","D#","E#","F#","G#","A#","B#","C#","D#","E#","F#","G#","A#","B#",), array("Db","Eb","F","Gb","Ab","Bb","C","Db","Eb","F","Gb","Ab","Bb","C",), array("D","E","F#","G","A","B","C#","D","E","F#","G","A","B","C#"), array("D#","E#","F##","G#","A#","B#","C##","D#","E#","F##","G#","A#","B#","C##"), array("Eb","F","G","Ab","Bb","C","D","Eb","F","G","Ab","Bb","C","D"), array("E","F#","G#","A","B","C#","D#","E","F#","G#","A","B","C#","D#"), array("E#","F##","G##","A#","B#","C##","D##","E#","F##","G##","A#","B#","C##","D##"), array("Fb","Gb","Ab","Bbb","Cb","Db","Eb","Fb","Gb","Ab","Bbb","Cb","Db","Eb"), array("F","G","A","Bb","C","D","E","F","G","A","Bb","C","D","E"), array("F#","G#","A#","B","C#","D#","E#","F#","G#","A#","B","C#","D#","E#"), array("Gb","Ab","Bb","Cb","Db","Eb","F","Gb","Ab","Bb","Cb","Db","Eb","F"), array("G","A","B","C","D","E","F#","G","A","B","C","D","E","F#"), array("G#","A#","B#","C#","D#","E#","F##","G#","A#","B#","C#","D#","E#","F##"), array("Ab","Bb","C","Db","Eb","F","G","Ab","Bb","C","Db","Eb","F","G"), array("A","B","C#","D","E","F#","G#","A","B","C#","D","E","F#","G#"), array("A#","B#","C##","D#","E#","F##","G##","A#","B#","C##","D#","E#","F##","G##"), array("Bb","C","D","Eb","F","G","A","Bb","C","D","Eb","F","G","A"), array("B","C#","D#","E","F#","G#","A#","B","C#","D#","E","F#","G#","A#"), array("B#","C##","D##","E#","F##","G##","A##","B#","C##","D##","E#","F##","G##","A##"), array("Cb","Db","Eb","Fb","Gb","Ab","Bb","Cb","Db","Eb","Fb","Gb","Ab","Bb") ); $bass is a string, like the one inside the arrays. The $selected_scale is just a number. What I'm trying to do is to find the $bass in one of those array in the position of $selected_scale. Basically, $bass = $major_scales[$selected_scale]. Therefore I want to create a loop in order to get the elements after that. But I don't know how to manage in this case the situation. I've looked everything in internet and try various solutions without success. I'd like to know how can I do it. Thanks
Try to use next loop: // if value exists in mentioned index if (in_array($bass,$major_scales[$selected_scale])){ // index of that value in that array $tmp_ind = array_search($bass,$major_scales[$selected_scale]); // length of the array $len = count($major_scales[$selected_scale]); // store values after this value $res = []; for ($i=$tmp_ind;$i<$len;$i++){ $res[$i] = $major_scales[$selected_scale][$i]; } } print_r($res); Demo1 If you need to find value by index $selected_scale in one of these arrays and also store values after this position: foreach($major_scales as $ar){ if ($ar[$selected_scale] == $bass){ // length of the array $len = count($ar); // store values after this value $res = []; for ($i=$selected_scale;$i<$len;$i++){ $res[$i] = $ar[$i]; } } } print_r($res); Demo2
php create arrays dynamically and merge
I'm trying to create an unknown number of arrays dynamically inside a foreach loop, merge them all at the end into one array, and use this in a JSON format for Google Analytics. So far I have the following code which is throwing an error at the merge part: $p=1; foreach(...){ ... $arr = 'arr'.$p; $name = $order->ProductGroupName; $name = str_replace("'", "", $name); $arr = array( "name"=>$name, "id"=>$order->ProductCode, "price"=>$order->RRP, "quantity"=>$order->Quantity ); $p++; } for ($q = 1; $q<$p; $q++){ $arry = 'arr'.$q; $merge = array_merge($arry, $merge); }; How do I create the arrays dynamically and merge them at the end, please? I'm relatively new to PHP and have tried my best to get this to work.
I think I understand what you're trying to do. Just dynamically append [] to the array and you don't need to merge: foreach($something as $order) { $arr[] = array ( "name"=>str_replace("'", "", $order->ProductGroupName), "id"=>$order->ProductCode, "price"=>$order->RRP, "quantity"=>$order->Quantity ); } If you want to have string keys for whatever reason, then: $p = 1; foreach($something as $order) { $arr["SomeText$p"] = array ( "name"=>str_replace("'", "", $order->ProductGroupName), "id"=>$order->ProductCode, "price"=>$order->RRP, "quantity"=>$order->Quantity ); $p++; } And that's it. Check with: print_r($arr); Things like $arry = 'arr'.$q; stink of variable variables (though not done correctly) and shouldn't be used.
PHP how to add slashes into array
i have a problem i want to add slashes at the starting and the end of each string of my array. This is an example of my actual array : $patte = array(); $patte[0] = "httpd"; $patte[1] = "vsftpd"; $patte[2] = 'gohphp'; $patte[3] = 'abcdef'; i use this array for taking information into a DataBase so i can't place slashes now, or this is going to not working. (mysql_query ... while mysql_fetch_array ...) I need to rename these entry. For this i use a second array, and with the command : "preg_replace" i can translate every strings like i want. But preg_replace want me to add slashes in $patte I want to obtain an array like this $pattes = array(); $pattes[0] = "/httpd/"; $pattes[1] = "/vsftpd/"; $pattes[2] = '/gohphp/'; $pattes[3] = '/abcdef/'; Can you help me please. I'm gonna have like 1000 line into this array.
Using array_map() you can apply callback to every element of your array : function addSlashes($str) { return "/".$str."/"; } $newArray = array_map("addSlashes", $patte);//array with the new values
Use array_map: $pattes = array_map(function($str) { return '/'.$str.'/'; }, $pattes);
Using variables to populate PHP arrays
For PHP, is it possible to do something like this: array( $designationVar => $dataVar ); With the idea being that I can dynamically create an array based on the values present
Yes why not , this is equivalent to like this $arr = array(); $arr[$keyname] = $value; In your case $arr = array(); $arr[$designationVar] = $dataVar;
While script inside of an array
pretty straight forward question this - I am trying to create an array to store the Model and Cost values taken from my database table. I figured I could begin the array, then create a while loop, and then end the array, and smiles all around. I may be mistaken, or I may have blindly missed something in my code, but could you have a look? $array = array( while ($overall_cost = mysql_fetch_assoc($query_ocost)) { $overall_cost["model"] => $overall_cost["cost"], } ); var_dump($array);
I think this is what you're looking for: $array = array(); while ($overall_cost = mysql_fetch_assoc($query_ocost)) { $array[$overall_cost["model"]] = $overall_cost["cost"]; } var_dump($array);
You can't do it like this. You need to add to the array inside the while loop: $array = array(); while ($overall_cost = mysql_fetch_assoc($query_ocost)) { $array[$overall_cost["model"]] = $overall_cost["cost"]; } var_dump($array); would be one way of doing it. EDITED to produce simple array.
I don't think that will work. Try something like: $array = array(); while ($overall_cost = mysql_fetch_assoc($query_ocost)) { $array[$overall_cost["model"]] = $overall_cost["cost"]; } var_dump($array);