I have this array (i can't change it) :
array(3) {
[0]=>
array(8) {
["kampania"]=> string(6) "dasdas"
[0]=> string(6) "dasdas"
["300x250"]=> int(1)
[1]=> int(1)
["160x600"]=> int(2)
[2]=> int(2)
["728x90"]=> int(3)
[3]=> int(3)
}
[1]=>
array(8) {
["kampania"]=> string(12) "aaaaaaaaaaaa"
[0]=> string(12) "aaaaaaaaaaaa"
["300x250"]=> int(4)
[1]=> int(4)
["160x600"]=> int(5)
[2]=> int(5)
["728x90"]=> int(6)
[3]=> int(6)
}
[2]=>
array(8) {
["kampania"]=> string(20) "AAAAAAAAAAAAAAAAAAAA"
[0]=> string(20) "AAAAAAAAAAAAAAAAAAAA"
["300x250"]=> int(7)
[1]=> int(7)
["160x600"]=> int(8)
[2]=> int(8)
["728x90"]=> int(9)
[3]=> int(9)
}
}
As you can see, I got repeated values, because the array as the defined key and then an integer key.
How can I create a function which will remove the integer key and value from array and return a new array already "clean"
the result expect should be like this:
array(3) {
[0]=>
array(8) {
["kampania"]=> string(6) "dasdas"
["300x250"]=> int(1)
["160x600"]=> int(2)
["728x90"]=> int(3)
}
[1]=>
array(8) {
["kampania"]=> string(12) "aaaaaaaaaaaa"
["300x250"]=> int(4)
["160x600"]=> int(5)
["728x90"]=> int(6)
}
[2]=>
array(8) {
["kampania"]=> string(20) "AAAAAAAAAAAAAAAAAAAA"
["300x250"]=> int(7)
["160x600"]=> int(8)
["728x90"]=> int(9)
}
}
Thanks Guys! I'm really sorry for ask it , but I loose already so much time trying to fix it by myself
function unset_num_keys($array)
{
$array_out = array();
foreach($array AS $k => $v)
{
if(is_array($v)) //value is an array, so clean it
{
$array_out[$k] = unset_num_keys($v); //clean "child" arrays
}
elseif(!is_numeric($k))
{
$array_out[$k] = $v; // key is "safe"
}
}
return $array_out;
}
Then
$clean_array = unset_num_keys($old_array);
Loop threw the array and if the key(index) is numeric then remove that item from the array.
foreach($level1_array as &$arr){
foreach ($arr as $key => $value) {
if (is_int($key)) {
unset($arr[$key]);
}
}
}
Example
<?php
$level1_array = array(
array(
"smith",
"name" => "smith",
"20",
"age"=>20
),
array(
"smith",
"name" => "smith",
"20",
"age"=>20
),
array(
"smith",
"name" => "smith",
"20",
"age"=>20
),
);
foreach($level1_array as &$arr){
foreach ($arr as $key => $value) {
if (is_int($key)) {
unset($arr[$key]);
}
}
}
var_dump($level1_array);
?>
You can try this code:
//$data = $yourData;
$newData = array();
foreach($data as $v){
array_push($newData,array_flip($v));
}
Related
I am trying to convert an array formatted as the following:
object(Categories_store_tree)#519 (1) {
["list_of_sections":"Categories_store_tree":private]=> array(5) {
["id"]=> int(1)
["name"]=> string(11) "Main Store"
["parent_id"]=> NULL
["children"]=> array(2) {
[0]=> array(5) {
["id"]=> int(2)
["name"]=> string(4) "Food"
["parent_id"]=> int(1)
["children"]=> array(0) { }
}
[1]=> array(5) {
["id"]=> int(3)
["name"]=> string(14) "Electronics"
["parent_id"]=> int(1)
["children"]=> array(2) {
[0]=> array(5) {
["id"]=> int(4)
["name"]=> string(8) "Headphones"
["parent_id"]=> int(3)
["children"]=> array(0) { }
}
[1]=> array(5) {
["id"]=> int(5)
["name"]=> string(5) "Smartphones"
["parent_id"]=> int(3)
["children"]=> array(0) { }
}
}
}
}
}
}
To this structure of array:
object(Categories_store_tree)#964 (1) {
["list_of_sections":"Categories_store_tree":private]=> array(5) {
[0]=> array(4) {
["id"]=> int(1)
["name"]=> string(11) "Main Store"
["parent_id"]=> NULL
}
[1]=> array(4) {
["id"]=> int(2)
["name"]=> string(4) "Food"
["parent_id"]=> int(1)
}
[2]=> array(4) {
["id"]=> int(3)
["name"]=> string(14) "Electronics"
["parent_id"]=> int(1)
}
[3]=> array(4) {
["id"]=> int(4)
["name"]=> string(8) "Headphones"
["parent_id"]=> int(3)
}
[4]=> array(4) {
["id"]=> int(5)
["name"]=> string(5) "Smartphones"
["parent_id"]=> int(3)
}
}
}
Currently I am doing in manual but the idea is to make it automatic. I have tried with this code, but it returns an empty array, I have also tried with a function but I have been stuck for a couple of days and don't know what to do.
$clean_array = array();
$cont = 0;
foreach ( $new_tree as $key => $value ) {
if ( is_array( $value ) ) {
$cont++;
foreach ( $value as $key1 => $value1 ) {
if ( is_array( $value1 ) ) {
$cont++;
foreach ( $value1 as $key2 => $value2 ) {
$clean_array[$cont][$key2] = $value2;
}
} else {
$clean_array[$cont][$key1] = $value1;
}
}
} else {
$clean_array[$cont][$key] = $value;
}
}
Maybe you could try something like this.
Convert the object To an array:
function objectToArray($d) {
if (is_object($d))
$d = get_object_vars($d);
return is_array($d) ? array_map(__METHOD__, $d) : $d;
}
Then:
$arr = objectToArray($object);
print_r($arr);
Source
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
I have this foreach loop that outputs the below array, and I'm having a senior moment, I need it to return one array with no duplicate values, and I just can't it right.
foreach ( $post_groups as $post_group => $id ) {
group = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $table_name WHERE ID = %d", $id), ARRAY_A);
$groups[$group['group_name']] = $group['group_name'] = unserialize( $group['group_users'] );
}
output:
array(2) {
["Registered Users"]=>
array(1) {
[0]=>
string(1) "2"
}
["Admin Users"]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
}
Cheers
I believe the following is what you're after. Simply merge the arrays together and then ensure the result is unique.
$userIds = [
'Registered Users' => array(1,2,3),
'Admin Users' => array(3,4,5),
];
$allUserIds = array_unique(call_user_func_array('array_merge', $userIds));
var_dump($userIds);
/*
array(2) {
["Registered Users"]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
["Admin Users"]=>
array(3) {
[0]=>
int(3)
[1]=>
int(4)
[2]=>
int(5)
}
}
*/
var_dump($allUserIds);
/*
array(5) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[4]=>
int(4)
[5]=>
int(5)
}
*/
Use to php functions : array_unique(array_merge())
test code:
$userIds = array('RegisteredUsers' => array('A','a','b','B'),'AdminUsers' => array('C','c','B','d','a'));
$allUserIds = array_unique(array_merge($userIds['RegisteredUsers'], $userIds['AdminUsers']));
echo "<br><br>";
var_dump($userIds);
var_dump($allUserIds);
I have my main array:
array(6) {
[1]=> array(3) {
[0]=> string(15) "Extension"
[1]=> int(1)
[2]=> string(6) "3,00 "
}
[2]=> array(3) {
[0]=> string(32) "Physics "
[1]=> string(1) "1"
[2]=> string(6) "3,00 "
}
[3]=> array(3) {
[0]=> string(31) "Physics "
[1]=> int(1)
[2]=> string(6) "6,00 "
}
[4]=> array(3) {
[0]=> string(34) "Desk"
[1]=> int(4)
[2]=> string(8) "127,00 "
}
[5]=> array(3) {
[0]=> string(18) "assistance"
[1]=> int(1)
[2]=> string(7) "12,50 "
}
[6]=> array(3) {
[0]=> string(15) "Extension"
[1]=> int(1)
[2]=> string(6) "3,00 "
}
}
My expected output is:
Extension 2
Physics 2
Desk 1
Assistance 1
The result must be in an resultarray
How can I do? I tried with array_count_values function but don't work.
How can I stock answear:
I tried this code but It doesn't work
$tabrecap = array();
foreach($counts as $key=>$value){
//echo $key." qte".$value;
$tabrecap = array ($key,$value,$valueOption);
}
As you asked in comment,Please try this:-
<?php
$array = array( '1'=> array('0'=>"Extension", '1'=> 1, '2'=>"3,00 " ), '2'=> array('0'=>"Physics",'1'=>"1","3,00 " ),'3'=> array('0'=>"Physics",'1'=>1,"6,00 "),'4'=> array('0'=>"Desk",'1'=>4,"127,00 "),'5'=> array('0'=>"assistance",'1'=>1,"12,50 " ),'6'=> array('0'=>"Extension",'1'=>1,"3,00 "));
$count = array();
$i = 0;
foreach ($array as $key=>$arr) {
// Add to the current group count if it exists
if (isset($count[$i][$arr[0]])) {
$count[$i][$arr[0]]++;
}
else $count[$i][$arr[0]] = 1;
$i++;
}
print_r($count);
?>
Output:- https://eval.in/379176
Looping is the answer.
<?php
// untested
$counts = Array();
foreach( $array as $subArray ){
$value = $subArray[0];
$counts[ $value ] = ( isset($counts[ $value ]) )
? $counts[ $value ] + 1
: 1;
}
var_dump( $counts);
Just make a loop and use first item of each array as key :
$array = array(
array("Extension", 1, "3,00"),
array("Physics", "1", "3,00"),
array("Physics", 1, "6,00 ")
);
$count = array();
foreach($array as $a)
$count[$a[0]]++;
var_dump($count); // array(2) { ["Extension"]=> int(1) ["Physics"]=> int(2) }
edit:do not read the related topic, the answer below is clear and gives the solution, while the other topic just states the issue.
I have something weird here
My code looks like this:
var_dump($resultFlatTree);
foreach($resultFlatTree as &$element)
{
/*if(isset($element["action"]) && $element["action"] == "new")
{
//let's save the original ID so we can find the children
$originalID = $element["id"];
//now we get the object
$newObject = $setUpForDimension->createAnObject($dimension,$element,$customer);
$element['id'] = $newObject->getId();
echo "new";
//and let's not forget to change the parent_id of its children
$arrayFunctions->arrayChangingValues($resultFlatTree,"parent_id",$element['id'],$originalID);
$em->persist($newObject);
} */
}
$em->flush();
var_dump($resultFlatTree);
the code inside the foreach is commented to be sure that it's not what I'm doing that's changing the array.
here the array before the foreach:
array(3) {
[0]=>
array(10) {
["id"]=>
int(2)
["name"]=>
string(7) "Revenue"
["code"]=>
string(6) "700000"
["sense"]=>
string(2) "CR"
["lft"]=>
int(1)
["lvl"]=>
int(2)
["rgt"]=>
int(1)
["root"]=>
int(1)
["$$hashKey"]=>
string(3) "00D"
["parent_id"]=>
int(1)
}
[1]=>
array(10) {
["id"]=>
int(3)
["name"]=>
string(7) "Charges"
["code"]=>
string(6) "600000"
["sense"]=>
string(2) "DR"
["lft"]=>
int(3)
["lvl"]=>
int(2)
["rgt"]=>
int(4)
["root"]=>
int(1)
["$$hashKey"]=>
string(3) "00P"
["parent_id"]=>
int(4)
}
[2]=>
array(10) {
["id"]=>
int(4)
["name"]=>
string(6) "Energy"
["code"]=>
string(6) "606000"
["sense"]=>
string(2) "DR"
["lft"]=>
int(2)
["lvl"]=>
int(1)
["rgt"]=>
int(5)
["root"]=>
int(1)
["$$hashKey"]=>
string(3) "00E"
["parent_id"]=>
int(1)
}
}
and then after:
array(3) {
[0]=>
array(10) {
["id"]=>
int(2)
["name"]=>
string(7) "Revenue"
["code"]=>
string(6) "700000"
["sense"]=>
string(2) "CR"
["lft"]=>
int(1)
["lvl"]=>
int(2)
["rgt"]=>
int(1)
["root"]=>
int(1)
["$$hashKey"]=>
string(3) "00D"
["parent_id"]=>
int(1)
}
[1]=>
array(10) {
["id"]=>
int(3)
["name"]=>
string(7) "Charges"
["code"]=>
string(6) "600000"
["sense"]=>
string(2) "DR"
["lft"]=>
int(3)
["lvl"]=>
int(2)
["rgt"]=>
int(4)
["root"]=>
int(1)
["$$hashKey"]=>
string(3) "00P"
["parent_id"]=>
int(4)
}
[2]=>
&array(10) {
["id"]=>
int(4)
["name"]=>
string(6) "Energy"
["code"]=>
string(6) "606000"
["sense"]=>
string(2) "DR"
["lft"]=>
int(2)
["lvl"]=>
int(1)
["rgt"]=>
int(5)
["root"]=>
int(1)
["$$hashKey"]=>
string(3) "00E"
["parent_id"]=>
int(1)
}
}
As you can see, the last element is now changed and is by reference.
This completely messes up the processes I do with the array afterward.
Is that normal behavior ?
How can I avoid it ?
When you pass by reference to a foreach statement, you really should read the docs :)
http://php.net/manual/en/control-structures.foreach.php
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().
Basically, it's saying that when you pass by ref, it will remain locked on the last item due to an internal pointer.
The second user-comment at 40 points:
"Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset()."
I cannot stress this point of the documentation enough! Here is a simple example of exactly why this must be done:
<?php
$arr1 = array("a" => 1, "b" => 2, "c" => 3);
$arr2 = array("x" => 4, "y" => 5, "z" => 6);
foreach ($arr1 as $key => &$val) {}
foreach ($arr2 as $key => $val) {}
var_dump($arr1);
var_dump($arr2);
?>
The output is:
array(3) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> &int(6) }
array(3) { ["x"]=> int(4) ["y"]=> int(5) ["z"]=> int(6) }
Notice how the last index in $arr1 is now the value from the last index in $arr2!
There are more comments which you will find interesting if you look for "reference" in that link.
tl;dr:
It's a bit funny/buggy/weird/un-patched.
Understand what the implications are as you write your code and make space for them.