I have a problem I've been trying to solve for two weeks now, please I need help on this. I do not understand using array well so please bear with me.
I'm trying to combine two arrays and store them in one array so I did this.
$oldvalue =$_SESSION[oldmids]; //value example aa=1,bb=2,cc=3;
$newvalue =$_SESSION[newmids]; //value example 001,002,003;
$result =array();
foreach($oldvalue as $oldval){
$kk =explode('=',$oldval);//i want to keep my tagging so
$oldtag =$kk[0]; // I use explode.
foreach($newvalue as $newid){
$kk =$oldtag.$newid;
$result[] =$kk;
}
}
// print_r($result);
my goal here is to keep my old tag and replace with numeric value, I don't have a problem with
the output, but I only need to get a unique value from my array $result. I tried using array_unique, but failed. Is this the right approach?
I'm not exactly sure what you are trying to do, but I think that array_push() is what you want:
http://www.php.net/array_push
This is my best guess:
$oldvalue =$_SESSION[oldmids]; //value example aa=1,bb=2,cc=3;
$newvalue =$_SESSION[newmids]; //value example 001,002,003;
$result = array();
foreach($oldvalue as $oldval){
$kk =explode('=',$oldval);//i want to keep my tagging so
$oldtag =$kk[0]; // i use explode.
foreach($newvalue as $newid){
$kk =$oldtag.$newid;
array_push($result, $kk);
}
}
print_r($result);
Related
The question is whether it's possible to address a multidimensional array with a string or another array.
Say we have:
$a['key1']['key2'] = "value"
//and
$keyArray = array('key1', 'key2')
//or
$keyString = 'key1,key2'
Is it possible to do something like:
$a[$keyArray]
//or
$a[keyString]
which would then give me back the value "value"?
Obviously, what I wrote doesn't work (not on my system, at least), but is something similar possible and if yes, how?
Thanks.
As far as I'm aware this isn't possible. However I feel like http://php.net/manual/en/class.arrayiterator.php might be help you answer your question
If you want a one liner:
$a['key1']['key2'] = "value";
$keyArray = array('key1', 'key2');
$value=$a;foreach ($keyArray as $key) $value=$value[$key];
echo $value;
You should note that this obviously doesn't check if the keys exist.
Obviously you cannot pass two indices using some string or one array value. But if you need to use it that way then one way around can be like:
$a['key1']['key2'] = "value"
$keyArray = array('key1', 'key2');
$a[$keyArray[0]][$keyArray[1]]//'value'
That's just in case you are somehow receiving key values in $keyArray and you can access $a only through that otherwise direct access is most convenient:
$a['key1']['key2']//'value'
I hope it helps
I think this is a naive question, but I can't find the proper syntax.
I have this code:
for ($i=1; $i<count($MyArray1); $i++){
$element=$MyArray1[$i];
$foo = $AnotherArray[$element];
echo $foo;
}
How can I skip the second line? I mean, the third line to be something like
$foo = $AnotherArray[$MyArray1[$i]];
for ($i=1; $i<count($MyArray1); $i++){
echo $AnotherArray[$MyArray1[$i]];
}
You can skip a fair amount of that code to make it a bit clearer. Firstly use foreach instead of for as it's a much more reliable way of iterating over arrays. Secondly I've broken down what you're trying to do, to simplify how you're getting it. Basically using the values of one array as the keys of another. So how to do it in three lines:
foreach(array_intersect_key($AnotherArray, array_flip($MyArray1)) as $value) {
echo $value;
}
This is using the excellent array_intersect_key method to grab all of the values from $AnotherArray with keys that match in the other array. As you want to use the values, we use array_flip to swap the keys and values, then just loop over the result and echo it.
I am tring to do (like) a 2 dimensional array, case insensitive.
I have:
foreach ($rows as $key=>$row) {
$names[$key]=$row['Name'];
}
array_multisort($rows,SORT_STRING|SORT_FLAG_CASE,$names);
The above ends up producing the same result (with or without case flag).
Sick of staring at this, any ideas from somebody outside?
First of all SORT_FLAG_CASE is only available in PHP v5.4+ so I suggest checking which version of PHP you are running (maybe 'uksort' could help if 5.3ish).
If not, make sure all the values that you put into $names lowercase or uppercase.
You have the order of the arguements $rows and $names reversed in the call to array_multisort.
Lastly if it comes from a database (or some other manner that means you cant change the data on the way into the array) then you can use array_walk.
Hope that helps
Since I ran into this with PHP 5.3.16, I thought I'd share my simple solution: just convert your keys to lower (or upper) case, e.g.:
foreach ($rows as $key=>$row) {
$names[$key]=strtolower($row['Name']);
}
array_multisort($names,SORT_STRING,$rows);
I also swapped the $rows & $names and removed the SORT_FLAG_CASE (to get rid of the log message).
You can also do a sort within a sort, so you can use usort with strcasecmp:
foreach ($rows as $key=>$row) {
$names[$key]=row['Name'];
}
array_multisort(usort($names,strcasecmp),$rows);
Above answer didn't work, because the first array got mingled while the other one didn't. So I wrote a general multidimensional array case insensitive compare function. It also can use multiple keys:
function array_casecmp($keys) {
if (gettype($keys) != "array") $keys = func_get_args();
return function ($a, $b) use ($keys) {
foreach($keys as $value) {
$akeys = $akeys . $a[$value];
$bkeys = $bkeys . $b[$value];
}
return strcasecmp($akeys, $bkeys);
};
}
Use it like:
usort($files,strcasecmp(array(0,1)); // with standard array
usort($files,strcasecmp(1); // single key
usort($files,strcasecmp(0,1); // arguments are converted to array
usort($files,strcasecmp("dir","link")); // you can also use symbolic keys
usort($files,strcasecmp(0,1,2,3,4,...); // use as many keys as you like
Keep in mind that the keys are concatenated, so maybe you have to use str_pad in your array colums to keep them seperated in the right way.
I am trying to prevent duplicates from occuring in a final array. I am trying to check for duplicates in a list of $media_candidate objects and compile them:
$iterator = 0;
// ensure items in final array are unique
while ((count($final_array) < $numResults) && ($iterator < count($media_data))) {
$media_candidate = $media_data[$iterator++];
if(!in_array($media_candidate['id'], $final_array)){
$final_array[] = $media_candidate;
}
}
As you can see in a print out of $final_array the last three elements are appearing 3 times with id, 343050519221992426_18478933. Any ideas as to what's going on?
First of all: You do not truncate the final array, so that all doublettes will end up at the end.
Second: You are reinventing the wheel: Read up on array_unique()
Edit
Third: After your edit, there is an even easier way:
$final_array=array();
foreach($media_data as $m) $final_array[$m['id']]=$m;
//You might want the next line or not
$final_array=array_values($final_array);
In essence you outsource the uniqueness to the hash keys of the array.
Try with:
if(!in_array($media_candidate['id'], $final_array)){
$final_array[] = $media_candidate['id'];
}
With $final_array[] you add new element at the end of the array.
You are checking $media_candidate['id'] but inserting $media_candidate in $final_array
Try array_unique function like this
$final_array = array_unique($media_candidate);
I'm using a nestedsortable jQuery plugin that gives me the order/sort of the elements serialized.
And example of this serialitzation (root means parent_id=0):
id[1]=root&id[5]=1&id[2]=1&id[3]=1&id[4]=3
First thing I'll do is explode by &:
$serialized = "id[1]=root&id[5]=1&id[2]=1&id[3]=1&id[4]=3";
$exploded = explode("&", $serialized);
But I don't know then how to manage a id[1]=root or id[3]=1. How I can do it?
And another question. In this way I don't know which is how to store the order. When I've the exploded with in array like array("id"=>1, "parent"=>"root"); I've to store the order. I will do it with an index, but how I recognise nested levels?
An example:
$i = 0;
foreach($exploded as $explode)
{
//update every id in MySQL and set parent=$explode["parent"] and order=$i
$i++;
}
But if I've N levels, how I can have a index $i for every one of them?
Thank you in advance!
Rather than exploding, you could try parse_str()
<?php
parse_str("id[1]=root&id[5]=1&id[2]=1&id[3]=1&id[4]=3",$result);
print_r($result);
?>
From there you can work with the array.