associative-array

traversing an array in php [duplicate]

≯℡__Kan透↙ 提交于 2019-12-02 10:10:42
Possible Duplicate: Loop an array of array So I know how to traverse an array of even key => value (associative), but I have a weird array where I need to walk through it and print out values: $object_array = array( 'type' => 'I am type', array( 'property' => 'value', 'property_2' => 'value_2' ) ); What I thought I could do is: foreach($object as $key=>$vlaue){ //what now? } So as you can see I am lost, how do I walk through the next array? foreach($object as $key=>$value){ if( is_array($value) ) { foreach($value as $key2=>$value2) { //stuff happens } } else { //other stuff ] } You can try:

Merge two associative arrays by same key

安稳与你 提交于 2019-12-02 09:47:34
I have two associative array having one value in common, like ARRAY 1( [0]=> array(2) { ["ID"]=> "AAAA" ["Name"]=> "Apple" } [1]=> array(2) { ["ID"]=> "BBBB" ["Name"]=> "Avocado" } [3]=> array(2) { ["ID"]=> "CCCC" ["Name"]=> "Banana" } ) Array2 ( [0]=> array(4) { ["ID"]=> "AAAA" ["Taste"]=> "Yumi" ["Location"]=> "France" ["Price"]=> "Cheap" } [1]=> array(4) { ["ID"]=> "CCCC" ["Taste"]=> "Yumi" ["Location"]=> "Africa" ["Price"]=> "Cheap" } [3]=> array(4) { ["ID"]=> "BBBB" ["Taste"]=> "Yumi" ["Location"]=> "America" ["Price"]=> "Expansive" } [3]=> array(4) { ["ID"]=> "HZGA" ["Taste"]=> "Berk" [

Array and Associative Array Merge

£可爱£侵袭症+ 提交于 2019-12-02 08:59:38
问题 How can I achieve the third array, merging Array1 and Array2? What's the best way to do that in PHP? Many thanks. Array2 has like index (key), the associative value of id in Array1. Array1 Array ( [0] => Array ( [id] => 56 [grade] => 6.7 ) [1] => Array ( [id] => 214 [grade] => 3.2 ) ) Array2 Array ( [56] => 2.4 [214] => 5.8 ) Result wanted Array ( [0] => Array ( [id] => 56 [grade] => 2.4 ) [1] => Array ( [id] => 214 [grade] => 5.8 ) ) 回答1: foreach($array1 as &$arrayItem) { $arrayItem['grade']

Merge and group by several arrays

我的未来我决定 提交于 2019-12-02 08:09:10
问题 I need to merge associative arrays and group by the name. Say I have such 3 arrays: ARRAY1 "/path/file.jpg" => 2, "/path/file2.bmp" => 1, "/file3.gif" => 5, ARRAY2 "/path/file.jpg" => 1, "/path/file2.bmp" => 1, "/file3.gif" => 0, ARRAY3 "/path/file.jpg" => 1, "/path/file2.bmp" => 1, I need to merge these arrays to one and group them by filepath and have result of sum of their values. Something like: SELECT filename, SUM(val) FROM files GROUP BY filename But with multiple input arrays. Arrays

Hash/associative array using several objects as key

狂风中的少年 提交于 2019-12-02 07:13:59
Is there a way of making an associative array where each key is a hash of several objects? I'm not interested in inspecting each object's state, but rather the object's identity. var myarray = {}; var a = new A(); var b = new B(); var c = new C(); // + is not right, but illustrates the hashing I'm after. myarray[a + b + c] = 42; The + operator is not right. In java I would arithmetically combine the System.identityHashCode() for each of these three instances and use the result to make my new hash key. Is there some similar mechanic in javascript? Overriding the .toString() method in A, B and C

php Get all values from multidimensional associative array

喜夏-厌秋 提交于 2019-12-02 07:13:25
问题 how can i get all values from multidimensional associative array I dont want to use print_r want to control my array put all the value in normal array with unique values my array is look like this array (size=10) 0 => array (size=3) 0 => array (size=1) 'Campaign' => string 'DEMO' (length=4) 1 => array (size=1) 'Campaign' => string 'Home_Sec' (length=8) 2 => array (size=1) 'Campaign' => string '' (length=0) 1 => array (size=0) empty 2 => array (size=0) empty 3 => array (size=1) 0 => array

Merge and group by several arrays

给你一囗甜甜゛ 提交于 2019-12-02 07:08:27
I need to merge associative arrays and group by the name. Say I have such 3 arrays: ARRAY1 "/path/file.jpg" => 2, "/path/file2.bmp" => 1, "/file3.gif" => 5, ARRAY2 "/path/file.jpg" => 1, "/path/file2.bmp" => 1, "/file3.gif" => 0, ARRAY3 "/path/file.jpg" => 1, "/path/file2.bmp" => 1, I need to merge these arrays to one and group them by filepath and have result of sum of their values. Something like: SELECT filename, SUM(val) FROM files GROUP BY filename But with multiple input arrays. Arrays are short (around 20 elements max). Each array might have different size. one possible way $rtn = array

PHP: While loop not working after adjusting SELECT for SQL injection prevention

拜拜、爱过 提交于 2019-12-02 05:16:31
I am trying to set up PHP queries for MySQL in a way to prevent SQL injection (standard website). I had a couple of INSERT queries where changing this worked well but on the following SELECT I keep getting an error since the update and it looks like the while loop doesn't work with the changes I made (it works well without using the statement as in the old code). Can someone tell me what I am doing wrong here ? New PHP: $stmt = $conn->prepare("SELECT ? FROM TranslationsMain WHERE location LIKE '%calendar weekday%' ORDER BY sortOrder, ?"); $stmt->bind_param('s', $selectedLang); $stmt->execute()

Array and Associative Array Merge

倖福魔咒の 提交于 2019-12-02 04:32:02
How can I achieve the third array, merging Array1 and Array2? What's the best way to do that in PHP? Many thanks. Array2 has like index (key), the associative value of id in Array1. Array1 Array ( [0] => Array ( [id] => 56 [grade] => 6.7 ) [1] => Array ( [id] => 214 [grade] => 3.2 ) ) Array2 Array ( [56] => 2.4 [214] => 5.8 ) Result wanted Array ( [0] => Array ( [id] => 56 [grade] => 2.4 ) [1] => Array ( [id] => 214 [grade] => 5.8 ) ) foreach($array1 as &$arrayItem) { $arrayItem['grade'] = $array2[$arrayItem['id']] } Here you will have array 1 merged as you wished 来源: https://stackoverflow.com

PHP Store Key Value from Associative Array into Simple Array

萝らか妹 提交于 2019-12-02 01:42:29
I'm having trouble wrapping my head around this, any help would be GREAT ... I have an array $stores that is structured like so: Array ( [0] => Array ( [id] => 123 [name] => 'Store A' ) [1] => Array ( [id] => 345 [name] => 'Store B' ) [2] => Array ( [id] => 567 [name] => 'Store C' ) [3] => Array ( [id] => 789 [name] => 'Store D' ) ) I want to extract the 'id' values from this array into a simple array that looks this: $simple = array(123,345,567,789); If you use php 5.5+, array_column() is quite useful : $simple = array_column($yourarray,'id'); http://php.net/array_column Calimero definitely