Searching php arrays - php

I have a PHP array question regarding searching which I'm hoping some kind person can help me with...
The array shown below is an collection of arrays, e.g. order items. As I loop a separate array of orderIds I would like to return the appropriate array of products.
For example, if I request an orderId of 98305 it would return the arrays with the indexes of 2 & 3.
Are there any PHP functions to do this? I could loop each array and check the value and break out when it matches, but I feel this brings quite an overhead of performing multiple loops per orderId lookup.
Array
(
[0] => Array
(
[orderId] => 98303
[product] => Product A
)
[1] => Array
(
[orderId] => 98304
[product] => Product B
)
[2] => Array
(
[orderId] => 98305
[product] => Product C
)
[3] => Array
(
[orderId] => 98305
[product] => Product D
)
[4] => Array
(
[orderId] => 98306
[product] => Product A
)
[5] => Array
(
[orderId] => 98306
[product] => Product B
)
)
Any help appreciated.
D

array_filter()
$output = array_filter($input,function($a) {
return $a['orderId'] == 98305;
});
Replace 98305 with the desired ID.

Related

php how to get all values array but array_values not working?

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.

Insert auto generated multidimensional array to database

I need to create a db function for multidimensional array. How deep the array currently dont know, bcoz they will come from xml file.
I have a sample array
Array
(
[employee] => Array
(
[0] => Array
(
[name] => Array
(
[lastname] => Kelly
[firstname] => Grace
)
[hiredate] => October 15, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Printer
[id] => 111
[price] => $111.00
)
[1] => Array
(
[product] => Laptop
[id] => 222
[price] => $989.00
)
)
)
)
[1] => Array
(
[name] => Array
(
[lastname] => Grant
[firstname] => Cary
)
[hiredate] => October 20, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Desktop
[id] => 333
[price] => $2995.00
)
[1] => Array
(
[product] => Scanner
[id] => 444
[price] => $200.00
)
)
)
)
[2] => Array
(
[name] => Array
(
[lastname] => Gable
[firstname] => Clark
)
[hiredate] => October 25, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Keyboard
[id] => 555
[price] => $129.00
)
[1] => Array
(
[product] => Mouse
[id] => 666
[price] => $25.00
)
)
)
)
)
)
I need to enter these type of array to db and then retrieve them in a good non programmer readable format
I created 2 table... 1st for array key with array level field and another for key=value
I tried this
function array_Dump($array, $d=1){
if (is_array($array)){
foreach($array as $key=>$val){
for ($i=0;$i<$d;$i++){
$level=$i;
}
if (is_array($val)){
if (is_int($key)){
array_Dump($val, $d+1);
}else{
$query = "insert into xml_array (level, input) VALUES ('$level','$key')";
insert_sql($query);
array_Dump($val, $d+1);
}
} else {
$query = "insert into xml_data (array_id,level_id, array_key,array_value) VALUES ('$insert_id','$level','$key','$val')";
insert_sql($query);
}
}
}
}
Create a table like this:
attributes(id, parent_id, properties)
where id will be the primary key, parent_id will be the id of the parent record and properties will be a small json field with the atomic properties. This way you support any depth the XML may throw towards your direction.
As about non-programmer representation. For instance you could use a table (you can solve that with divs as well) which will contain a row for each element in the top level array. Such a row would contain separate columns for each property. When a property is an array, then the given cell will be a table of its own, which will be handled similarly as the first table. It is advisable to make the inner tables collapsible, so if one wants to see the main levels only, the user will not have to scroll for a long while.

Array parsing using array value

i have below array,and i have amenities id = 50,i need to show amenities name like 'Express check-out' using amenities id = 50 from this array using php.
Array
(
[amenities] => Array
(
[0] => Array
(
[id] => 0
[name] => Cash machine
[key] => CASHMACHINE
)
[1] => Array
(
[id] => 42
[name] => Express check-in
[key] => EXPRESSCHECKINSERVICE
)
[2] => Array
(
[id] => 50
[name] => Express check-out
[key] => EXPRESSCHECKOUTSERVICE
)
[5] => Array
(
[id] => 3
[name] => Wi-Fi
[key] => WIFISERVICE
)
)
)
There are many ways that your problem can be solved. Easy way can be as follow
function getAmenities($array,$id){
foreach($array['amenities'] as $tmp_arr)
if($tmp_arr['id']==$id)
return $tmp_arr['name'];
}
echo getAmenities($array,50);
I have not checked result but should work fine. Please let me know if this works for you
How do u create this Array?
Can't u just use the id as the key when u create it, like:
$key = $array2['id'];
$array['amenities'][$key] = $array2;

code igniter form_dropdown() order

I am trying to order the dropdown items in alphabetical order but am unable to do so. I must be missing something obvious..
I assumed ORDER BY type_name would have created the array in alphabetical order
$data['training_types'] = $this->db->query("SELECT * FROM training_types ORDER BY type_name")->result_array();
print_r($training_types);
foreach ($training_types as $type)
{
$options[$type['id']] = $type['type_name'];
echo $options[$type['id']]; //test only: this displays the options in alphabetical order just fine
}
print_r($options);
echo form_dropdown('training_type',$options,'0');
//for some reason when the dropdown is created, the order is not alphabetical, it's not even ordered by id... I have no idea what is ordering it this way.
1st print_r returns:
Array ( [0] => Array ( [id] => 6 [type_name] => Independent Study ) [1] => Array ( [id] => 1 [type_name] => Instructor Lead ) [2] => Array ( [id] => 3 [type_name] => Instructor Lead/Virtual ) [3] => Array ( [id] => 7 [type_name] => Job Aid ) [4] => Array ( [id] => 5 [type_name] => Mentoring ) [5] => Array ( [id] => 2 [type_name] => Virtual ) [6] => Array ( [id] => 4 [type_name] => Web ) )
2nd print_r returns:
Array ( [2] => Virtual [3] => Instructor Lead/Virtual [4] => Web [1] => Instructor Lead [5] => Mentoring [6] => Independent Study [7] => Job Aid )
Can you print_r($data['training_types']) before the foreach loop and print_r($options) after the loop, and post results? This will help give insight as to what is going into the loop and what is coming out, to make sure it isn't the Form Helper form_dropdown() isn't reordering anything.
My suggestion is to just add a simple asort($options); before the form_dropdown() to insure it is alphabetical.
You didn't specify whether it's Ascending or Descending.
ORDER BY type_name ASC or ORDER BY type_name DESC

php reorder array based on order of other array

Given this array:
Array
(
[0] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[1] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[2] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
[3] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
)
The ssm_featured_post_id value corresponds to the value of the array items in the second array.
I want to order the first array items in the same order as the items in the second array
Array
(
[1] => 63
[0] => 70
[3] => 1
[2] => 49
)
so the result after sorting would be
Array
(
[0] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[1] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[2] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
[3] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
)
The simpler way would be to use usort and write a function that uses the second table to compare two values from first table.
You may want to check out array_multisort, particularly the third example given. The idea is that you create arrays based on the "columns" of the multidimensional array, then sort them simultaneously, and put the result back in the original array.

Categories