I have the array below:
[1]=>
array(2) {
[0]=>
object(stdClass)#23 (7) {
["AddressesTableID"]=> string(1) "8"
["AccreditNo"]=> string(13) "5129876de28ff"
["Type"]=> string(4) "home"
["Street"]=> string(34) "Wallace, Village, Under the bridge"
["Municipality"]=> string(8) "Tortuous"
["Province"]=> string(8) "Sardonic"
["ContactNo"]=> string(8) "92012010"
}
[1]=>
object(stdClass)#24 (7) {
["AddressesTableID"]=> string(1) "9"
["AccreditNo"]=> string(13) "5129876de28ff"
["Type"]=> string(6) "office"
["Street"]=> string(25) "Rasputin Query, Palpitate"
["Municipality"]=> string(7) "Opulent"
["Province"]=> string(6) "Number"
["ContactNo"]=> string(8) "29101011"
}
}
Can you guys help me on how to transform this into:
Where all the values are grouped as an array into similar keys?
["AccreditNo"]=> array(2) { "5129876de28ff", "GKIJUGUIKGI" }
["Type"]=> array(2) { "home", "home" }
["Street"]=> ...
["Municipality"]=> ...
["Province"]=> ...
["ContactNo"]=> ...
Try with:
$input = array( /* your input data*/ );
$output = array();
foreach ( $input as $data ) {
foreach ( $data as $key => $value ) {
if ( !isset($output[$key]) ) {
$output[$key] = array();
}
$output[$key][] = $value;
}
}
You can use the array_merge_recursive() function, as follows:
$arr = array_merge_recursive($arr1, $arr2)
for more information, please see here:http://www.php.net/manual/en/function.array-merge-recursive.php
Related
I have an array which I want to iterate over to push the items into a select box, but I can't figure out how to do it.
The array I get from the function:
array(2) {
["de"]=> array(10) {
["id"]=> int(10)
["order"]=> int(1)
["slug"]=> string(2) "de"
["locale"]=> string(5) "de-DE"
["name"]=> string(7) "Deutsch"
["url"]=> string(34) "http://localhost/werk/Mol/de/haus/"
["flag"]=> string(66) "http://localhost/werk/Mol/wp-content/plugins/polylang/flags/de.png"
["current_lang"]=> bool(false)
["no_translation"]=> bool(false)
["classes"]=> array(4) {
[0]=> string(9) "lang-item"
[1]=> string(12) "lang-item-10"
[2]=> string(12) "lang-item-de"
[3]=> string(15) "lang-item-first"
}
}
["nl"]=> array(10) {
["id"]=> int(3)
["order"]=> int(2)
["slug"]=> string(2) "nl"
["locale"]=> string(5) "nl-NL"
["name"]=> string(10) "Nederlands"
["url"]=> string(26) "http://localhost/werk/Mol/"
["flag"]=> string(66) "http://localhost/werk/Mol/wp-content/plugins/polylang/flags/nl.png"
["current_lang"]=> bool(true)
["no_translation"]=> bool(false)
["classes"]=> array(4) {
[0]=> string(9) "lang-item"
[1]=> string(11) "lang-item-3"
[2]=> string(12) "lang-item-nl"
[3]=> string(12) "current-lang"
}
}
}
I tried a foreach but I did only get the indexes of the array
<?php
$translations = pll_the_languages(array('raw' => 1));
$lang_codes = array();
foreach ($translations as $key => $value) {
array_push($lang_codes, $key);
}
?>
I need the language slug, URL, and flag from all indexes in this array (de & nl), what should I do?
A simple iteration over the outer array and then pick the values you want from the sub array.
<?php
$translations = pll_the_languages(array('raw' => 1));
$lang_codes = array();
foreach ($translations as $lang => $info) {
$lang_codes[$lang] = [ 'slug' => $info['slug'],
'url' => $info['url'],
'flag' => $info['flag']
];
}
?>
You can approach this as
$res = [];
foreach($translations as $key => $value){
$res[$key] = [
'slug' => $value['slug'],
'url' => $value['url'],
'flag' => $value['flag']
];
}
Live Demo
guys i have multidimensional array which i got it from var_dump of $menu_order with this following array :
array(5) {
[0]=>
array(1) {
[0]=>
array(1) {
["variant_name"]=>
string(5) "Spicy"
}
}
[1]=>
array(2) {
[0]=>
array(1) {
["variant_name"]=>
string(5) "Spicy"
}
[1]=>
array(1) {
["variant_name"]=>
string(5) "small"
}
}
[2]=>
array(2) {
[0]=>
array(1) {
["variant_name"]=>
string(5) "Salty"
}
[1]=>
array(1) {
["variant_name"]=>
string(6) "medium"
}
}
[3]=>
array(2) {
[0]=>
array(1) {
["variant_name"]=>
string(12) "Mix of Herbs"
}
[1]=>
array(1) {
["variant_name"]=>
string(5) "large"
}
}
[4]=>
array(0) {
}
}
from that array, i need to get the variant_name become variant_menu_id with this following code :
foreach ($menu_order as $item) {
if (isset($item[0]["variant_name"])) {
foreach($item as $value) {
$variant_id[] = $this->Main_home_m->m_get_choice_id($value["variant_name"]);
}
} else {
$variant_id[] = array();
}
}
the model of m_get_choice_id have this following code :
Function m_get_choice_id($variant_name){
$this->db->select("variant_menu_id");
$this->db->from("uhd_variant_menu");
$this->db->where("variant_name",$variant_name);
$query = $this->db->get();
return $query->row_array();
}
the variant_id will be return to this multidimensional array :
array(8) {
[0]=>
array(1) {
["variant_menu_id"]=>
string(1) "3"
}
[1]=>
array(1) {
["variant_menu_id"]=>
string(1) "3"
}
[2]=>
array(1) {
["variant_menu_id"]=>
string(1) "6"
}
[3]=>
array(1) {
["variant_menu_id"]=>
string(1) "4"
}
[4]=>
array(1) {
["variant_menu_id"]=>
string(1) "7"
}
[5]=>
array(1) {
["variant_menu_id"]=>
string(1) "5"
}
[6]=>
array(1) {
["variant_menu_id"]=>
string(1) "8"
}
[7]=>
array(0) {
}
}
but i want the result variant_id become this multidimensional array :
array(5) {
[0]=>
array(1) {
[0]=>
string(1) "3"
}
[1]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "6"
}
[2]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "7"
}
[3]=>
array(2) {
[0]=>
string(1) "5"
[1]=>
string(1) "8"
}
[4]=>
array(0) {
}
}
guys can you help me how to get the multidimensional array?
thank you (:
Alternatively, you can create a temporary container holding the ids with an array. After getting them all as an array, push that whole batch inside a parent container:
$result = array();
foreach ($menu_order as $item) {
$temp = array(); // initialize temporary storage
if (isset($item[0]["variant_name"])) {
foreach($item as $value) {
$variant = $this->Main_home_m->m_get_choice_id($value["variant_name"]);
$temp[] = $variant['variant_menu_id']; // push single id into temporary storage
}
}
$result[] = $temp; // push ending batch
}
I have 1 arrays:
array(5) { // This is the keys on the CSV.
[0]=>
array(4) {
[0]=>
string(4) "user"
[1]=>
string(4) "date"
[2]=>
string(3) "md5"
[3]=>
string(4) "sha1"
}
[1]=>
array(4) {
[0]=>
string(4) "user1"
[1]=>
string(8) "02/02/15"
[2]=>
string(6) "123456"
[3]=>
string(5) "54321"
}
[2]=>
array(4) {
[0]=>
string(4) "user1"
[1]=>
string(8) "02/03/15"
[2]=>
string(6) "123456"
[3]=>
string(5) "54321"
}
[3]=>
array(4) {
[0]=>
string(5) "user2"
[1]=>
string(8) "02/02/15"
[2]=>
string(6) "112233"
[3]=>
string(6) "332211"
}
[4]=>
array(4) {
[0]=>
string(5) "user2"
[1]=>
string(8) "02/03/15"
[2]=>
string(6) "112244"
[3]=>
string(6) "332244"
}
}
So User 1 and User 2 each have a file that is MD5/SHA1 sum'd and checked.
I would like the data to turn from the above, to this:
array(3) {
["user"]=>
array(1) {
["date"]=>
array(2) {
[0]=>
string(3) "md5"
[1]=>
string(4) "sha1"
}
}
["user1"]=>
array(1) {
["02/02/15"]=>
array(2) {
[0]=>
string(6) "123456"
[1]=>
string(5) "54321"
}
["02/03/15"]=>
array(2) {
[0]=>
string(6) "123456"
[1]=>
string(5) "54321"
}
}
["user2"]=>
array(1) {
["02/02/15"]=>
array(2) {
[0]=>
string(6) "112233"
[1]=>
string(6) "332211"
}
["02/03/15"]=>
array(2) {
[0]=>
string(6) "112244"
[1]=>
string(6) "332244"
}
}
}
So far, I'm close. Here is my code:
<?php
error_reporting(E_ALL & ~E_NOTICE);
//Reading csv
$csv = array_map('str_getcsv',file('/var/www/sums.csv'));
debug($csv);
//User list
$userData = array();
foreach ($csv as $user) {
$ts = $user[1];
$un = $user[0];
$sums = array($user[2],$user[3]);
$userPreProc[$un] = array($ts => $sums);
$userData = array_merge($userPreProc, $userData);
}
debug($userData);
function debug($db) {
echo "<pre>";
var_dump($db);
echo "</pre>";
}
?>
The issue here is that only the first 'user1' and 'user2' entries are merged to the new array, so it looks like this:
array(3) {
["user"]=>
array(1) {
["date"]=>
array(2) {
[0]=>
string(3) "md5"
[1]=>
string(4) "sha1"
}
}
["user1"]=>
array(1) {
["02/02/15"]=>
array(2) {
[0]=>
string(6) "123456"
[1]=>
string(5) "54321"
}
}
["user2"]=>
array(1) {
["02/02/15"]=>
array(2) {
[0]=>
string(6) "112233"
[1]=>
string(6) "332211"
}
}
}
Any suggestions how I can ensure that all the keys are merged as expected?
Just shift off the first two elements and use those respective values as the first and second level keys.
Code: (Demo)
$result = [];
foreach ($array as $row) {
$result[array_shift($row)][array_shift($row)] = $row;
}
var_export($result);
Or use array destructuring in the foreach declaration: (Demo)
$result = [];
foreach ($array as [$user, $date, $md5, $sha1]) {
$result[$user][$date] = [$md5, $sha1];
}
var_export($result);
Just change your foreach loop to be like this. You don't need to merge your array via function.
foreach ($csv as $user) {
$ts = $user[1];
$un = $user[0];
$sums = array($user[2],$user[3]);
$userData[$un][$ts] = $sums;
}
I'm not even sure what to search for for this question. What I really want is I have an array of objects like this
array(3) {
[0]=>
object(stdClass)#423 (4) {
["name"]=>
string(3) "Blah"
["full_name"]=>
string(10) "/Blah"
["id"]=>
string(32) "BlahBlah"
["parent_id"]=>
string(32) "BlahBlah"
}
[1]=>
object(stdClass)#422 (4) {
["name"]=>
string(8) "Blah1"
["full_name"]=>
string(9) "Blah2"
["id"]=>
string(32) "BlahBlah2"
["parent_id"]=>
NULL
}
[2]=>
object(stdClass)#421 (4) {
["name"]=>
string(4) "Blah3"
["full_name"]=>
string(11) "Blah3"
["id"]=>
string(32) "BlahBlah3"
["parent_id"]=>
string(32) "BlahBlahBlah3"
}
}
I want to filter to just the object that I want so what I did was
$found_label = array_filter($labels, function($obj) use($label) {
return $obj->name === $label;
});
But then the results I got is this
array(1) {
[1]=>
object(stdClass)#422 (4) {
["name"]=>
string(8) "Blah1"
["full_name"]=>
string(9) "Blah1"
["id"]=>
string(32) "BlahBlah2"
["parent_id"]=>
NULL
}
}
But what I really want is just this
object(stdClass)#422 (4) {
["name"]=>
string(8) "Blah1"
["full_name"]=>
string(9) "Blah1"
["id"]=>
string(32) "BlahBlah2"
["parent_id"]=>
NULL
}
Then I have to do this to just get the actual object
$theKey = key($found_label);
return $found_label[$theKey];
I thought they should be a better way of doing this, also I'm new to PHP.
You can't do that with array_filter, there is no way to stop it and return only the first result. If you can't use the key to extract the result you want from the array returned from array_filter, you should use a loop. Something like this:
$label = "wantedLabel";
foreach ($labels as $l) {
if( $l->name === $label ) {
print_r ($l);
break;
}
}
This:
<?php
$labels = array(
"0" => (object) array('name' => "name1", "title" => "title1"),
"1" => (object) array('name' => "name2", "title" => "title2")
);
$label = "name1";
$found_label = array_filter($labels, function($obj) use($label) {
return $obj->name === $label;
});
print_r($found_label[0]);
Produces:
stdClass Object ( [name] => name1 [title] => title1 )
I grouped an array using the following script
$grouped_array = array();
foreach($ungrouped_array as $item) {
//group them by id
$grouped_array[$item['id']][] = $item;
}
Now this grouped array looks like this
array(3) {
[1]=>
array(2) {
[0]=>
array(1) {
["id"]=>
string(1) "1"
}
[1]=>
array(1) {
["id"]=>
string(1) "1"
}
}
[6]=>
array(1) {
[0]=>
array(1) {
["id"]=>
string(1) "6"
}
}
[2]=>
array(4) {
[0]=>
array(1) {
["id"]=>
string(1) "2"
}
[1]=>
array(2) {
["id"]=>
string(1) "2"
["sub"]=>
string(1) "1"
}
[2]=>
array(2) {
["id"]=>
string(1) "2"
["sub"]=>
string(1) "2"
}
[3]=>
array(1) {
["id"]=>
string(1) "2"
}
}
}
I have deleted the most part of the array to make it shorter but there is no [0] field in this grouped array
All array fields are given the name of [id]'s value. I have no problem with that, I just have to short it again by [ID]
any suggestion will be great.
This should work to get 1, 2, 6:
<?php
$grouped_array = array();
foreach($ungrouped_array as $item) {
$grouped_array[$item['id']][] = $item;
}
// sort by key.
ksort( $grouped_array, SORT_NUMERIC );
print_r( $grouped_array );